github.com/shish-dev/gqlgen@v0.99.0/codegen/root_.gotpl (about) 1 {{ reserveImport "context" }} 2 {{ reserveImport "fmt" }} 3 {{ reserveImport "io" }} 4 {{ reserveImport "strconv" }} 5 {{ reserveImport "time" }} 6 {{ reserveImport "sync" }} 7 {{ reserveImport "sync/atomic" }} 8 {{ reserveImport "errors" }} 9 {{ reserveImport "bytes" }} 10 11 {{ reserveImport "github.com/vektah/gqlparser/v2" "gqlparser" }} 12 {{ reserveImport "github.com/vektah/gqlparser/v2/ast" }} 13 {{ reserveImport "github.com/99designs/gqlgen/graphql" }} 14 {{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }} 15 16 // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. 17 func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { 18 return &executableSchema{ 19 resolvers: cfg.Resolvers, 20 directives: cfg.Directives, 21 complexity: cfg.Complexity, 22 } 23 } 24 25 type Config struct { 26 Resolvers ResolverRoot 27 Directives DirectiveRoot 28 Complexity ComplexityRoot 29 } 30 31 type ResolverRoot interface { 32 {{- range $object := .Objects -}} 33 {{ if $object.HasResolvers -}} 34 {{ucFirst $object.Name}}() {{ucFirst $object.Name}}Resolver 35 {{ end }} 36 {{- end }} 37 {{- range $object := .Inputs -}} 38 {{ if $object.HasResolvers -}} 39 {{ucFirst $object.Name}}() {{ucFirst $object.Name}}Resolver 40 {{ end }} 41 {{- end }} 42 } 43 44 type DirectiveRoot struct { 45 {{ range $directive := .Directives }} 46 {{- $directive.Declaration }} 47 {{ end }} 48 } 49 50 type ComplexityRoot struct { 51 {{ range $object := .Objects }} 52 {{ if not $object.IsReserved -}} 53 {{ ucFirst $object.Name }} struct { 54 {{ range $_, $fields := $object.UniqueFields }} 55 {{- $field := index $fields 0 -}} 56 {{ if not $field.IsReserved -}} 57 {{ $field.GoFieldName }} {{ $field.ComplexitySignature }} 58 {{ end }} 59 {{- end }} 60 } 61 {{- end }} 62 {{ end }} 63 } 64 65 type executableSchema struct { 66 resolvers ResolverRoot 67 directives DirectiveRoot 68 complexity ComplexityRoot 69 } 70 71 func (e *executableSchema) Schema() *ast.Schema { 72 return parsedSchema 73 } 74 75 func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) { 76 ec := executionContext{nil, e} 77 _ = ec 78 switch typeName + "." + field { 79 {{ range $object := .Objects }} 80 {{ if not $object.IsReserved }} 81 {{ range $_, $fields := $object.UniqueFields }} 82 {{- $len := len $fields }} 83 {{- range $i, $field := $fields }} 84 {{- $last := eq (add $i 1) $len }} 85 {{- if not $field.IsReserved }} 86 {{- if eq $i 0 }}case {{ end }}"{{$object.Name}}.{{$field.Name}}"{{ if not $last }},{{ else }}: 87 if e.complexity.{{ucFirst $object.Name }}.{{$field.GoFieldName}} == nil { 88 break 89 } 90 {{ if $field.Args }} 91 args, err := ec.{{ $field.ArgsFunc }}(context.TODO(),rawArgs) 92 if err != nil { 93 return 0, false 94 } 95 {{ end }} 96 return e.complexity.{{ucFirst $object.Name}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{ end }}), true 97 {{ end }} 98 {{- end }} 99 {{- end }} 100 {{ end }} 101 {{ end }} 102 {{ end }} 103 } 104 return 0, false 105 } 106 107 func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { 108 rc := graphql.GetOperationContext(ctx) 109 ec := executionContext{rc, e} 110 first := true 111 112 switch rc.Operation.Operation { 113 {{- if .QueryRoot }} case ast.Query: 114 return func(ctx context.Context) *graphql.Response { 115 if !first { return nil } 116 first = false 117 {{ if .Directives.LocationDirectives "QUERY" -}} 118 data := ec._queryMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){ 119 return ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet), nil 120 }) 121 {{- else -}} 122 data := ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet) 123 {{- end }} 124 var buf bytes.Buffer 125 data.MarshalGQL(&buf) 126 127 return &graphql.Response{ 128 Data: buf.Bytes(), 129 } 130 } 131 {{ end }} 132 133 {{- if .MutationRoot }} case ast.Mutation: 134 return func(ctx context.Context) *graphql.Response { 135 if !first { return nil } 136 first = false 137 {{ if .Directives.LocationDirectives "MUTATION" -}} 138 data := ec._mutationMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){ 139 return ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet), nil 140 }) 141 {{- else -}} 142 data := ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet) 143 {{- end }} 144 var buf bytes.Buffer 145 data.MarshalGQL(&buf) 146 147 return &graphql.Response{ 148 Data: buf.Bytes(), 149 } 150 } 151 {{ end }} 152 153 {{- if .SubscriptionRoot }} case ast.Subscription: 154 {{ if .Directives.LocationDirectives "SUBSCRIPTION" -}} 155 next := ec._subscriptionMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){ 156 return ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet),nil 157 }) 158 {{- else -}} 159 next := ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet) 160 {{- end }} 161 162 var buf bytes.Buffer 163 return func(ctx context.Context) *graphql.Response { 164 buf.Reset() 165 data := next() 166 167 if data == nil { 168 return nil 169 } 170 data.MarshalGQL(&buf) 171 172 return &graphql.Response{ 173 Data: buf.Bytes(), 174 } 175 } 176 {{ end }} 177 default: 178 return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation")) 179 } 180 } 181 182 type executionContext struct { 183 *graphql.OperationContext 184 *executableSchema 185 } 186 187 func (ec *executionContext) introspectSchema() (*introspection.Schema, error) { 188 if ec.DisableIntrospection { 189 return nil, errors.New("introspection disabled") 190 } 191 return introspection.WrapSchema(parsedSchema), nil 192 } 193 194 func (ec *executionContext) introspectType(name string) (*introspection.Type, error) { 195 if ec.DisableIntrospection { 196 return nil, errors.New("introspection disabled") 197 } 198 return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil 199 } 200 201 var sources = []*ast.Source{ 202 {{- range $source := .Config.Sources }} 203 {Name: {{$source.Name|quote}}, Input: {{$source.Input|rawQuote}}, BuiltIn: {{$source.BuiltIn}}}, 204 {{- end }} 205 } 206 var parsedSchema = gqlparser.MustLoadSchema(sources...)