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