github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/plugin/schemaconfig/schemaconfig.go (about) 1 package schemaconfig 2 3 import ( 4 "github.com/99designs/gqlgen/codegen/config" 5 "github.com/99designs/gqlgen/plugin" 6 "github.com/vektah/gqlparser/ast" 7 ) 8 9 func New() plugin.Plugin { 10 return &Plugin{} 11 } 12 13 type Plugin struct{} 14 15 var _ plugin.ConfigMutator = &Plugin{} 16 17 func (m *Plugin) Name() string { 18 return "schemaconfig" 19 } 20 21 func (m *Plugin) MutateConfig(cfg *config.Config) error { 22 schema, err := cfg.LoadSchema() 23 if err != nil { 24 return err 25 } 26 27 cfg.Directives["goModel"] = config.DirectiveConfig{ 28 SkipRuntime: true, 29 } 30 31 cfg.Directives["goField"] = config.DirectiveConfig{ 32 SkipRuntime: true, 33 } 34 35 for _, schemaType := range schema.Types { 36 if schemaType == schema.Query || schemaType == schema.Mutation || schemaType == schema.Subscription { 37 continue 38 } 39 40 if bd := schemaType.Directives.ForName("goModel"); bd != nil { 41 if ma := bd.Arguments.ForName("model"); ma != nil { 42 if mv, err := ma.Value.Value(nil); err == nil { 43 cfg.Models.Add(schemaType.Name, mv.(string)) 44 } 45 } 46 if ma := bd.Arguments.ForName("models"); ma != nil { 47 if mvs, err := ma.Value.Value(nil); err == nil { 48 for _, mv := range mvs.([]interface{}) { 49 cfg.Models.Add(schemaType.Name, mv.(string)) 50 } 51 } 52 } 53 } 54 55 if schemaType.Kind == ast.Object || schemaType.Kind == ast.InputObject { 56 for _, field := range schemaType.Fields { 57 if fd := field.Directives.ForName("goField"); fd != nil { 58 forceResolver := cfg.Models[schemaType.Name].Fields[field.Name].Resolver 59 fieldName := cfg.Models[schemaType.Name].Fields[field.Name].FieldName 60 61 if ra := fd.Arguments.ForName("forceResolver"); ra != nil { 62 if fr, err := ra.Value.Value(nil); err == nil { 63 forceResolver = fr.(bool) 64 } 65 } 66 67 if na := fd.Arguments.ForName("name"); na != nil { 68 if fr, err := na.Value.Value(nil); err == nil { 69 fieldName = fr.(string) 70 } 71 } 72 73 if cfg.Models[schemaType.Name].Fields == nil { 74 cfg.Models[schemaType.Name] = config.TypeMapEntry{ 75 Model: cfg.Models[schemaType.Name].Model, 76 Fields: map[string]config.TypeMapField{}, 77 } 78 } 79 80 cfg.Models[schemaType.Name].Fields[field.Name] = config.TypeMapField{ 81 FieldName: fieldName, 82 Resolver: forceResolver, 83 } 84 } 85 } 86 } 87 } 88 return nil 89 }