From ef3830b5e951d30dd49c43183dc029c79c338645 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2019 10:22:12 +1000 Subject: [PATCH] fix field schema directives applied to roots --- codegen/field.go | 3 + codegen/testserver/directive.graphql | 25 ++ codegen/testserver/directive_test.go | 25 ++ codegen/testserver/generated.go | 351 ++++++++++++++++++--------- codegen/testserver/resolver.go | 25 +- codegen/testserver/schema.graphql | 21 -- codegen/testserver/stub.go | 44 ++-- 7 files changed, 321 insertions(+), 173 deletions(-) create mode 100644 codegen/testserver/directive.graphql diff --git a/codegen/field.go b/codegen/field.go index e51c11e3091..264b59ce9a7 100644 --- a/codegen/field.go +++ b/codegen/field.go @@ -288,6 +288,9 @@ func (f *Field) HasDirectives() bool { } func (f *Field) DirectiveObjName() string { + if f.Object.Root { + return "nil" + } return f.GoReceiverName } diff --git a/codegen/testserver/directive.graphql b/codegen/testserver/directive.graphql new file mode 100644 index 00000000000..b2cc8501a6d --- /dev/null +++ b/codegen/testserver/directive.graphql @@ -0,0 +1,25 @@ +directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINITION +directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION +directive @custom on ARGUMENT_DEFINITION +directive @logged(id: UUID!) on FIELD + +extend type Query { + directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String + directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String + directiveInputNullable(arg: InputDirectives): String + directiveInput(arg: InputDirectives!): String + directiveInputType(arg: InnerInput! @custom): String + directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid") + directiveField: String +} + +input InputDirectives { + text: String! @length(min: 0, max: 7, message: "not valid") + inner: InnerDirectives! + innerNullable: InnerDirectives + thirdParty: ThirdParty @length(min: 0, max: 7) +} + +input InnerDirectives { + message: String! @length(min: 1, message: "not valid") +} diff --git a/codegen/testserver/directive_test.go b/codegen/testserver/directive_test.go index b3e73adf31f..9867b12705f 100644 --- a/codegen/testserver/directive_test.go +++ b/codegen/testserver/directive_test.go @@ -180,6 +180,31 @@ func TestDirectives(t *testing.T) { require.Equal(t, "Ok", *resp.DirectiveArg) }) }) + t.Run("field definition directives", func(t *testing.T) { + resolvers.QueryResolver.DirectiveFieldDef = func(ctx context.Context, ret string) (i string, e error) { + return ret, nil + } + + t.Run("too short", func(t *testing.T) { + var resp struct { + DirectiveFieldDef string + } + + err := c.Post(`query { directiveFieldDef(ret: "") }`, &resp) + + require.EqualError(t, err, `[{"message":"not valid","path":["directiveFieldDef"]}]`) + }) + + t.Run("ok", func(t *testing.T) { + var resp struct { + DirectiveFieldDef string + } + + c.MustPost(`query { directiveFieldDef(ret: "aaa") }`, &resp) + + require.Equal(t, "aaa", resp.DirectiveFieldDef) + }) + }) t.Run("field directives", func(t *testing.T) { t.Run("add field directive", func(t *testing.T) { var resp struct { diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index aac80a6b14e..97a5e10d0f5 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -196,6 +196,7 @@ type ComplexityRoot struct { DeprecatedField func(childComplexity int) int DirectiveArg func(childComplexity int, arg string) int DirectiveField func(childComplexity int) int + DirectiveFieldDef func(childComplexity int, ret string) int DirectiveInput func(childComplexity int, arg InputDirectives) int DirectiveInputNullable func(childComplexity int, arg *InputDirectives) int DirectiveInputType func(childComplexity int, arg InnerInput) int @@ -321,17 +322,18 @@ type QueryResolver interface { ModelMethods(ctx context.Context) (*ModelMethods, error) User(ctx context.Context, id int) (*User, error) NullableArg(ctx context.Context, arg *int) (*string, error) + InputSlice(ctx context.Context, arg []string) (bool, error) + ShapeUnion(ctx context.Context) (ShapeUnion, error) + Autobind(ctx context.Context) (*Autobind, error) + DeprecatedField(ctx context.Context) (string, error) + Overlapping(ctx context.Context) (*OverlappingFields, error) DirectiveArg(ctx context.Context, arg string) (*string, error) DirectiveNullableArg(ctx context.Context, arg *int, arg2 *int) (*string, error) DirectiveInputNullable(ctx context.Context, arg *InputDirectives) (*string, error) DirectiveInput(ctx context.Context, arg InputDirectives) (*string, error) DirectiveInputType(ctx context.Context, arg InnerInput) (*string, error) + DirectiveFieldDef(ctx context.Context, ret string) (string, error) DirectiveField(ctx context.Context) (*string, error) - InputSlice(ctx context.Context, arg []string) (bool, error) - ShapeUnion(ctx context.Context) (ShapeUnion, error) - Autobind(ctx context.Context) (*Autobind, error) - DeprecatedField(ctx context.Context) (string, error) - Overlapping(ctx context.Context) (*OverlappingFields, error) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) ErrorBubble(ctx context.Context) (*Error, error) Errors(ctx context.Context) (*Errors, error) @@ -776,6 +778,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.DirectiveField(childComplexity), true + case "Query.directiveFieldDef": + if e.complexity.Query.DirectiveFieldDef == nil { + break + } + + args, err := ec.field_Query_directiveFieldDef_args(context.TODO(), rawArgs) + if err != nil { + return 0, false + } + + return e.complexity.Query.DirectiveFieldDef(childComplexity, args["ret"].(string)), true + case "Query.directiveInput": if e.complexity.Query.DirectiveInput == nil { break @@ -1308,6 +1322,32 @@ type OverlappingFields { newFoo: Int! new_foo: Int! } +`}, + &ast.Source{Name: "directive.graphql", Input: `directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINITION +directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION +directive @custom on ARGUMENT_DEFINITION +directive @logged(id: UUID!) on FIELD + +extend type Query { + directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String + directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String + directiveInputNullable(arg: InputDirectives): String + directiveInput(arg: InputDirectives!): String + directiveInputType(arg: InnerInput! @custom): String + directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid") + directiveField: String +} + +input InputDirectives { + text: String! @length(min: 0, max: 7, message: "not valid") + inner: InnerDirectives! + innerNullable: InnerDirectives + thirdParty: ThirdParty @length(min: 0, max: 7) +} + +input InnerDirectives { + message: String! @length(min: 1, message: "not valid") +} `}, &ast.Source{Name: "loops.graphql", Input: `type LoopA { b: LoopB! @@ -1403,12 +1443,6 @@ type EmbeddedDefaultScalar { modelMethods: ModelMethods user(id: Int!): User! nullableArg(arg: Int = 123): String - directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String - directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String - directiveInputNullable(arg: InputDirectives): String - directiveInput(arg: InputDirectives!): String - directiveInputType(arg: InnerInput! @custom): String - directiveField: String inputSlice(arg: [String!]!): Boolean! shapeUnion: ShapeUnion! autobind: Autobind @@ -1469,17 +1503,6 @@ input OuterInput { scalar ThirdParty -input InputDirectives { - text: String! @length(min: 0, max: 7, message: "not valid") - inner: InnerDirectives! - innerNullable: InnerDirectives - thirdParty: ThirdParty @length(min: 0, max: 7) -} - -input InnerDirectives { - message: String! @length(min: 1, message: "not valid") -} - type OuterObject { inner: InnerObject! } @@ -1511,10 +1534,6 @@ type EmbeddedPointer { Title: String } -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 -directive @logged(id: UUID!) on FIELD scalar UUID enum Status { @@ -1823,6 +1842,20 @@ func (ec *executionContext) field_Query_directiveArg_args(ctx context.Context, r return args, nil } +func (ec *executionContext) field_Query_directiveFieldDef_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { + var err error + args := map[string]interface{}{} + var arg0 string + if tmp, ok := rawArgs["ret"]; ok { + arg0, err = ec.unmarshalNString2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["ret"] = arg0 + return args, nil +} + func (ec *executionContext) field_Query_directiveInputNullable_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -4406,7 +4439,7 @@ func (ec *executionContext) _Query_nullableArg(ctx context.Context, field graphq return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveArg(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_inputSlice(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4423,7 +4456,7 @@ func (ec *executionContext) _Query_directiveArg(ctx context.Context, field graph } ctx = graphql.WithResolverContext(ctx, rctx) rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_directiveArg_args(ctx, rawArgs) + args, err := ec.field_Query_inputSlice_args(ctx, rawArgs) if err != nil { ec.Error(ctx, err) return graphql.Null @@ -4432,19 +4465,22 @@ func (ec *executionContext) _Query_directiveArg(ctx context.Context, field graph ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveArg(rctx, args["arg"].(string)) + return ec.resolvers.Query().InputSlice(rctx, args["arg"].([]string)) }) if resTmp == nil { + if !ec.HasError(rctx) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(*string) + res := resTmp.(bool) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOString2ᚖstring(ctx, field.Selections, res) + return ec.marshalNBoolean2bool(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveNullableArg(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_shapeUnion(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4460,29 +4496,25 @@ func (ec *executionContext) _Query_directiveNullableArg(ctx context.Context, fie IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) - rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_directiveNullableArg_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveNullableArg(rctx, args["arg"].(*int), args["arg2"].(*int)) + return ec.resolvers.Query().ShapeUnion(rctx) }) if resTmp == nil { + if !ec.HasError(rctx) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(*string) + res := resTmp.(ShapeUnion) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOString2ᚖstring(ctx, field.Selections, res) + return ec.marshalNShapeUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShapeUnion(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveInputNullable(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_autobind(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4498,29 +4530,22 @@ func (ec *executionContext) _Query_directiveInputNullable(ctx context.Context, f IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) - rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_directiveInputNullable_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveInputNullable(rctx, args["arg"].(*InputDirectives)) + return ec.resolvers.Query().Autobind(rctx) }) if resTmp == nil { return graphql.Null } - res := resTmp.(*string) + res := resTmp.(*Autobind) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOString2ᚖstring(ctx, field.Selections, res) + return ec.marshalOAutobind2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐAutobind(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveInput(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_deprecatedField(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4536,29 +4561,56 @@ func (ec *executionContext) _Query_directiveInput(ctx context.Context, field gra IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) - rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_directiveInput_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) + ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) + resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().DeprecatedField(rctx) + }) + + if resTmp == nil { + if !ec.HasError(rctx) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - rctx.Args = args + res := resTmp.(string) + rctx.Result = res + ctx = ec.Tracer.StartFieldChildExecution(ctx) + return ec.marshalNString2string(ctx, field.Selections, res) +} + +func (ec *executionContext) _Query_overlapping(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + ctx = ec.Tracer.StartFieldExecution(ctx, field) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + ec.Tracer.EndFieldExecution(ctx) + }() + rctx := &graphql.ResolverContext{ + Object: "Query", + Field: field, + Args: nil, + IsMethod: true, + } + ctx = graphql.WithResolverContext(ctx, rctx) ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveInput(rctx, args["arg"].(InputDirectives)) + return ec.resolvers.Query().Overlapping(rctx) }) if resTmp == nil { return graphql.Null } - res := resTmp.(*string) + res := resTmp.(*OverlappingFields) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOString2ᚖstring(ctx, field.Selections, res) + return ec.marshalOOverlappingFields2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOverlappingFields(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveInputType(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveArg(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4575,7 +4627,7 @@ func (ec *executionContext) _Query_directiveInputType(ctx context.Context, field } ctx = graphql.WithResolverContext(ctx, rctx) rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_directiveInputType_args(ctx, rawArgs) + args, err := ec.field_Query_directiveArg_args(ctx, rawArgs) if err != nil { ec.Error(ctx, err) return graphql.Null @@ -4584,7 +4636,7 @@ func (ec *executionContext) _Query_directiveInputType(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveInputType(rctx, args["arg"].(InnerInput)) + return ec.resolvers.Query().DirectiveArg(rctx, args["arg"].(string)) }) if resTmp == nil { @@ -4596,7 +4648,7 @@ func (ec *executionContext) _Query_directiveInputType(ctx context.Context, field return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_directiveField(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveNullableArg(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4612,10 +4664,17 @@ func (ec *executionContext) _Query_directiveField(ctx context.Context, field gra IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) + rawArgs := field.ArgumentMap(ec.Variables) + args, err := ec.field_Query_directiveNullableArg_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DirectiveField(rctx) + return ec.resolvers.Query().DirectiveNullableArg(rctx, args["arg"].(*int), args["arg2"].(*int)) }) if resTmp == nil { @@ -4627,7 +4686,7 @@ func (ec *executionContext) _Query_directiveField(ctx context.Context, field gra return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_inputSlice(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveInputNullable(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4644,7 +4703,7 @@ func (ec *executionContext) _Query_inputSlice(ctx context.Context, field graphql } ctx = graphql.WithResolverContext(ctx, rctx) rawArgs := field.ArgumentMap(ec.Variables) - args, err := ec.field_Query_inputSlice_args(ctx, rawArgs) + args, err := ec.field_Query_directiveInputNullable_args(ctx, rawArgs) if err != nil { ec.Error(ctx, err) return graphql.Null @@ -4653,22 +4712,19 @@ func (ec *executionContext) _Query_inputSlice(ctx context.Context, field graphql ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().InputSlice(rctx, args["arg"].([]string)) + return ec.resolvers.Query().DirectiveInputNullable(rctx, args["arg"].(*InputDirectives)) }) if resTmp == nil { - if !ec.HasError(rctx) { - ec.Errorf(ctx, "must not be null") - } return graphql.Null } - res := resTmp.(bool) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalNBoolean2bool(ctx, field.Selections, res) + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_shapeUnion(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveInput(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4684,25 +4740,29 @@ func (ec *executionContext) _Query_shapeUnion(ctx context.Context, field graphql IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) + rawArgs := field.ArgumentMap(ec.Variables) + args, err := ec.field_Query_directiveInput_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().ShapeUnion(rctx) + return ec.resolvers.Query().DirectiveInput(rctx, args["arg"].(InputDirectives)) }) if resTmp == nil { - if !ec.HasError(rctx) { - ec.Errorf(ctx, "must not be null") - } return graphql.Null } - res := resTmp.(ShapeUnion) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalNShapeUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShapeUnion(ctx, field.Selections, res) + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_autobind(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveInputType(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4718,22 +4778,29 @@ func (ec *executionContext) _Query_autobind(ctx context.Context, field graphql.C IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) + rawArgs := field.ArgumentMap(ec.Variables) + args, err := ec.field_Query_directiveInputType_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().Autobind(rctx) + return ec.resolvers.Query().DirectiveInputType(rctx, args["arg"].(InnerInput)) }) if resTmp == nil { return graphql.Null } - res := resTmp.(*Autobind) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOAutobind2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐAutobind(ctx, field.Selections, res) + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } -func (ec *executionContext) _Query_deprecatedField(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveFieldDef(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4749,10 +4816,38 @@ func (ec *executionContext) _Query_deprecatedField(ctx context.Context, field gr IsMethod: true, } ctx = graphql.WithResolverContext(ctx, rctx) + rawArgs := field.ArgumentMap(ec.Variables) + args, err := ec.field_Query_directiveFieldDef_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + rctx.Args = args ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().DeprecatedField(rctx) + directive0 := func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().DirectiveFieldDef(rctx, args["ret"].(string)) + } + directive1 := func(ctx context.Context) (interface{}, error) { + 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, nil, directive0, min, nil, message) + } + tmp, err := directive1(rctx) + if err != nil { + return nil, err + } + if data, ok := tmp.(string); ok { + return data, nil + } + return nil, fmt.Errorf(`unexpected type %T from directive, should be string`, tmp) }) if resTmp == nil { @@ -4767,7 +4862,7 @@ func (ec *executionContext) _Query_deprecatedField(ctx context.Context, field gr return ec.marshalNString2string(ctx, field.Selections, res) } -func (ec *executionContext) _Query_overlapping(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { +func (ec *executionContext) _Query_directiveField(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { if r := recover(); r != nil { @@ -4786,16 +4881,16 @@ func (ec *executionContext) _Query_overlapping(ctx context.Context, field graphq ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().Overlapping(rctx) + return ec.resolvers.Query().DirectiveField(rctx) }) if resTmp == nil { return graphql.Null } - res := resTmp.(*OverlappingFields) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOOverlappingFields2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOverlappingFields(ctx, field.Selections, res) + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } func (ec *executionContext) _Query_mapStringInterface(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { @@ -8622,7 +8717,7 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr res = ec._Query_nullableArg(ctx, field) return res }) - case "directiveArg": + case "inputSlice": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8630,10 +8725,13 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveArg(ctx, field) + res = ec._Query_inputSlice(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } return res }) - case "directiveNullableArg": + case "shapeUnion": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8641,10 +8739,13 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveNullableArg(ctx, field) + res = ec._Query_shapeUnion(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } return res }) - case "directiveInputNullable": + case "autobind": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8652,10 +8753,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveInputNullable(ctx, field) + res = ec._Query_autobind(ctx, field) return res }) - case "directiveInput": + case "deprecatedField": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8663,10 +8764,13 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveInput(ctx, field) + res = ec._Query_deprecatedField(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } return res }) - case "directiveInputType": + case "overlapping": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8674,10 +8778,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveInputType(ctx, field) + res = ec._Query_overlapping(ctx, field) return res }) - case "directiveField": + case "directiveArg": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8685,10 +8789,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_directiveField(ctx, field) + res = ec._Query_directiveArg(ctx, field) return res }) - case "inputSlice": + case "directiveNullableArg": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8696,13 +8800,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_inputSlice(ctx, field) - if res == graphql.Null { - atomic.AddUint32(&invalids, 1) - } + res = ec._Query_directiveNullableArg(ctx, field) return res }) - case "shapeUnion": + case "directiveInputNullable": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8710,13 +8811,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_shapeUnion(ctx, field) - if res == graphql.Null { - atomic.AddUint32(&invalids, 1) - } + res = ec._Query_directiveInputNullable(ctx, field) return res }) - case "autobind": + case "directiveInput": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8724,10 +8822,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_autobind(ctx, field) + res = ec._Query_directiveInput(ctx, field) return res }) - case "deprecatedField": + case "directiveInputType": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8735,13 +8833,24 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_deprecatedField(ctx, field) + res = ec._Query_directiveInputType(ctx, field) + return res + }) + case "directiveFieldDef": + field := field + out.Concurrently(i, func() (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_directiveFieldDef(ctx, field) if res == graphql.Null { atomic.AddUint32(&invalids, 1) } return res }) - case "overlapping": + case "directiveField": field := field out.Concurrently(i, func() (res graphql.Marshaler) { defer func() { @@ -8749,7 +8858,7 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr ec.Error(ctx, ec.Recover(ctx, r)) } }() - res = ec._Query_overlapping(ctx, field) + res = ec._Query_directiveField(ctx, field) return res }) case "mapStringInterface": diff --git a/codegen/testserver/resolver.go b/codegen/testserver/resolver.go index ab8f7ec8bda..0af7087630d 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -134,37 +134,40 @@ func (r *queryResolver) User(ctx context.Context, id int) (*User, error) { func (r *queryResolver) NullableArg(ctx context.Context, arg *int) (*string, error) { panic("not implemented") } -func (r *queryResolver) DirectiveArg(ctx context.Context, arg string) (*string, error) { +func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, error) { panic("not implemented") } -func (r *queryResolver) DirectiveNullableArg(ctx context.Context, arg *int, arg2 *int) (*string, error) { +func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) { panic("not implemented") } -func (r *queryResolver) DirectiveInputNullable(ctx context.Context, arg *InputDirectives) (*string, error) { +func (r *queryResolver) Autobind(ctx context.Context) (*Autobind, error) { panic("not implemented") } -func (r *queryResolver) DirectiveInput(ctx context.Context, arg InputDirectives) (*string, error) { +func (r *queryResolver) DeprecatedField(ctx context.Context) (string, error) { panic("not implemented") } -func (r *queryResolver) DirectiveInputType(ctx context.Context, arg InnerInput) (*string, error) { +func (r *queryResolver) Overlapping(ctx context.Context) (*OverlappingFields, error) { panic("not implemented") } -func (r *queryResolver) DirectiveField(ctx context.Context) (*string, error) { +func (r *queryResolver) DirectiveArg(ctx context.Context, arg string) (*string, error) { panic("not implemented") } -func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, error) { +func (r *queryResolver) DirectiveNullableArg(ctx context.Context, arg *int, arg2 *int) (*string, error) { panic("not implemented") } -func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) { +func (r *queryResolver) DirectiveInputNullable(ctx context.Context, arg *InputDirectives) (*string, error) { panic("not implemented") } -func (r *queryResolver) Autobind(ctx context.Context) (*Autobind, error) { +func (r *queryResolver) DirectiveInput(ctx context.Context, arg InputDirectives) (*string, error) { panic("not implemented") } -func (r *queryResolver) DeprecatedField(ctx context.Context) (string, error) { +func (r *queryResolver) DirectiveInputType(ctx context.Context, arg InnerInput) (*string, error) { panic("not implemented") } -func (r *queryResolver) Overlapping(ctx context.Context) (*OverlappingFields, error) { +func (r *queryResolver) DirectiveFieldDef(ctx context.Context, ret string) (string, error) { + panic("not implemented") +} +func (r *queryResolver) DirectiveField(ctx context.Context) (*string, error) { panic("not implemented") } func (r *queryResolver) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) { diff --git a/codegen/testserver/schema.graphql b/codegen/testserver/schema.graphql index e75b3bb2dc4..32fd3288914 100644 --- a/codegen/testserver/schema.graphql +++ b/codegen/testserver/schema.graphql @@ -9,12 +9,6 @@ type Query { modelMethods: ModelMethods user(id: Int!): User! nullableArg(arg: Int = 123): String - directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String - directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String - directiveInputNullable(arg: InputDirectives): String - directiveInput(arg: InputDirectives!): String - directiveInputType(arg: InnerInput! @custom): String - directiveField: String inputSlice(arg: [String!]!): Boolean! shapeUnion: ShapeUnion! autobind: Autobind @@ -75,17 +69,6 @@ input OuterInput { scalar ThirdParty -input InputDirectives { - text: String! @length(min: 0, max: 7, message: "not valid") - inner: InnerDirectives! - innerNullable: InnerDirectives - thirdParty: ThirdParty @length(min: 0, max: 7) -} - -input InnerDirectives { - message: String! @length(min: 1, message: "not valid") -} - type OuterObject { inner: InnerObject! } @@ -117,10 +100,6 @@ type EmbeddedPointer { Title: String } -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 -directive @logged(id: UUID!) on FIELD scalar UUID enum Status { diff --git a/codegen/testserver/stub.go b/codegen/testserver/stub.go index 7b801e2cbc4..4c79fed9eb6 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -48,17 +48,18 @@ type Stub struct { ModelMethods func(ctx context.Context) (*ModelMethods, error) User func(ctx context.Context, id int) (*User, error) NullableArg func(ctx context.Context, arg *int) (*string, error) + InputSlice func(ctx context.Context, arg []string) (bool, error) + ShapeUnion func(ctx context.Context) (ShapeUnion, error) + Autobind func(ctx context.Context) (*Autobind, error) + DeprecatedField func(ctx context.Context) (string, error) + Overlapping func(ctx context.Context) (*OverlappingFields, error) DirectiveArg func(ctx context.Context, arg string) (*string, error) DirectiveNullableArg func(ctx context.Context, arg *int, arg2 *int) (*string, error) DirectiveInputNullable func(ctx context.Context, arg *InputDirectives) (*string, error) DirectiveInput func(ctx context.Context, arg InputDirectives) (*string, error) DirectiveInputType func(ctx context.Context, arg InnerInput) (*string, error) + DirectiveFieldDef func(ctx context.Context, ret string) (string, error) DirectiveField func(ctx context.Context) (*string, error) - InputSlice func(ctx context.Context, arg []string) (bool, error) - ShapeUnion func(ctx context.Context) (ShapeUnion, error) - Autobind func(ctx context.Context) (*Autobind, error) - DeprecatedField func(ctx context.Context) (string, error) - Overlapping func(ctx context.Context) (*OverlappingFields, error) MapStringInterface func(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) ErrorBubble func(ctx context.Context) (*Error, error) Errors func(ctx context.Context) (*Errors, error) @@ -207,6 +208,21 @@ func (r *stubQuery) User(ctx context.Context, id int) (*User, error) { func (r *stubQuery) NullableArg(ctx context.Context, arg *int) (*string, error) { return r.QueryResolver.NullableArg(ctx, arg) } +func (r *stubQuery) InputSlice(ctx context.Context, arg []string) (bool, error) { + return r.QueryResolver.InputSlice(ctx, arg) +} +func (r *stubQuery) ShapeUnion(ctx context.Context) (ShapeUnion, error) { + return r.QueryResolver.ShapeUnion(ctx) +} +func (r *stubQuery) Autobind(ctx context.Context) (*Autobind, error) { + return r.QueryResolver.Autobind(ctx) +} +func (r *stubQuery) DeprecatedField(ctx context.Context) (string, error) { + return r.QueryResolver.DeprecatedField(ctx) +} +func (r *stubQuery) Overlapping(ctx context.Context) (*OverlappingFields, error) { + return r.QueryResolver.Overlapping(ctx) +} func (r *stubQuery) DirectiveArg(ctx context.Context, arg string) (*string, error) { return r.QueryResolver.DirectiveArg(ctx, arg) } @@ -222,24 +238,12 @@ func (r *stubQuery) DirectiveInput(ctx context.Context, arg InputDirectives) (*s func (r *stubQuery) DirectiveInputType(ctx context.Context, arg InnerInput) (*string, error) { return r.QueryResolver.DirectiveInputType(ctx, arg) } +func (r *stubQuery) DirectiveFieldDef(ctx context.Context, ret string) (string, error) { + return r.QueryResolver.DirectiveFieldDef(ctx, ret) +} func (r *stubQuery) DirectiveField(ctx context.Context) (*string, error) { return r.QueryResolver.DirectiveField(ctx) } -func (r *stubQuery) InputSlice(ctx context.Context, arg []string) (bool, error) { - return r.QueryResolver.InputSlice(ctx, arg) -} -func (r *stubQuery) ShapeUnion(ctx context.Context) (ShapeUnion, error) { - return r.QueryResolver.ShapeUnion(ctx) -} -func (r *stubQuery) Autobind(ctx context.Context) (*Autobind, error) { - return r.QueryResolver.Autobind(ctx) -} -func (r *stubQuery) DeprecatedField(ctx context.Context) (string, error) { - return r.QueryResolver.DeprecatedField(ctx) -} -func (r *stubQuery) Overlapping(ctx context.Context) (*OverlappingFields, error) { - return r.QueryResolver.Overlapping(ctx) -} func (r *stubQuery) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) { return r.QueryResolver.MapStringInterface(ctx, in) }