-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Just4Ease/dev
feat: GraphRPC new updates
- Loading branch information
Showing
33 changed files
with
1,682 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Where are all the schema files located? globs are supported eg src/**/*.graphqls | ||
schema: | ||
- graph/schemas/*.graphql | ||
|
||
# Where should the generated server code go? | ||
exec: | ||
filename: graph/generated.go | ||
package: graph | ||
|
||
# Uncomment to enable federation | ||
# federation: | ||
# filename: graph/generated/federation.go | ||
# package: generated | ||
|
||
# Where should any generated models go? | ||
model: | ||
filename: graph/types.go | ||
package: graph | ||
|
||
# Where should the resolver implementations go? | ||
resolver: | ||
layout: follow-schema | ||
dir: graph | ||
package: graph | ||
|
||
# Optional: turn on use `gqlgen:"fieldName"` tags in your models | ||
# struct_tag: json | ||
|
||
# Optional: turn on to use []Thing instead of []*Thing | ||
# omit_slice_element_pointers: true | ||
|
||
# Optional: set to speed up generation time by not performing a final validation pass. | ||
# skip_validation: true | ||
|
||
# gqlgen will search for any type names in the schema in these go packages | ||
# if they match it will use them, otherwise it will generate them. | ||
autobind: | ||
- "{{.}}/graph" | ||
|
||
# This section declares type mapping between the GraphQL and go type systems | ||
# | ||
# The first line in each type will be used as defaults for resolver arguments and | ||
# modelgen, the others will be allowed when binding to fields. Configure them to | ||
# your liking | ||
models: | ||
Int: | ||
model: | ||
- github.com/99designs/gqlgen/graphql.Int64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/99designs/gqlgen/graphql" | ||
"github.com/Just4Ease/graphrpc/generator" | ||
"github.com/gookit/color" | ||
"github.com/urfave/cli/v2" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
) | ||
|
||
var genCmd = &cli.Command{ | ||
Name: "generate", | ||
Usage: "generate a graphql server based on schema", | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{Name: "verbose, v", Usage: "show logs"}, | ||
&cli.StringFlag{Name: "filename, f", Usage: "the server filename you want code to be generated into"}, | ||
}, | ||
Action: func(ctx *cli.Context) error { | ||
fileName := "server.go" | ||
|
||
if serverFilename := ctx.String("filename"); serverFilename != "" { | ||
fileName = serverFilename | ||
} else { | ||
color.Yellow.Print("⚡️ Server filename not provided, defaulting to server.go \n") | ||
} | ||
|
||
generator.GenerateGraphRPCServer(fileName) | ||
return nil | ||
}, | ||
} | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "graphrpcgen" | ||
app.Usage = genCmd.Usage | ||
app.Description = ` | ||
This is a library for quickly creating strictly typed graphql servers in golang. | ||
See https://gqlgen.com/ for a getting started guide. | ||
Note, this library will scaffold a GraphRPC Server for you. | ||
See https://github.com/Just4Ease/graphrpc 🦾 🐙 🐉 🔦 🕸 | ||
` | ||
app.HideVersion = true | ||
app.Flags = genCmd.Flags | ||
app.Version = graphql.Version | ||
app.Before = func(context *cli.Context) error { | ||
if context.Bool("verbose") { | ||
log.SetFlags(0) | ||
} else { | ||
log.SetOutput(ioutil.Discard) | ||
} | ||
return nil | ||
} | ||
|
||
app.Action = genCmd.Action | ||
app.Commands = []*cli.Command{genCmd} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
fmt.Fprint(os.Stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
package generator | ||
|
||
// TODO: implement server generator | ||
import ( | ||
"fmt" | ||
"github.com/99designs/gqlgen/api" | ||
genCfg "github.com/99designs/gqlgen/codegen/config" | ||
"github.com/Just4Ease/graphrpc/generator/servergen" | ||
"github.com/Just4Ease/graphrpc/internal/code" | ||
"github.com/gookit/color" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v2" | ||
"os" | ||
) | ||
|
||
func GenerateGraphRPCServer(fileName string) { | ||
|
||
pkgName := code.ImportPathForDir(".") | ||
if pkgName == "" { | ||
_, _ = fmt.Fprintln(os.Stderr, "unable to determine import path for current directory, you probably need to run go mod init first\"") | ||
os.Exit(4) | ||
return | ||
} | ||
|
||
configByte, err := servergen.InitConfig(pkgName) | ||
if err != nil { | ||
return | ||
} | ||
|
||
cfg := genCfg.DefaultConfig() | ||
|
||
if err := yaml.UnmarshalStrict(configByte, cfg); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, errors.Wrap(err, "unable to parse config").Error()) | ||
os.Exit(4) | ||
return | ||
} | ||
|
||
if err := genCfg.CompleteConfig(cfg); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(4) | ||
return | ||
} | ||
|
||
if err := servergen.PrepareSchema("graph/schemas/"); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(4) | ||
return | ||
} | ||
|
||
_, _ = fmt.Fprint(os.Stdout, color.Green.Sprint("✅ Successfully generated resolvers.\n")) | ||
|
||
if err := api.Generate(cfg, api.AddPlugin(servergen.New(fileName, pkgName))); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(4) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package servergen | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
var configTemplate = template.Must(template.New("gqlgen.yml").Parse(` | ||
# Where are all the schema files located? globs are supported eg src/**/*.graphqls | ||
schema: | ||
- graph/schemas/*.graphql | ||
# Where should the generated server code go? | ||
exec: | ||
filename: graph/generated.go | ||
package: graph | ||
# Uncomment to enable federation | ||
# federation: | ||
# filename: graph/generated/federation.go | ||
# package: generated | ||
# Where should any generated models go? | ||
model: | ||
filename: graph/types.go | ||
package: graph | ||
# Where should the resolver implementations go? | ||
resolver: | ||
layout: follow-schema | ||
dir: graph | ||
package: graph | ||
# Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models | ||
# struct_tag: json | ||
# Optional: turn on to use []Thing instead of []*Thing | ||
# omit_slice_element_pointers: true | ||
# Optional: set to speed up generation time by not performing a final validation pass. | ||
# skip_validation: true | ||
# gqlgen will search for any type names in the schema in these go packages | ||
# if they match it will use them, otherwise it will generate them. | ||
autobind: | ||
- "{{.}}/graph" | ||
# This section declares type mapping between the GraphQL and go type systems | ||
# | ||
# The first line in each type will be used as defaults for resolver arguments and | ||
# modelgen, the others will be allowed when binding to fields. Configure them to | ||
# your liking | ||
models: | ||
Int: | ||
model: | ||
- github.com/99designs/gqlgen/graphql.Int64 | ||
`)) | ||
|
||
func InitConfig(pkgName string) ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := configTemplate.Execute(&buf, pkgName); err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
Oops, something went wrong.