From be890ab9a1d887a1993aa3403c4e36c294003187 Mon Sep 17 00:00:00 2001 From: asamusev Date: Tue, 25 Jun 2019 07:18:19 +0300 Subject: [PATCH] use UnmarshalFunc in args directives implement --- codegen/directive.go | 14 ++--- codegen/directives.gotpl | 17 ++++-- codegen/testserver/directive_test.go | 20 ++++--- codegen/testserver/generated.go | 90 +++++++++++++++++++++------- codegen/testserver/schema.graphql | 4 +- 5 files changed, 98 insertions(+), 47 deletions(-) diff --git a/codegen/directive.go b/codegen/directive.go index 5857717c520..0f61d060ca7 100644 --- a/codegen/directive.go +++ b/codegen/directive.go @@ -151,18 +151,12 @@ func (d *Directive) CallArgs() string { return strings.Join(args, ", ") } -func (d *Directive) ResolveArgs(obj string, next string) string { - args := []string{"ctx", obj, next} +func (d *Directive) ResolveArgs(obj string, next int) string { + args := []string{"ctx", obj, fmt.Sprintf("directive%d", next)} for _, arg := range d.Args { - dArg := "&" + arg.VarName - if !arg.TypeReference.IsPtr() { - if arg.Value != nil { - dArg = templates.Dump(arg.Value) - } else { - dArg = templates.Dump(arg.Default) - } - } else if arg.Value == nil && arg.Default == nil { + dArg := arg.VarName + if arg.Value == nil && arg.Default == nil { dArg = "nil" } diff --git a/codegen/directives.gotpl b/codegen/directives.gotpl index 9d8863a7546..72498d1e9c3 100644 --- a/codegen/directives.gotpl +++ b/codegen/directives.gotpl @@ -2,12 +2,19 @@ {{- range $i, $directive := .Directives -}} directive{{add $i 1}} := func(ctx context.Context) (interface{}, error) { {{- range $arg := $directive.Args }} - {{- if and $arg.TypeReference.IsPtr ( notNil "Value" $arg ) }} - {{ $arg.VarName }} := {{ $arg.Value | dump }} - {{- end }} + {{- if notNil "Value" $arg }} + {{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Value | dump }}) + if err != nil{ + return nil, err + } + {{- else if notNil "Default" $arg }} + {{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Default | dump }}) + if err != nil{ + return nil, err + } + {{- end }} {{- end }} - n := directive{{$i}} - return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "in" "n" }}) + return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "in" $i }}) } {{- end }} {{ end }} diff --git a/codegen/testserver/directive_test.go b/codegen/testserver/directive_test.go index 494be75f959..3d6badd0804 100644 --- a/codegen/testserver/directive_test.go +++ b/codegen/testserver/directive_test.go @@ -44,7 +44,13 @@ func TestDirectives(t *testing.T) { NewExecutableSchema(Config{ Resolvers: resolvers, Directives: DirectiveRoot{ - Length: func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int) (interface{}, error) { + Length: func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int, message *string) (interface{}, error) { + e := func(msg string) error { + if message == nil{ + return fmt.Errorf(msg) + } + return fmt.Errorf(*message) + } res, err := next(ctx) if err != nil { return nil, err @@ -52,10 +58,10 @@ func TestDirectives(t *testing.T) { s := res.(string) if len(s) < min { - return nil, fmt.Errorf("too short") + return nil, e("too short") } if max != nil && len(s) > *max { - return nil, fmt.Errorf("too long") + return nil, e("too long") } return res, nil }, @@ -119,7 +125,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveArg(arg: "") }`, &resp) - require.EqualError(t, err, `[{"message":"too short","path":["directiveArg"]}]`) + require.EqualError(t, err, `[{"message":"invalid length","path":["directiveArg"]}]`) require.Nil(t, resp.DirectiveArg) }) t.Run("when function errors on nullable arg directives", func(t *testing.T) { @@ -171,7 +177,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"invalid text",inner:{message:"123"}}) }`, &resp) - require.EqualError(t, err, `[{"message":"too long","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function errors on inner directives", func(t *testing.T) { @@ -181,7 +187,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"2",inner:{message:""}}) }`, &resp) - require.EqualError(t, err, `[{"message":"too short","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function errors on nullable inner directives", func(t *testing.T) { @@ -191,7 +197,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"success",inner:{message:"1"},innerNullable:{message:""}}) }`, &resp) - require.EqualError(t, err, `[{"message":"too short","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function success", func(t *testing.T) { diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 0404a17ec5e..91f1fe4c2b4 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -54,7 +54,7 @@ type ResolverRoot interface { type DirectiveRoot struct { Custom func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) - Length func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int) (res interface{}, err error) + Length func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int, message *string) (res interface{}, err error) Range func(ctx context.Context, obj interface{}, next graphql.Resolver, min *int, max *int) (res interface{}, err error) } @@ -1289,7 +1289,7 @@ func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{} } n := next next = func(ctx context.Context) (interface{}, error) { - return ec.directives.Length(ctx, obj, n, args["min"].(int), args["max"].(*int)) + return ec.directives.Length(ctx, obj, n, args["min"].(int), args["max"].(*int), args["message"].(*string)) } } case "range": @@ -1514,7 +1514,7 @@ input InputDirectives { text: String! @length(min: 0, max: 7, message: "not valid") inner: InnerDirectives! innerNullable: InnerDirectives - thirdParty: ThirdParty @length(min: 0, max: 7, message: "not valid") + thirdParty: ThirdParty @length(min: 0, max: 7) } input InnerDirectives { @@ -1552,7 +1552,7 @@ type EmbeddedPointer { Title: String } -directive @length(min: Int!, max: Int) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION +directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION directive @custom on ARGUMENT_DEFINITION @@ -1724,6 +1724,14 @@ func (ec *executionContext) dir_length_args(ctx context.Context, in map[string]i } } args["max"] = arg1 + var arg2 *string + if tmp, ok := in["message"]; ok { + arg2, err = ec.unmarshalOString2ᚖstring(ctx, tmp) + if err != nil { + return nil, err + } + } + args["message"] = arg2 return args, nil } @@ -1812,9 +1820,19 @@ func (ec *executionContext) field_Query_directiveArg_args(ctx context.Context, i if tmp, ok := in["arg"]; ok { directive0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshalNString2string(ctx, tmp) } directive1 := func(ctx context.Context) (interface{}, error) { - max := 255 - n := directive0 - return ec.directives.Length(ctx, in, n, 1, &max) + min, err := ec.unmarshalNInt2int(ctx, 1) + if err != nil { + return nil, err + } + max, err := ec.unmarshalOInt2ᚖint(ctx, 255) + if err != nil { + return nil, err + } + message, err := ec.unmarshalOString2ᚖstring(ctx, "invalid length") + if err != nil { + return nil, err + } + return ec.directives.Length(ctx, in, directive0, min, max, message) } tmp, err = directive1(ctx) @@ -1854,8 +1872,7 @@ func (ec *executionContext) field_Query_directiveInputType_args(ctx context.Cont return ec.unmarshalNInnerInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx, tmp) } directive1 := func(ctx context.Context) (interface{}, error) { - n := directive0 - return ec.directives.Custom(ctx, in, n) + return ec.directives.Custom(ctx, in, directive0) } tmp, err = directive1(ctx) @@ -1893,9 +1910,11 @@ func (ec *executionContext) field_Query_directiveNullableArg_args(ctx context.Co if tmp, ok := in["arg"]; ok { directive0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshalOInt2ᚖint(ctx, tmp) } directive1 := func(ctx context.Context) (interface{}, error) { - min := 0 - n := directive0 - return ec.directives.Range(ctx, in, n, &min, nil) + min, err := ec.unmarshalOInt2ᚖint(ctx, 0) + if err != nil { + return nil, err + } + return ec.directives.Range(ctx, in, directive0, min, nil) } tmp, err = directive1(ctx) @@ -1913,9 +1932,11 @@ func (ec *executionContext) field_Query_directiveNullableArg_args(ctx context.Co if tmp, ok := in["arg2"]; ok { directive0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshalOInt2ᚖint(ctx, tmp) } directive1 := func(ctx context.Context) (interface{}, error) { - min := 0 - n := directive0 - return ec.directives.Range(ctx, in, n, &min, nil) + min, err := ec.unmarshalOInt2ᚖint(ctx, 0) + if err != nil { + return nil, err + } + return ec.directives.Range(ctx, in, directive0, min, nil) } tmp, err = directive1(ctx) @@ -6146,8 +6167,15 @@ func (ec *executionContext) unmarshalInputInnerDirectives(ctx context.Context, i var err error directive0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshalNString2string(ctx, v) } directive1 := func(ctx context.Context) (interface{}, error) { - n := directive0 - return ec.directives.Length(ctx, in, n, 1, nil) + min, err := ec.unmarshalNInt2int(ctx, 1) + if err != nil { + return nil, err + } + message, err := ec.unmarshalOString2ᚖstring(ctx, "not valid") + if err != nil { + return nil, err + } + return ec.directives.Length(ctx, in, directive0, min, nil, message) } tmp, err := directive1(ctx) @@ -6193,9 +6221,19 @@ func (ec *executionContext) unmarshalInputInputDirectives(ctx context.Context, i var err error directive0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshalNString2string(ctx, v) } directive1 := func(ctx context.Context) (interface{}, error) { - max := 7 - n := directive0 - return ec.directives.Length(ctx, in, n, 0, &max) + min, err := ec.unmarshalNInt2int(ctx, 0) + if err != nil { + return nil, err + } + max, err := ec.unmarshalOInt2ᚖint(ctx, 7) + if err != nil { + return nil, err + } + message, err := ec.unmarshalOString2ᚖstring(ctx, "not valid") + if err != nil { + return nil, err + } + return ec.directives.Length(ctx, in, directive0, min, max, message) } tmp, err := directive1(ctx) @@ -6225,9 +6263,15 @@ func (ec *executionContext) unmarshalInputInputDirectives(ctx context.Context, i return ec.unmarshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx, v) } directive1 := func(ctx context.Context) (interface{}, error) { - max := 7 - n := directive0 - return ec.directives.Length(ctx, in, n, 0, &max) + min, err := ec.unmarshalNInt2int(ctx, 0) + if err != nil { + return nil, err + } + max, err := ec.unmarshalOInt2ᚖint(ctx, 7) + if err != nil { + return nil, err + } + return ec.directives.Length(ctx, in, directive0, min, max, nil) } tmp, err := directive1(ctx) diff --git a/codegen/testserver/schema.graphql b/codegen/testserver/schema.graphql index 0fd5b598651..a230aefa374 100644 --- a/codegen/testserver/schema.graphql +++ b/codegen/testserver/schema.graphql @@ -78,7 +78,7 @@ input InputDirectives { text: String! @length(min: 0, max: 7, message: "not valid") inner: InnerDirectives! innerNullable: InnerDirectives - thirdParty: ThirdParty @length(min: 0, max: 7, message: "not valid") + thirdParty: ThirdParty @length(min: 0, max: 7) } input InnerDirectives { @@ -116,7 +116,7 @@ type EmbeddedPointer { Title: String } -directive @length(min: Int!, max: Int) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION +directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION directive @custom on ARGUMENT_DEFINITION