github.com/shish-dev/gqlgen@v0.99.0/codegen/directives.gotpl (about)

     1  {{ define "implDirectives" }}{{ $in := .DirectiveObjName }}
     2  	{{- range $i, $directive := .ImplDirectives -}}
     3  		directive{{add $i 1}} := func(ctx context.Context) (interface{}, error) {
     4  			{{- range $arg := $directive.Args }}
     5  				{{- if notNil "Value" $arg }}
     6  						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Value | dump }})
     7  						if err != nil{
     8  							return nil, err
     9  						}
    10  					{{- else if notNil "Default" $arg }}
    11  						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Default | dump }})
    12  						if err != nil{
    13  							return nil, err
    14  						}
    15  					{{- end }}
    16  			{{- end }}
    17  			if ec.directives.{{$directive.Name|ucFirst}} == nil {
    18  				return nil, errors.New("directive {{$directive.Name}} is not implemented")
    19  			}
    20  			return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs $in $i }})
    21  		}
    22  	{{ end -}}
    23  {{ end }}
    24  
    25  {{define "queryDirectives"}}
    26  	for _, d := range obj.Directives {
    27  		switch d.Name {
    28  		{{- range $directive := . }}
    29  		case "{{$directive.Name}}":
    30  			{{- if $directive.Args }}
    31  				rawArgs := d.ArgumentMap(ec.Variables)
    32  				args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
    33  				if err != nil {
    34  					ec.Error(ctx, err)
    35  					return graphql.Null
    36  				}
    37  			{{- end }}
    38  			n := next
    39  			next = func(ctx context.Context) (interface{}, error) {
    40  				if ec.directives.{{$directive.Name|ucFirst}} == nil {
    41  					return nil, errors.New("directive {{$directive.Name}} is not implemented")
    42  				}
    43  				return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
    44  			}
    45  		{{- end }}
    46  		}
    47  	}
    48  	tmp, err := next(ctx)
    49  	if err != nil {
    50  		ec.Error(ctx, err)
    51  		return graphql.Null
    52  	}
    53  	if data, ok := tmp.(graphql.Marshaler); ok {
    54  		return data
    55  	}
    56  	ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
    57  	return graphql.Null
    58  {{end}}
    59  
    60  {{ if .Directives.LocationDirectives "QUERY" }}
    61  func (ec *executionContext) _queryMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler {
    62  	{{ template "queryDirectives" .Directives.LocationDirectives "QUERY" }}
    63  }
    64  {{ end }}
    65  
    66  {{ if .Directives.LocationDirectives "MUTATION" }}
    67  func (ec *executionContext) _mutationMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler {
    68  	{{ template "queryDirectives" .Directives.LocationDirectives "MUTATION" }}
    69  }
    70  {{ end }}
    71  
    72  {{ if .Directives.LocationDirectives "SUBSCRIPTION" }}
    73  func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func() graphql.Marshaler {
    74  	for _, d := range obj.Directives {
    75  		switch d.Name {
    76  		{{- range $directive := .Directives.LocationDirectives "SUBSCRIPTION" }}
    77  		case "{{$directive.Name}}":
    78  			{{- if $directive.Args }}
    79  				rawArgs := d.ArgumentMap(ec.Variables)
    80  				args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
    81  				if err != nil {
    82  					ec.Error(ctx, err)
    83  					return func() graphql.Marshaler {
    84  						return graphql.Null
    85  					}
    86  				}
    87  			{{- end }}
    88  			n := next
    89  			next = func(ctx context.Context) (interface{}, error) {
    90  				if ec.directives.{{$directive.Name|ucFirst}} == nil {
    91  					return nil, errors.New("directive {{$directive.Name}} is not implemented")
    92  				}
    93  				return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
    94  			}
    95  		{{- end }}
    96  		}
    97  	}
    98  	tmp, err := next(ctx)
    99  	if err != nil {
   100  		ec.Error(ctx, err)
   101  		return func() graphql.Marshaler {
   102  			return graphql.Null
   103  		}
   104  	}
   105  	if data, ok := tmp.(func() graphql.Marshaler); ok {
   106  		return data
   107  	}
   108  	ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
   109  	return func() graphql.Marshaler {
   110  		return graphql.Null
   111  	}
   112  }
   113  {{ end }}
   114  
   115  {{ if .Directives.LocationDirectives "FIELD" }}
   116  	func (ec *executionContext) _fieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) interface{} {
   117  		{{- if .Directives.LocationDirectives "FIELD" }}
   118  		fc := graphql.GetFieldContext(ctx)
   119  		for _, d := range fc.Field.Directives {
   120  			switch d.Name {
   121  			{{- range $directive := .Directives.LocationDirectives "FIELD" }}
   122  			case "{{$directive.Name}}":
   123  				{{- if $directive.Args }}
   124  					rawArgs := d.ArgumentMap(ec.Variables)
   125  					args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
   126  					if err != nil {
   127  						ec.Error(ctx, err)
   128  						return nil
   129  					}
   130  				{{- end }}
   131  				n := next
   132  				next = func(ctx context.Context) (interface{}, error) {
   133  					if ec.directives.{{$directive.Name|ucFirst}} == nil {
   134  						return nil, errors.New("directive {{$directive.Name}} is not implemented")
   135  					}
   136  					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
   137  				}
   138  			{{- end }}
   139  			}
   140  		}
   141  		{{- end }}
   142  		res, err := ec.ResolverMiddleware(ctx, next)
   143  		if err != nil {
   144  			ec.Error(ctx, err)
   145  			return nil
   146  		}
   147  		return res
   148  	}
   149  {{ end }}