Skip to content

Commit

Permalink
Add plugin for providing config via schema directives
Browse files Browse the repository at this point in the history
  • Loading branch information
lwc committed May 30, 2019
1 parent 17a82c3 commit de75743
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 14 deletions.
2 changes: 2 additions & 0 deletions api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/99designs/gqlgen/plugin"
"github.com/99designs/gqlgen/plugin/modelgen"
"github.com/99designs/gqlgen/plugin/resolvergen"
"github.com/99designs/gqlgen/plugin/schemaconfig"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)
Expand All @@ -17,6 +18,7 @@ func Generate(cfg *config.Config, option ...Option) error {
_ = syscall.Unlink(cfg.Model.Filename)

plugins := []plugin.Plugin{
schemaconfig.New(),
modelgen.New(),
resolvergen.New(),
}
Expand Down
7 changes: 0 additions & 7 deletions example/config/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ resolver:
models:
Todo: # Object
fields:
id:
resolver: true
text:
fieldName: Description # Field
NewTodo: # Input
fields:
userId:
fieldName: UserID # Field
User:
model: github.com/99designs/gqlgen/example/config.User
fields:
name:
fieldName: FullName # Method
44 changes: 40 additions & 4 deletions example/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/config/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
directive @goModel(model: String, models: [String!]) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

type Query {
todos: [Todo!]!
}
Expand Down
2 changes: 1 addition & 1 deletion example/config/todo.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Todo {
id: ID!
id: ID! @goField(forceResolver: true)
databaseId: Int!
text: String!
done: Boolean!
Expand Down
5 changes: 3 additions & 2 deletions example/config/user.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type User {
type User
@goModel(model:"github.com/99designs/gqlgen/example/config.User") {
id: ID!
name: String!
name: String! @goField(name:"FullName")
}
95 changes: 95 additions & 0 deletions plugin/schemaconfig/schemaconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package schemaconfig

import (
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin"
"github.com/vektah/gqlparser/ast"
)

func New() plugin.Plugin {
return &Plugin{}
}

type Plugin struct{}

var _ plugin.ConfigMutator = &Plugin{}

func (m *Plugin) Name() string {
return "schemaconfig"
}

func (m *Plugin) MutateConfig(cfg *config.Config) error {
if err := cfg.Check(); err != nil {
return err
}

schema, _, err := cfg.LoadSchema()
if err != nil {
return err
}

cfg.InjectBuiltins(schema)

cfg.Directives["goModel"] = config.DirectiveConfig{
SkipRuntime: true,
}

cfg.Directives["goField"] = config.DirectiveConfig{
SkipRuntime: true,
}

for _, schemaType := range schema.Types {
if schemaType == schema.Query || schemaType == schema.Mutation || schemaType == schema.Subscription {
continue
}

if bd := schemaType.Directives.ForName("goModel"); bd != nil {
if ma := bd.Arguments.ForName("model"); ma != nil {
if mv, err := ma.Value.Value(nil); err == nil {
cfg.Models.Add(schemaType.Name, mv.(string))
}
}
if ma := bd.Arguments.ForName("models"); ma != nil {
if mvs, err := ma.Value.Value(nil); err == nil {
for _, mv := range mvs.([]interface{}) {
cfg.Models.Add(schemaType.Name, mv.(string))
}
}
}
}

if schemaType.Kind == ast.Object || schemaType.Kind == ast.InputObject {
for _, field := range schemaType.Fields {
if fd := field.Directives.ForName("goField"); fd != nil {
forceResolver := cfg.Models[schemaType.Name].Fields[field.Name].Resolver
fieldName := cfg.Models[schemaType.Name].Fields[field.Name].FieldName

if ra := fd.Arguments.ForName("forceResolver"); ra != nil {
if fr, err := ra.Value.Value(nil); err == nil {
forceResolver = fr.(bool)
}
}

if na := fd.Arguments.ForName("name"); na != nil {
if fr, err := na.Value.Value(nil); err == nil {
fieldName = fr.(string)
}
}

if cfg.Models[schemaType.Name].Fields == nil {
cfg.Models[schemaType.Name] = config.TypeMapEntry{
Model: cfg.Models[schemaType.Name].Model,
Fields: map[string]config.TypeMapField{},
}
}

cfg.Models[schemaType.Name].Fields[field.Name] = config.TypeMapField{
FieldName: fieldName,
Resolver: forceResolver,
}
}
}
}
}
return nil
}

0 comments on commit de75743

Please sign in to comment.