From 7400221c3a5e8cd8917726e9e92522679c2acfbe Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Sat, 4 Aug 2018 12:01:26 +1000 Subject: [PATCH] Fix introspection api --- example/chat/generated.go | 7 +++++-- example/config/generated.go | 7 +++++-- example/dataloader/generated.go | 7 +++++-- example/scalars/generated.go | 7 +++++-- example/selection/generated.go | 7 +++++-- example/starwars/generated.go | 7 +++++-- example/todo/generated.go | 11 +++++++++-- example/todo/schema.graphql | 4 ++++ example/todo/todo_test.go | 2 +- graphql/introspection/introspection.go | 2 +- graphql/introspection/schema.go | 9 ++++++++- graphql/introspection/type.go | 26 +++++++++++++++++++++++--- test/generated.go | 7 +++++-- 13 files changed, 81 insertions(+), 22 deletions(-) diff --git a/example/chat/generated.go b/example/chat/generated.go index 1b44be29959..1a8cbfd58e0 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -990,8 +990,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/config/generated.go b/example/config/generated.go index fbc3678a436..f89970038f0 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -907,8 +907,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 0b27e308b9a..f0cdd36ef93 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -1159,8 +1159,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 6c699dad658..60781ff47ec 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -964,8 +964,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/selection/generated.go b/example/selection/generated.go index e332de21741..31ee288de6f 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -879,8 +879,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/starwars/generated.go b/example/starwars/generated.go index 5ad0e648197..88ea2e4b9e5 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -2007,8 +2007,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} diff --git a/example/todo/generated.go b/example/todo/generated.go index 0896d901364..a9f5be136a3 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -963,8 +963,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"} @@ -1468,13 +1471,17 @@ type Todo { done: Boolean! } +"Passed to createTodo to create a new todo" input TodoInput { + "The body text" text: String! + "Is it done already?" done: Boolean } scalar Map +"Prevents access to a field if the user doesnt have the matching role" directive @hasRole(role: Role!) on FIELD_DEFINITION enum Role { diff --git a/example/todo/schema.graphql b/example/todo/schema.graphql index fd84dbef9db..563a8daf0ff 100644 --- a/example/todo/schema.graphql +++ b/example/todo/schema.graphql @@ -21,13 +21,17 @@ type Todo { done: Boolean! } +"Passed to createTodo to create a new todo" input TodoInput { + "The body text" text: String! + "Is it done already?" done: Boolean } scalar Map +"Prevents access to a field if the user doesnt have the matching role" directive @hasRole(role: Role!) on FIELD_DEFINITION enum Role { diff --git a/example/todo/todo_test.go b/example/todo/todo_test.go index a4a067ccd0a..e7bbad936f1 100644 --- a/example/todo/todo_test.go +++ b/example/todo/todo_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/99designs/gqlgen/client" - introspection "github.com/99designs/gqlgen/graphql/introspection" + "github.com/99designs/gqlgen/graphql/introspection" "github.com/99designs/gqlgen/handler" "github.com/stretchr/testify/require" ) diff --git a/graphql/introspection/introspection.go b/graphql/introspection/introspection.go index c8dfc1f0c27..baff882ef29 100644 --- a/graphql/introspection/introspection.go +++ b/graphql/introspection/introspection.go @@ -30,7 +30,7 @@ type ( InputValue struct { Name string Description string - DefaultValue string + DefaultValue *string Type *Type } ) diff --git a/graphql/introspection/schema.go b/graphql/introspection/schema.go index e781e5eb6d0..dc93693b886 100644 --- a/graphql/introspection/schema.go +++ b/graphql/introspection/schema.go @@ -1,6 +1,10 @@ package introspection -import "github.com/vektah/gqlparser/ast" +import ( + "strings" + + "github.com/vektah/gqlparser/ast" +) type Schema struct { schema *ast.Schema @@ -9,6 +13,9 @@ type Schema struct { func (s *Schema) Types() []Type { var types []Type for _, typ := range s.schema.Types { + if strings.HasPrefix(typ.Name, "__") { + continue + } types = append(types, *WrapTypeFromDef(s.schema, typ)) } return types diff --git a/graphql/introspection/type.go b/graphql/introspection/type.go index 72f993f3079..f82c26bf566 100644 --- a/graphql/introspection/type.go +++ b/graphql/introspection/type.go @@ -69,9 +69,20 @@ func (t *Type) Fields(includeDeprecated bool) []Field { continue } + var args []InputValue + for _, arg := range f.Arguments { + args = append(args, InputValue{ + Type: WrapTypeFromType(t.schema, arg.Type), + Name: arg.Name, + Description: arg.Description, + DefaultValue: defaultValue(f.DefaultValue), + }) + } + fields = append(fields, Field{ Name: f.Name, Description: f.Description, + Args: args, Type: WrapTypeFromType(t.schema, f.Type), IsDeprecated: isDeprecated(f.Directives), DeprecationReason: deprecationReason(f.Directives), @@ -88,14 +99,23 @@ func (t *Type) InputFields() []InputValue { var res []InputValue for _, f := range t.def.Fields { res = append(res, InputValue{ - Name: f.Name, - Description: f.Description, - Type: WrapTypeFromType(t.schema, f.Type), + Name: f.Name, + Description: f.Description, + Type: WrapTypeFromType(t.schema, f.Type), + DefaultValue: defaultValue(f.DefaultValue), }) } return res } +func defaultValue(value *ast.Value) *string { + if value == nil { + return nil + } + val := value.String() + return &val +} + func (t *Type) Interfaces() []Type { if t.def == nil || t.def.Kind != ast.Object { return nil diff --git a/test/generated.go b/test/generated.go index caf99bff3e4..fc71feb3860 100644 --- a/test/generated.go +++ b/test/generated.go @@ -998,8 +998,11 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel if resTmp == nil { return graphql.Null } - res := resTmp.(string) - return graphql.MarshalString(res) + res := resTmp.(*string) + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __SchemaImplementors = []string{"__Schema"}