github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/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  {{ reserveImport "embed"  }}
    11  
    12  {{ reserveImport "github.com/vektah/gqlparser/v2" "gqlparser" }}
    13  {{ reserveImport "github.com/vektah/gqlparser/v2/ast" }}
    14  {{ reserveImport "github.com/99designs/gqlgen/graphql" }}
    15  {{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}
    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  		{{ucFirst $object.Name}}() {{ucFirst $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  		{{ ucFirst $object.Name }} 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  type executableSchema struct {
    62  	resolvers  ResolverRoot
    63  	directives DirectiveRoot
    64  	complexity ComplexityRoot
    65  }
    66  
    67  func (e *executableSchema) Schema() *ast.Schema {
    68  	return parsedSchema
    69  }
    70  
    71  func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
    72  	ec := executionContext{nil, e}
    73  	_ = ec
    74  	switch typeName + "." + field {
    75  	{{ range $object := .Objects }}
    76  		{{ if not $object.IsReserved }}
    77  			{{ range $_, $fields := $object.UniqueFields }}
    78  				{{- $len := len $fields }}
    79  				{{- range $i, $field := $fields }}
    80  					{{- $last := eq (add $i 1) $len }}
    81  					{{- if not $field.IsReserved }}
    82  						{{- if eq $i 0 }}case {{ end }}"{{$object.Name}}.{{$field.Name}}"{{ if not $last }},{{ else }}:
    83  						if e.complexity.{{ucFirst $object.Name }}.{{$field.GoFieldName}} == nil {
    84  						break
    85  						}
    86  						{{ if $field.Args }}
    87  							args, err := ec.{{ $field.ArgsFunc }}(context.TODO(),rawArgs)
    88  							if err != nil {
    89  							return 0, false
    90  							}
    91  						{{ end }}
    92  						return e.complexity.{{ucFirst $object.Name}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{ end }}), true
    93  						{{ end }}
    94  					{{- end }}
    95  				{{- end }}
    96  			{{ end }}
    97  		{{ end }}
    98  	{{ end }}
    99  	}
   100  	return 0, false
   101  }
   102  
   103  func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
   104  	rc := graphql.GetOperationContext(ctx)
   105  	ec := executionContext{rc, e}
   106  	inputUnmarshalMap := graphql.BuildUnmarshalerMap(
   107  		{{- range $input := .Inputs -}}
   108  			{{ if not $input.HasUnmarshal }}
   109  				ec.unmarshalInput{{ $input.Name }},
   110  			{{- end }}
   111  		{{- end }}
   112  	)
   113  	first := true
   114  
   115  	switch rc.Operation.Operation {
   116  	{{- if .QueryRoot }} case ast.Query:
   117  		return func(ctx context.Context) *graphql.Response {
   118  			if !first { return nil }
   119  			first = false
   120  			ctx = graphql.WithUnmarshalerMap(ctx, inputUnmarshalMap)
   121  			{{ if .Directives.LocationDirectives "QUERY" -}}
   122  				data := ec._queryMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
   123  					return ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet), nil
   124  				})
   125  			{{- else -}}
   126  				data := ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet)
   127  			{{- end }}
   128  			var buf bytes.Buffer
   129  			data.MarshalGQL(&buf)
   130  
   131  			return &graphql.Response{
   132  				Data:       buf.Bytes(),
   133  			}
   134  		}
   135  	{{ end }}
   136  
   137  	{{- if .MutationRoot }} case ast.Mutation:
   138  		return func(ctx context.Context) *graphql.Response {
   139  			if !first { return nil }
   140  			first = false
   141  			ctx = graphql.WithUnmarshalerMap(ctx, inputUnmarshalMap)
   142  			{{ if .Directives.LocationDirectives "MUTATION" -}}
   143  				data := ec._mutationMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
   144  					return ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet), nil
   145  				})
   146  			{{- else -}}
   147  				data := ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet)
   148  			{{- end }}
   149  			var buf bytes.Buffer
   150  			data.MarshalGQL(&buf)
   151  
   152  			return &graphql.Response{
   153  				Data:       buf.Bytes(),
   154  			}
   155  		}
   156  	{{ end }}
   157  
   158  	{{- if .SubscriptionRoot }} case ast.Subscription:
   159  		{{ if .Directives.LocationDirectives "SUBSCRIPTION" -}}
   160  			next := ec._subscriptionMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
   161  				return ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet),nil
   162  			})
   163  		{{- else -}}
   164  			next := ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet)
   165  		{{- end }}
   166  
   167  		var buf bytes.Buffer
   168  		return func(ctx context.Context) *graphql.Response {
   169  			buf.Reset()
   170  			data := next(ctx)
   171  
   172  			if data == nil {
   173  				return nil
   174  			}
   175  			data.MarshalGQL(&buf)
   176  
   177  			return &graphql.Response{
   178  				Data:       buf.Bytes(),
   179  			}
   180  		}
   181  	{{ end }}
   182  	default:
   183  		return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
   184  	}
   185  }
   186  
   187  type executionContext struct {
   188  	*graphql.OperationContext
   189  	*executableSchema
   190  }
   191  
   192  func (ec *executionContext) introspectSchema() (*introspection.Schema, error) {
   193  	if ec.DisableIntrospection {
   194  		return nil, errors.New("introspection disabled")
   195  	}
   196  	return introspection.WrapSchema(parsedSchema), nil
   197  }
   198  
   199  func (ec *executionContext) introspectType(name string) (*introspection.Type, error) {
   200  	if ec.DisableIntrospection {
   201  		return nil, errors.New("introspection disabled")
   202  	}
   203  	return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil
   204  }
   205  
   206  
   207  {{if .HasEmbeddableSources }}
   208  //go:embed{{- range $source := .AugmentedSources }}{{if $source.Embeddable}} {{$source.RelativePath|quote}}{{end}}{{- end }}
   209  var sourcesFS embed.FS
   210  
   211  func sourceData(filename string) string {
   212  	data, err := sourcesFS.ReadFile(filename)
   213  	if err != nil {
   214  		panic(fmt.Sprintf("codegen problem: %s not available", filename))
   215  	}
   216  	return string(data)
   217  }
   218  {{- end}}
   219  
   220  var sources = []*ast.Source{
   221  {{- range $source := .AugmentedSources }}
   222  	{Name: {{$source.RelativePath|quote}}, Input: {{if (not $source.Embeddable)}}{{$source.Source|rawQuote}}{{else}}sourceData({{$source.RelativePath|quote}}){{end}}, BuiltIn: {{$source.BuiltIn}}},
   223  {{- end }}
   224  }
   225  var parsedSchema = gqlparser.MustLoadSchema(sources...)