github.com/vetcher/gqlgen@v0.6.0/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  			Extensions: ec.Extensions,		}
   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  			Extensions: ec.Extensions,
   152  		}
   153  	{{- else }}
   154  		return graphql.ErrorResponse(ctx, "mutations are not supported")
   155  	{{- end }}
   156  }
   157  
   158  func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {
   159  	{{- if .SubscriptionRoot }}
   160  		ec := executionContext{graphql.GetRequestContext(ctx), e}
   161  
   162  		next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)
   163  		if ec.Errors != nil {
   164  			return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors})
   165  		}
   166  
   167  		var buf bytes.Buffer
   168  		return func() *graphql.Response {
   169  			buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
   170  				buf.Reset()
   171  				data := next()
   172  
   173  				if data == nil {
   174  					return nil
   175  				}
   176  				data.MarshalGQL(&buf)
   177  				return buf.Bytes()
   178  			})
   179  
   180  			if buf == nil {
   181  				return nil
   182  			}
   183  
   184  			return &graphql.Response{
   185  				Data:       buf,
   186  				Errors:     ec.Errors,
   187  				Extensions: ec.Extensions,
   188  			}
   189  		}
   190  	{{- else }}
   191  		return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
   192  	{{- end }}
   193  }
   194  
   195  type executionContext struct {
   196  	*graphql.RequestContext
   197  	*executableSchema
   198  }
   199  
   200  {{- range $object := .Objects }}
   201  	{{ template "object.gotpl" $object }}
   202  
   203  	{{- range $field := $object.Fields }}
   204  		{{ template "field.gotpl" $field }}
   205  	{{ end }}
   206  {{- end}}
   207  
   208  {{- range $interface := .Interfaces }}
   209  	{{ template "interface.gotpl" $interface }}
   210  {{- end }}
   211  
   212  {{- range $input := .Inputs }}
   213  	{{ template "input.gotpl" $input }}
   214  {{- end }}
   215  
   216  func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {
   217  	defer func() {
   218  		if r := recover(); r != nil {
   219  			ec.Error(ctx, ec.Recover(ctx, r))
   220  			ret = nil
   221  		}
   222  	}()
   223  	{{- if .Directives }}
   224  	rctx := graphql.GetResolverContext(ctx)
   225  	for _, d := range rctx.Field.Definition.Directives {
   226  		switch d.Name {
   227  		{{- range $directive := .Directives }}
   228  		case "{{$directive.Name}}":
   229  			if ec.directives.{{$directive.Name|ucFirst}} != nil {
   230  				{{- if $directive.Args }}
   231  					rawArgs := d.ArgumentMap(ec.Variables)
   232  					args, err := {{ $directive.ArgsFunc }}(rawArgs)
   233  					if err != nil {
   234  						ec.Error(ctx, err)
   235  						return nil
   236  					}
   237  				{{- end }}
   238  				n := next
   239  				next = func(ctx context.Context) (interface{}, error) {
   240  					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
   241  				}
   242  			}
   243  		{{- end }}
   244  		}
   245  	}
   246  	{{- end }}
   247  	res, err := ec.ResolverMiddleware(ctx, next)
   248  	if err != nil {
   249  		ec.Error(ctx, err)
   250  		return nil
   251  	}
   252  	return res
   253  }
   254  
   255  func (ec *executionContext) introspectSchema() *introspection.Schema {
   256  	return introspection.WrapSchema(parsedSchema)
   257  }
   258  
   259  func (ec *executionContext) introspectType(name string) *introspection.Type {
   260  	return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])
   261  }
   262  
   263  var parsedSchema = gqlparser.MustLoadSchema(
   264  	&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},
   265  )