diff --git a/api/generate.go b/api/generate.go index 3dd083f52f6..3256bdc3de1 100644 --- a/api/generate.go +++ b/api/generate.go @@ -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" ) @@ -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(), } diff --git a/example/config/.gqlgen.yml b/example/config/.gqlgen.yml index a710b9c7903..ccea6688f39 100644 --- a/example/config/.gqlgen.yml +++ b/example/config/.gqlgen.yml @@ -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 diff --git a/example/config/generated.go b/example/config/generated.go index ccf93600be2..666914062f5 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -235,7 +235,10 @@ func (ec *executionContext) introspectType(name string) (*introspection.Type, er } var parsedSchema = gqlparser.MustLoadSchema( - &ast.Source{Name: "schema.graphql", Input: `type Query { + &ast.Source{Name: "schema.graphql", Input: `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!]! } @@ -244,7 +247,7 @@ type Mutation { } `}, &ast.Source{Name: "todo.graphql", Input: `type Todo { - id: ID! + id: ID! @goField(forceResolver: true) databaseId: Int! text: String! done: Boolean! @@ -257,9 +260,10 @@ input NewTodo { } `}, - &ast.Source{Name: "user.graphql", Input: `type User { + &ast.Source{Name: "user.graphql", Input: `type User +@goModel(model:"github.com/99designs/gqlgen/example/config.User") { id: ID! - name: String! + name: String! @goField(name:"FullName") } `}, ) @@ -2286,6 +2290,38 @@ func (ec *executionContext) marshalOString2string(ctx context.Context, sel ast.S return graphql.MarshalString(v) } +func (ec *executionContext) unmarshalOString2ᚕstring(ctx context.Context, v interface{}) ([]string, error) { + var vSlice []interface{} + if v != nil { + if tmp1, ok := v.([]interface{}); ok { + vSlice = tmp1 + } else { + vSlice = []interface{}{v} + } + } + var err error + res := make([]string, len(vSlice)) + for i := range vSlice { + res[i], err = ec.unmarshalNString2string(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) marshalOString2ᚕstring(ctx context.Context, sel ast.SelectionSet, v []string) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + for i := range v { + ret[i] = ec.marshalNString2string(ctx, sel, v[i]) + } + + return ret +} + func (ec *executionContext) unmarshalOString2ᚖstring(ctx context.Context, v interface{}) (*string, error) { if v == nil { return nil, nil diff --git a/example/config/schema.graphql b/example/config/schema.graphql index 3485fb1c673..4e7b69fb054 100644 --- a/example/config/schema.graphql +++ b/example/config/schema.graphql @@ -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!]! } diff --git a/example/config/todo.graphql b/example/config/todo.graphql index ccee3e44dc7..8e7c8aa0aa1 100644 --- a/example/config/todo.graphql +++ b/example/config/todo.graphql @@ -1,5 +1,5 @@ type Todo { - id: ID! + id: ID! @goField(forceResolver: true) databaseId: Int! text: String! done: Boolean! diff --git a/example/config/user.graphql b/example/config/user.graphql index 321de95b1e7..52c688bd331 100644 --- a/example/config/user.graphql +++ b/example/config/user.graphql @@ -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") } diff --git a/plugin/schemaconfig/schemaconfig.go b/plugin/schemaconfig/schemaconfig.go new file mode 100644 index 00000000000..8068d8f44e5 --- /dev/null +++ b/plugin/schemaconfig/schemaconfig.go @@ -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 +}