From 07ee49f3162553b41d45cee11ed0b96ecfe5d745 Mon Sep 17 00:00:00 2001 From: hollowj Date: Wed, 22 Aug 2018 15:41:43 +0300 Subject: [PATCH 1/2] Added args to introspection scheme directives. --- graphql/introspection/schema.go | 36 ++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/graphql/introspection/schema.go b/graphql/introspection/schema.go index dc93693b88..b5d2c48224 100644 --- a/graphql/introspection/schema.go +++ b/graphql/introspection/schema.go @@ -35,16 +35,34 @@ func (s *Schema) SubscriptionType() *Type { func (s *Schema) Directives() []Directive { var res []Directive + for _, d := range s.schema.Directives { - var locs []string - for _, loc := range d.Locations { - locs = append(locs, string(loc)) - } - res = append(res, Directive{ - Name: d.Name, - Description: d.Description, - Locations: locs, - }) + res = append(res, s.directiveFromDef(d)) } + return res } + +func (s *Schema) directiveFromDef(d *ast.DirectiveDefinition) Directive { + var locs []string + for _, loc := range d.Locations { + locs = append(locs, string(loc)) + } + + var args []InputValue + for _, arg := range d.Arguments { + args = append(args, InputValue{ + Name: arg.Name, + Description: arg.Description, + DefaultValue: defaultValue(arg.DefaultValue), + Type: WrapTypeFromType(s.schema, arg.Type), + }) + } + + return Directive{ + Name: d.Name, + Description: d.Description, + Locations: locs, + Args: args, + } +} From 12efa2d5d8c01a2359721b04a27a58c353567e34 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 23 Aug 2018 11:15:03 +1000 Subject: [PATCH 2/2] add tests --- integration/generated.go | 39 ++++++++++++++++++++++++++--- integration/integration-test.js | 6 ++--- integration/schema-expected.graphql | 5 +++- integration/schema.graphql | 3 +++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/integration/generated.go b/integration/generated.go index 00a3ec341f..b9795c3967 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -36,6 +36,7 @@ type ResolverRoot interface { } type DirectiveRoot struct { + Magic func(ctx context.Context, next graphql.Resolver, kind *int) (res interface{}, err error) } type ElementResolver interface { Child(ctx context.Context, obj *models.Element) (models.Element, error) @@ -1663,6 +1664,35 @@ func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Re ret = nil } }() + rctx := graphql.GetResolverContext(ctx) + for _, d := range rctx.Field.Definition.Directives { + switch d.Name { + case "magic": + if ec.directives.Magic != nil { + rawArgs := d.ArgumentMap(ec.Variables) + args := map[string]interface{}{} + var arg0 *int + if tmp, ok := rawArgs["kind"]; ok { + var err error + var ptr1 int + if tmp != nil { + ptr1, err = graphql.UnmarshalInt(tmp) + arg0 = &ptr1 + } + + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + } + args["kind"] = arg0 + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.Magic(ctx, n, args["kind"].(*int)) + } + } + } + } res, err := ec.ResolverMiddleware(ctx, next) if err != nil { ec.Error(ctx, err) @@ -1680,7 +1710,10 @@ func (ec *executionContext) introspectType(name string) *introspection.Type { } var parsedSchema = gqlparser.MustLoadSchema( - &ast.Source{Name: "schema.graphql", Input: `type Element { + &ast.Source{Name: "schema.graphql", Input: `"This directive does magical things" +directive @magic(kind: Int) on FIELD_DEFINITION + +type Element { child: Element! error: Boolean! mismatched: [Boolean!] @@ -1701,7 +1734,7 @@ enum DATE_FILTER_OP { input DateFilter { value: String! timezone: String = "UTC" - op: DATE_FILTER_OP = EQ + op: DATE_FILTER_OP = EQ @magic(kind: 1) } type User { @@ -1710,7 +1743,7 @@ type User { } type Viewer { - user: User + user: User @magic(kind: 1) } type Query { diff --git a/integration/integration-test.js b/integration/integration-test.js index 5880854d87..e7bcc4871a 100644 --- a/integration/integration-test.js +++ b/integration/integration-test.js @@ -4,12 +4,10 @@ import {ApolloClient} from "apollo-client"; import fetch from "node-fetch"; import gql from 'graphql-tag'; -if (!process.env.SERVER_URL) { - throw "SERVER_URL must be set" -} +var uri = process.env.SERVER_URL || 'http://localhost:8080/query'; const client = new ApolloClient({ - link: new HttpLink({uri: process.env.SERVER_URL, fetch: fetch}), + link: new HttpLink({uri, fetch}), cache: new InMemoryCache(), defaultOptions: { watchQuery: { diff --git a/integration/schema-expected.graphql b/integration/schema-expected.graphql index 212606aac2..52e5308faf 100644 --- a/integration/schema-expected.graphql +++ b/integration/schema-expected.graphql @@ -1,5 +1,8 @@ # source: http://localhost:8080/query -# timestamp: Fri Aug 10 2018 11:50:28 GMT+1000 (AEST) +# timestamp: Thu Aug 23 2018 10:30:37 GMT+1000 (AEST) + +"""This directive does magical things""" +directive @magic(kind: Int) on FIELD_DEFINITION enum DATE_FILTER_OP { EQ diff --git a/integration/schema.graphql b/integration/schema.graphql index 259aa40ad3..23c7d52aee 100644 --- a/integration/schema.graphql +++ b/integration/schema.graphql @@ -1,3 +1,6 @@ +"This directive does magical things" +directive @magic(kind: Int) on FIELD_DEFINITION + type Element { child: Element! error: Boolean!