github.com/pouriasharifi/gqlgen@v0.7.2/codegen/templates/generated.gotpl (about)

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