Skip to content

Commit

Permalink
fix(GraphQL): Typename for types should be filled in query for schema…
Browse files Browse the repository at this point in the history
… introspection queries (dgraph-io#5827)

There was a missing " in the __typename that was filled in for types in schema introspection queries. That resulted in JSON parsing errors, we have fixed it and modified the existing test to test for it. Fixes dgraph-io#5792.
  • Loading branch information
pawanrawal authored and dna2github committed Jul 18, 2020
1 parent bee014e commit 17949ce
Show file tree
Hide file tree
Showing 6 changed files with 697 additions and 415 deletions.
9 changes: 6 additions & 3 deletions graphql/e2e/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const (
}
],
"enumValues":[]
} }`
},
"__typename" : "Query"
}`

expectedForType = `
{ "__type": {
Expand Down Expand Up @@ -75,7 +77,7 @@ const (
}
],
"enumValues":[]
} }`
}, "__typename" : "Query" }`

expectedForEnum = `
{ "__type": {
Expand All @@ -98,7 +100,7 @@ const (
}
],
"fields":[]
} }`
}, "__typename" : "Query" }`
)

func SchemaTest(t *testing.T, expectedDgraphSchema string) {
Expand Down Expand Up @@ -138,6 +140,7 @@ func graphQLDescriptions(t *testing.T) {
description
}
}
__typename
}`

for testName, tCase := range testCases {
Expand Down
4 changes: 4 additions & 0 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ func (rf *resolverFactory) WithSchemaIntrospection() ResolverFactory {
return QueryResolverFunc(resolveIntrospection)
}).
WithQueryResolver("__type",
func(q schema.Query) QueryResolver {
return QueryResolverFunc(resolveIntrospection)
}).
WithQueryResolver("__typename",
func(q schema.Query) QueryResolver {
return QueryResolverFunc(resolveIntrospection)
})
Expand Down
5 changes: 2 additions & 3 deletions graphql/schema/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// Introspect performs an introspection query given a query that's expected to be either
// __schema or __type.
func Introspect(q Query) (json.RawMessage, error) {
if q.Name() != "__schema" && q.Name() != "__type" {
if q.Name() != "__schema" && q.Name() != "__type" && q.Name() != Typename {
return nil, errors.New("call to introspect for field that isn't an introspection query " +
"this indicates bug (Please let us know : https://github.com/dgraph-io/dgraph/issues)")
}
Expand Down Expand Up @@ -164,7 +164,6 @@ func (ec *executionContext) handleQuery(sel ast.Selection) []byte {
}
ec.writeKey(field.Alias)
switch field.Name {
// TODO - Add tests for __typename.
case Typename:
x.Check2(ec.b.WriteString(`"Query"`))
case "__type":
Expand Down Expand Up @@ -327,7 +326,7 @@ func (ec *executionContext) handleType(sel ast.SelectionSet, obj *introspection.
ec.writeKey(field.Alias)
switch field.Name {
case Typename:
x.Check2(ec.b.WriteString(`"__Type`))
x.Check2(ec.b.WriteString(`"__Type"`))
case "kind":
ec.writeStringValue(obj.Kind())
case "name":
Expand Down
124 changes: 121 additions & 3 deletions graphql/schema/introspection_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema

import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestIntrospectionQueryMissingNameArg(t *testing.T) {
schema {
query: TestType
}
type TestType {
testField: String
}
Expand Down Expand Up @@ -220,6 +221,122 @@ func TestIntrospectionQueryWithVars(t *testing.T) {
testutil.CompareJSON(t, string(expectedBuf), string(resp))
}

const (
testIntrospectionQuery = `query {
__schema {
__typename
queryType {
name
__typename
}
mutationType {
name
__typename
}
subscriptionType {
name
__typename
}
types {
...FullType
}
directives {
__typename
name
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
fields(includeDeprecated: true) {
__typename
name
args {
...InputValue
__typename
}
type {
...TypeRef
__typename
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
__typename
}
interfaces {
...TypeRef
__typename
}
enumValues(includeDeprecated: true) {
name
isDeprecated
deprecationReason
__typename
}
possibleTypes {
...TypeRef
__typename
}
__typename
}
fragment InputValue on __InputValue {
__typename
name
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}`
)

func TestFullIntrospectionQuery(t *testing.T) {
sch := gqlparser.MustLoadSchema(
&ast.Source{Name: "schema.graphql", Input: `
Expand All @@ -232,7 +349,7 @@ func TestFullIntrospectionQuery(t *testing.T) {
}
`})

doc, gqlErr := parser.ParseQuery(&ast.Source{Input: introspectionQuery})
doc, gqlErr := parser.ParseQuery(&ast.Source{Input: testIntrospectionQuery})
require.Nil(t, gqlErr)

listErr := validator.Validate(sch, doc)
Expand All @@ -242,13 +359,14 @@ func TestFullIntrospectionQuery(t *testing.T) {
require.NotNil(t, op)
oper := &operation{op: op,
vars: map[string]interface{}{},
query: string(introspectionQuery),
query: string(testIntrospectionQuery),
doc: doc,
inSchema: &schema{schema: sch},
}

queries := oper.Queries()
resp, err := Introspect(queries[0])
fmt.Println(string(resp))
require.NoError(t, err)

expectedBuf, err := ioutil.ReadFile("testdata/introspection/output/full_query.json")
Expand Down
Loading

0 comments on commit 17949ce

Please sign in to comment.