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