From 40dc728d228c83bfe75e66c2e4562d6a6352b231 Mon Sep 17 00:00:00 2001 From: creativej Date: Thu, 2 Aug 2018 13:33:36 +1000 Subject: [PATCH] Ignore __ fields in instrospection --- graphql/introspection/type.go | 6 +++++ graphql/introspection/type_test.go | 36 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 graphql/introspection/type_test.go diff --git a/graphql/introspection/type.go b/graphql/introspection/type.go index 9c65b7ee813..72f993f3079 100644 --- a/graphql/introspection/type.go +++ b/graphql/introspection/type.go @@ -1,6 +1,8 @@ package introspection import ( + "strings" + "github.com/vektah/gqlparser/ast" ) @@ -63,6 +65,10 @@ func (t *Type) Fields(includeDeprecated bool) []Field { } var fields []Field for _, f := range t.def.Fields { + if strings.HasPrefix(f.Name, "__") { + continue + } + fields = append(fields, Field{ Name: f.Name, Description: f.Description, diff --git a/graphql/introspection/type_test.go b/graphql/introspection/type_test.go new file mode 100644 index 00000000000..e3a4613c5dd --- /dev/null +++ b/graphql/introspection/type_test.go @@ -0,0 +1,36 @@ +package introspection + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/vektah/gqlparser/ast" +) + +func TestType(t *testing.T) { + schemaType := Type{ + def: &ast.Definition{ + Name: "Query", + Description: "test description", + Fields: ast.FieldList{ + &ast.FieldDefinition{Name: "__schema"}, + &ast.FieldDefinition{Name: "test"}, + }, + Kind: ast.Object, + }, + } + + t.Run("name", func(t *testing.T) { + require.Equal(t, "Query", schemaType.Name()) + }) + + t.Run("description", func(t *testing.T) { + require.Equal(t, "test description", schemaType.Description()) + }) + + t.Run("fields ", func(t *testing.T) { + fields := schemaType.Fields(false) + require.Len(t, fields, 1) + require.Equal(t, "test", fields[0].Name) + }) +}