github.com/tobiash/gqlgen@v0.5.1/codegen/templates/generated.gotpl (about)

     1  // Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
     2  
     3  package {{ .PackageName }}
     4  
     5  import (
     6  {{- range $import := .Imports }}
     7  	{{- $import.Write }}
     8  {{ end }}
     9  )
    10  
    11  // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
    12  func NewExecutableSchema(cfg Config) graphql.ExecutableSchema {
    13  	return &executableSchema{
    14  		resolvers: cfg.Resolvers,
    15  		directives: cfg.Directives,
    16  		complexity: cfg.Complexity,
    17  	}
    18  }
    19  
    20  type Config struct {
    21  	Resolvers  ResolverRoot
    22  	Directives DirectiveRoot
    23  	Complexity ComplexityRoot
    24  }
    25  
    26  type ResolverRoot interface {
    27  {{- range $object := .Objects -}}
    28  	{{ if $object.HasResolvers -}}
    29  		{{$object.GQLType}}() {{$object.GQLType}}Resolver
    30  	{{ end }}
    31  {{- end }}
    32  }
    33  
    34  type DirectiveRoot struct {
    35  {{ range $directive := .Directives }}
    36  	{{ $directive.Declaration }}
    37  {{ end }}
    38  }
    39  
    40  type ComplexityRoot struct {
    41  {{ range $object := .Objects }}
    42  	{{ if not $object.IsReserved -}}
    43  		{{ $object.GQLType|toCamel }} struct {
    44  		{{ range $field := $object.Fields -}}
    45  			{{ if not $field.IsReserved -}}
    46  				{{ $field.GQLName|toCamel }} {{ $field.ComplexitySignature }}
    47  			{{ end }}
    48  		{{- end }}
    49  		}
    50  	{{- end }}
    51  {{ end }}
    52  }
    53  
    54  {{ range $object := .Objects -}}
    55  	{{ if $object.HasResolvers }}
    56  		type {{$object.GQLType}}Resolver interface {
    57  		{{ range $field := $object.Fields -}}
    58  			{{ $field.ShortResolverDeclaration }}
    59  		{{ end }}
    60  		}
    61  	{{- end }}
    62  {{- end }}
    63  
    64  {{ range $object := .Objects -}}
    65  	{{ range $field := $object.Fields -}}
    66  		{{ if $field.Args }}
    67  			func {{ $field.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
    68  			{{ template "args.gotpl" $field.Args }}
    69  			}
    70  		{{ end }}
    71  	{{ end }}
    72  {{- end }}
    73  
    74  {{ range $directive := .Directives }}
    75  	{{ if $directive.Args }}
    76  		func {{ $directive.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
    77  		{{ template "args.gotpl" $directive.Args }}
    78  		}
    79  	{{ end }}
    80  {{ end }}
    81  
    82  type executableSchema struct {
    83  	resolvers  ResolverRoot
    84  	directives DirectiveRoot
    85  	complexity ComplexityRoot
    86  }
    87  
    88  func (e *executableSchema) Schema() *ast.Schema {
    89  	return parsedSchema
    90  }
    91  
    92  func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
    93  	switch typeName + "." + field {
    94  	{{ range $object := .Objects }}
    95  		{{ if not $object.IsReserved }}
    96  			{{ range $field := $object.Fields }}
    97  				{{ if not $field.IsReserved }}
    98  					case "{{$object.GQLType}}.{{$field.GQLName}}":
    99  						if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
   100  							break
   101  						}
   102  						{{ if $field.Args }}
   103  							args, err := {{ $field.ArgsFunc }}(rawArgs)
   104  							if err != nil {
   105  								return 0, false
   106  							}
   107  						{{ end }}
   108  						return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
   109  				{{ end }}
   110  			{{ end }}
   111  		{{ end }}
   112  	{{ end }}
   113  	}
   114  	return 0, false
   115  }
   116  
   117  func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
   118  	{{- if .QueryRoot }}
   119  		ec := executionContext{graphql.GetRequestContext(ctx), e}
   120  
   121  		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
   122  			data := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)
   123  			var buf bytes.Buffer
   124  			data.MarshalGQL(&buf)
   125  			return buf.Bytes()
   126  		})
   127  
   128  		return &graphql.Response{
   129  			Data:   buf,
   130  			Errors: ec.Errors,
   131  		}
   132  	{{- else }}
   133  		return graphql.ErrorResponse(ctx, "queries are not supported")
   134  	{{- end }}
   135  }
   136  
   137  func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
   138  	{{- if .MutationRoot }}
   139  		ec := executionContext{graphql.GetRequestContext(ctx), e}
   140  
   141  		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
   142  			data := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)
   143  			var buf bytes.Buffer
   144  			data.MarshalGQL(&buf)
   145  			return buf.Bytes()
   146  		})
   147  
   148  		return &graphql.Response{
   149  			Data:   buf,
   150  			Errors: ec.Errors,
   151  		}
   152  	{{- else }}
   153  		return graphql.ErrorResponse(ctx, "mutations are not supported")
   154  	{{- end }}
   155  }
   156  
   157  func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {
   158  	{{- if .SubscriptionRoot }}
   159  		ec := executionContext{graphql.GetRequestContext(ctx), e}
   160  
   161  		next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)
   162  		if ec.Errors != nil {
   163  			return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors})
   164  		}
   165  
   166  		var buf bytes.Buffer
   167  		return func() *graphql.Response {
   168  			buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
   169  				buf.Reset()
   170  				data := next()
   171  
   172  				if data == nil {
   173  					return nil
   174  				}
   175  				data.MarshalGQL(&buf)
   176  				return buf.Bytes()
   177  			})
   178  
   179  			if buf == nil {
   180  				return nil
   181  			}
   182  
   183  			return &graphql.Response{
   184  				Data:   buf,
   185  				Errors: ec.Errors,
   186  			}
   187  		}
   188  	{{- else }}
   189  		return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
   190  	{{- end }}
   191  }
   192  
   193  type executionContext struct {
   194  	*graphql.RequestContext
   195  	*executableSchema
   196  }
   197  
   198  {{- range $object := .Objects }}
   199  	{{ template "object.gotpl" $object }}
   200  
   201  	{{- range $field := $object.Fields }}
   202  		{{ template "field.gotpl" $field }}
   203  	{{ end }}
   204  {{- end}}
   205  
   206  {{- range $interface := .Interfaces }}
   207  	{{ template "interface.gotpl" $interface }}
   208  {{- end }}
   209  
   210  {{- range $input := .Inputs }}
   211  	{{ template "input.gotpl" $input }}
   212  {{- end }}
   213  
   214  func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {
   215  	defer func() {
   216  		if r := recover(); r != nil {
   217  			ec.Error(ctx, ec.Recover(ctx, r))
   218  			ret = nil
   219  		}
   220  	}()
   221  	{{- if .Directives }}
   222  	rctx := graphql.GetResolverContext(ctx)
   223  	for _, d := range rctx.Field.Definition.Directives {
   224  		switch d.Name {
   225  		{{- range $directive := .Directives }}
   226  		case "{{$directive.Name}}":
   227  			if ec.directives.{{$directive.Name|ucFirst}} != nil {
   228  				{{- if $directive.Args }}
   229  					rawArgs := d.ArgumentMap(ec.Variables)
   230  					args, err := {{ $directive.ArgsFunc }}(rawArgs)
   231  					if err != nil {
   232  						ec.Error(ctx, err)
   233  						return nil
   234  					}
   235  				{{- end }}
   236  				n := next
   237  				next = func(ctx context.Context) (interface{}, error) {
   238  					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
   239  				}
   240  			}
   241  		{{- end }}
   242  		}
   243  	}
   244  	{{- end }}
   245  	res, err := ec.ResolverMiddleware(ctx, next)
   246  	if err != nil {
   247  		ec.Error(ctx, err)
   248  		return nil
   249  	}
   250  	return res
   251  }
   252  
   253  func (ec *executionContext) introspectSchema() *introspection.Schema {
   254  	return introspection.WrapSchema(parsedSchema)
   255  }
   256  
   257  func (ec *executionContext) introspectType(name string) *introspection.Type {
   258  	return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])
   259  }
   260  
   261  var parsedSchema = gqlparser.MustLoadSchema(
   262  	&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},
   263  )