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