From 5ba8c8ead3ca891601215263b5969e4942c52a6b Mon Sep 17 00:00:00 2001 From: Mathew Byrne Date: Wed, 6 Mar 2019 11:32:47 +1100 Subject: [PATCH] Add builtin flag for build directives These have an internal implementation and should be excluded from the DirectiveRoot. In the future this may be a func that plugins could use to add custom implementations. --- codegen/data.go | 9 +++++- codegen/directive.go | 14 +++++---- codegen/testserver/generated.go | 50 +++++++++++++++++++++++++++++++ codegen/testserver/resolver.go | 3 ++ codegen/testserver/schema.graphql | 1 + codegen/testserver/stub.go | 4 +++ 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/codegen/data.go b/codegen/data.go index df1188b75a4..e7b5a92ef5e 100644 --- a/codegen/data.go +++ b/codegen/data.go @@ -62,9 +62,16 @@ func BuildData(cfg *config.Config) (*Data, error) { return nil, err } + dataDirectives := make(map[string]*Directive) + for name, d := range b.Directives { + if !d.Builtin { + dataDirectives[name] = d + } + } + s := Data{ Config: cfg, - Directives: b.Directives, + Directives: dataDirectives, Schema: b.Schema, SchemaStr: b.SchemaStr, Interfaces: map[string]*Interface{}, diff --git a/codegen/directive.go b/codegen/directive.go index 52f3cbae7b5..5a27e8ace71 100644 --- a/codegen/directive.go +++ b/codegen/directive.go @@ -11,8 +11,9 @@ import ( ) type Directive struct { - Name string - Args []*FieldArgument + Name string + Args []*FieldArgument + Builtin bool } func (b *builder) buildDirectives() (map[string]*Directive, error) { @@ -22,8 +23,10 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) { if _, ok := directives[name]; ok { return nil, errors.Errorf("directive with name %s already exists", name) } + + var builtin bool if name == "skip" || name == "include" || name == "deprecated" { - continue + builtin = true } var args []*FieldArgument @@ -50,8 +53,9 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) { } directives[name] = &Directive{ - Name: name, - Args: args, + Name: name, + Args: args, + Builtin: builtin, } } diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 6932d23fd05..3eb743e9b26 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -130,6 +130,7 @@ type ComplexityRoot struct { InputSlice func(childComplexity int, arg []string) int ShapeUnion func(childComplexity int) int Autobind func(childComplexity int) int + DeprecatedField func(childComplexity int) int Panics func(childComplexity int) int ValidType func(childComplexity int) int } @@ -191,6 +192,7 @@ type QueryResolver interface { 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) Panics(ctx context.Context) (*Panics, error) ValidType(ctx context.Context) (*ValidType, error) } @@ -578,6 +580,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.Autobind(childComplexity), true + case "Query.DeprecatedField": + if e.complexity.Query.DeprecatedField == nil { + break + } + + return e.complexity.Query.DeprecatedField(childComplexity), true + case "Query.Panics": if e.complexity.Query.Panics == nil { break @@ -850,6 +859,7 @@ scalar MarshalPanic inputSlice(arg: [String!]!): Boolean! shapeUnion: ShapeUnion! autobind: Autobind + deprecatedField: String! @deprecated(reason: "test deprecated directive") } type Subscription { @@ -2709,6 +2719,32 @@ func (ec *executionContext) _Query_autobind(ctx context.Context, field graphql.C return ec.marshalOAutobind2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐAutobind(ctx, field.Selections, res) } +func (ec *executionContext) _Query_deprecatedField(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { + ctx = ec.Tracer.StartFieldExecution(ctx, field) + defer func() { ec.Tracer.EndFieldExecution(ctx) }() + rctx := &graphql.ResolverContext{ + Object: "Query", + Field: field, + Args: nil, + } + 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().DeprecatedField(rctx) + }) + if resTmp == nil { + if !ec.HasError(rctx) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + rctx.Result = res + ctx = ec.Tracer.StartFieldChildExecution(ctx) + return ec.marshalNString2string(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_panics(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { ec.Tracer.EndFieldExecution(ctx) }() @@ -4932,6 +4968,20 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr res = ec._Query_autobind(ctx, field) return res }) + case "deprecatedField": + 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_deprecatedField(ctx, field) + if res == graphql.Null { + invalid = true + } + return res + }) case "panics": field := field out.Concurrently(i, func() (res graphql.Marshaler) { diff --git a/codegen/testserver/resolver.go b/codegen/testserver/resolver.go index 91c494ab1a8..fc062c5f01f 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -110,6 +110,9 @@ func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) { func (r *queryResolver) Autobind(ctx context.Context) (*Autobind, error) { panic("not implemented") } +func (r *queryResolver) DeprecatedField(ctx context.Context) (string, error) { + panic("not implemented") +} func (r *queryResolver) Panics(ctx context.Context) (*Panics, error) { panic("not implemented") } diff --git a/codegen/testserver/schema.graphql b/codegen/testserver/schema.graphql index d6a7fb51274..5ec9de8d065 100644 --- a/codegen/testserver/schema.graphql +++ b/codegen/testserver/schema.graphql @@ -18,6 +18,7 @@ type Query { inputSlice(arg: [String!]!): Boolean! shapeUnion: ShapeUnion! autobind: Autobind + deprecatedField: String! @deprecated(reason: "test deprecated directive") } type Subscription { diff --git a/codegen/testserver/stub.go b/codegen/testserver/stub.go index a99c44461fc..783f8aff58a 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -40,6 +40,7 @@ type Stub struct { 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) Panics func(ctx context.Context) (*Panics, error) ValidType func(ctx context.Context) (*ValidType, error) } @@ -151,6 +152,9 @@ func (r *stubQuery) ShapeUnion(ctx context.Context) (ShapeUnion, error) { 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) Panics(ctx context.Context) (*Panics, error) { return r.QueryResolver.Panics(ctx) }