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