Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dgraph-lambda-go

*Note: this is a fork, for the original project by Schartey go to https://github.com/Schartey/dgraph-lambda-go*

Go Library written to build Dgraph Lambda servers as an alternative to the [Dgraph JS Lambda Server](https://github.com/dgraph-io/dgraph-lambda)

It is currently in **development**! Please create an issue if something is not working correctly.
Expand All @@ -9,10 +11,10 @@ If you would like to support me please visit my [:coffee:](https://ko-fi.com/sch
## Getting started

- Create project ```go mod init```
- To install dgraph-lambda-go run the command ```go get -d github.com/schartey/dgraph-lambda-go``` in your project directory.
- Then initialize the project by running ```go run github.com/schartey/dgraph-lambda-go init```.
- To install dgraph-lambda-go run the command ```go get -d github.com/miko/dgraph-lambda-go``` in your project directory.
- Then initialize the project by running ```go run github.com/miko/dgraph-lambda-go init```.
- Set path to your graphql schema in lambda.yaml
- Generate types and resolvers ```go run github.com/schartey/dgraph-lambda-go generate```
- Generate types and resolvers ```go run github.com/miko/dgraph-lambda-go generate```
- Implement your lambda resolvers
- Run your server ```go run server.go```

Expand All @@ -36,7 +38,7 @@ When first initializing the lambda server it will generate a basic lambda.yaml f
- "Home"

autobind:
- "github.com/schartey/dgraph-lambda-go/examples/models"
- "github.com/miko/dgraph-lambda-go/examples/models"

resolver:
dir: lambda/resolvers
Expand Down
2 changes: 1 addition & 1 deletion cmd/example.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/schartey/dgraph-lambda-go/examples"
"github.com/miko/dgraph-lambda-go/examples"
"github.com/urfave/cli/v2"
)

Expand Down
10 changes: 5 additions & 5 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/generator"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/generator"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/internal"
"github.com/urfave/cli/v2"
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/generator"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/generator"
"github.com/miko/dgraph-lambda-go/internal"
"github.com/urfave/cli/v2"
)

Expand Down
6 changes: 3 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/schartey/dgraph-lambda-go/codegen/graphql"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/graphql"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/internal"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"golang.org/x/tools/go/packages"
Expand Down
22 changes: 11 additions & 11 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"path/filepath"
"testing"

"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/internal"
"github.com/stretchr/testify/assert"
)

var autobindValues = []string{"github.com/schartey/dgraph-lambda-go/examples/models"}
var autobindValues = []string{"github.com/miko/dgraph-lambda-go/examples/models"}
var models = []string{"User", "Author", "Apple", "Figure", "Hotel"}
var fieldResolvers = []string{"User.reputation", "User.rank", "User.active", "Post.additionalInfo", "Figure.size"}

Expand All @@ -22,7 +22,7 @@ var mutationResolvers = []string{"newAuthor"}
var middlewareResolvers = []string{"user", "admin"}

func Test_LoadConfig(t *testing.T) {
config, err := LoadConfigFile("github.com/schartey/dgraph-lambda-go", "../../lambda.yaml")
config, err := LoadConfigFile("github.com/miko/dgraph-lambda-go", "../../lambda.yaml")
assert.NoError(t, err)
err = config.LoadConfig("../../lambda.yaml")
assert.NoError(t, err)
Expand All @@ -32,38 +32,38 @@ func Test_LoadConfig(t *testing.T) {
assert.Equal(t, "generated", config.Exec.Package)
assert.Equal(t, "examples/lambda/model/models_gen.go", config.Model.Filename)
assert.Equal(t, "model", config.Model.Package)
assert.Equal(t, "github.com/schartey/dgraph-lambda-go/examples/models", config.AutoBind[0])
assert.Equal(t, "github.com/miko/dgraph-lambda-go/examples/models", config.AutoBind[0])
assert.Equal(t, "follow-schema", config.Resolver.Layout)
assert.Equal(t, "examples/lambda/resolvers", config.Resolver.Dir)
assert.Equal(t, "resolvers", config.Resolver.Package)
assert.Equal(t, "{resolver}.resolver.go", config.Resolver.FilenameTemplate)
assert.Equal(t, true, config.Server.Standalone)
assert.Equal(t, "github.com/schartey/dgraph-lambda-go", config.Root)
assert.Equal(t, "github.com/miko/dgraph-lambda-go", config.Root)
assert.NotNil(t, config.DefaultModelPackage)
assert.Equal(t, "model", config.DefaultModelPackage.Name)
assert.Equal(t, "github.com/schartey/dgraph-lambda-go/examples/lambda/model", config.DefaultModelPackage.PkgPath)
assert.Equal(t, "github.com/miko/dgraph-lambda-go/examples/lambda/model", config.DefaultModelPackage.PkgPath)
assert.Equal(t, 3, len(config.Sources))
assert.Contains(t, config.Sources[2].Name, "dgraph-lambda-go/examples/test.graphql")
}

func Test_LoadConfig_Fail(t *testing.T) {
// Non existent file
_, err := LoadConfigFile("github.com/schartey/dgraph-lambda-go", "./lambda.yaml")
_, err := LoadConfigFile("github.com/miko/dgraph-lambda-go", "./lambda.yaml")
assert.Error(t, err)

// Invalid file type
_, err = LoadConfigFile("github.com/schartey/dgraph-lambda-go", "./config.go")
_, err = LoadConfigFile("github.com/miko/dgraph-lambda-go", "./config.go")
assert.Error(t, err)

for i := 1; i < 6; i++ {
// Invalid file type
_, err = LoadConfigFile("github.com/schartey/dgraph-lambda-go", fmt.Sprintf("../../test_resources/faulty%d.yaml", i))
_, err = LoadConfigFile("github.com/miko/dgraph-lambda-go", fmt.Sprintf("../../test_resources/faulty%d.yaml", i))
assert.Error(t, err)
}
}

func Test_loadSchema(t *testing.T) {
config, err := LoadConfigFile("github.com/schartey/dgraph-lambda-go", "../../lambda.yaml")
config, err := LoadConfigFile("github.com/miko/dgraph-lambda-go", "../../lambda.yaml")
assert.NoError(t, err)
err = config.LoadConfig("../../lambda.yaml")
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/executer_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateExecuter(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand Down Expand Up @@ -64,7 +64,7 @@ func generateExecuter(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rew
pkgs["errors"] = types.NewPackage("errors", "errors")
pkgs["http"] = types.NewPackage("net/http", "http")
pkgs["strings"] = types.NewPackage("strings", "strings")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")

if len(parsedTree.ResolverTree.FieldResolvers) > 0 ||
len(parsedTree.ResolverTree.Queries) > 0 ||
Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/field_resolver_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateFieldResolvers(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand All @@ -35,7 +35,7 @@ func generateFieldResolvers(c *config.Config, parsedTree *parser.Tree, r *rewrit
}
if len(parsedTree.ResolverTree.FieldResolvers) > 0 {
pkgs["context"] = types.NewPackage("context", "context")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")
}

err = fieldResolverTemplate.Execute(f, struct {
Expand Down
16 changes: 12 additions & 4 deletions codegen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strings"
"unicode"

"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/graphql"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
"github.com/pkg/errors"
"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/graphql"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
)

func Generate(c *config.Config, p *parser.Tree, r *rewriter.Rewriter) error {
Expand Down Expand Up @@ -96,10 +96,18 @@ func args(args []*parser.Argument) string {
return strings.Join(arglist, ",")
}

//Replace reserved words which could cause go compiler to fail
var ReservedMap map[string]string = map[string]string{
"type": "xtype",
}

func argsW(args []*parser.Argument) string {
var arglist []string

for _, arg := range args {
if replaced, exists := ReservedMap[arg.Name]; exists {
arg.Name = replaced
}
arglist = append(arglist, fmt.Sprintf("%s %s", arg.Name, pointer(arg.GoType, arg.IsArray)))
}
return strings.Join(arglist, ",")
Expand Down
6 changes: 3 additions & 3 deletions codegen/generator/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"text/template"

"github.com/pkg/errors"
"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/config"
)

func GenerateConfig(filename string) error {
Expand Down Expand Up @@ -119,7 +119,7 @@ model:
package: model

autobind:
# - "github.com/schartey/dgraph-lambda-go/examples/models"
# - "github.com/miko/dgraph-lambda-go/examples/models"

resolver:
dir: lambda/resolvers
Expand All @@ -146,7 +146,7 @@ import (
"github.com/go-chi/chi"
{{ end }}

"github.com/schartey/dgraph-lambda-go/api"
"github.com/miko/dgraph-lambda-go/api"
"{{ .GeneratedPath }}"
"{{ .ResolverPath }}"
)
Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/middleware_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateMiddleware(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand All @@ -25,7 +25,7 @@ func generateMiddleware(c *config.Config, parsedTree *parser.Tree, r *rewriter.R
pkgs := make(map[string]*types.Package)

if len(parsedTree.Middleware) > 0 {
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")
}

err = middlewareResolverTemplate.Execute(f, struct {
Expand Down
4 changes: 2 additions & 2 deletions codegen/generator/model_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"golang.org/x/tools/go/packages"
)

Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/mutation_resolver_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateMutationResolvers(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand Down Expand Up @@ -38,7 +38,7 @@ func generateMutationResolvers(c *config.Config, parsedTree *parser.Tree, r *rew
}
if len(parsedTree.ResolverTree.Mutations) > 0 {
pkgs["context"] = types.NewPackage("context", "context")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")
}

err = mutationResolverTemplate.Execute(f, struct {
Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/query_resolver_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateQueryResolvers(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand Down Expand Up @@ -39,7 +39,7 @@ func generateQueryResolvers(c *config.Config, parsedTree *parser.Tree, r *rewrit
}
if len(parsedTree.ResolverTree.Queries) > 0 {
pkgs["context"] = types.NewPackage("context", "context")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")
}

err = queryResolverTemplate.Execute(f, struct {
Expand Down
8 changes: 4 additions & 4 deletions codegen/generator/webhook_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"path"
"text/template"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/codegen/rewriter"
)

func generateWebhook(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewriter) error {
Expand All @@ -35,7 +35,7 @@ func generateWebhook(c *config.Config, parsedTree *parser.Tree, r *rewriter.Rewr

if len(models) > 0 {
pkgs["context"] = types.NewPackage("context", "context")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
pkgs["api"] = types.NewPackage("github.com/miko/dgraph-lambda-go/api", "api")
}

err = webhookResolverTemplate.Execute(f, struct {
Expand Down
4 changes: 2 additions & 2 deletions codegen/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"go/types"
"regexp"

"github.com/schartey/dgraph-lambda-go/codegen/graphql"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/graphql"
"github.com/miko/dgraph-lambda-go/internal"
"github.com/vektah/gqlparser/v2/ast"
)

Expand Down
6 changes: 3 additions & 3 deletions codegen/rewriter/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"path"
"strings"

"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/internal"
"github.com/miko/dgraph-lambda-go/codegen/config"
"github.com/miko/dgraph-lambda-go/codegen/parser"
"github.com/miko/dgraph-lambda-go/internal"
)

type Rewriter struct {
Expand Down
6 changes: 3 additions & 3 deletions examples/lambda/generated/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"net/http"
"strings"

"github.com/schartey/dgraph-lambda-go/api"
"github.com/schartey/dgraph-lambda-go/examples/lambda/model"
"github.com/schartey/dgraph-lambda-go/examples/lambda/resolvers"
"github.com/miko/dgraph-lambda-go/api"
"github.com/miko/dgraph-lambda-go/examples/lambda/model"
"github.com/miko/dgraph-lambda-go/examples/lambda/resolvers"
)

type Executer struct {
Expand Down
Loading