Skip to content

Commit

Permalink
Merge pull request #443 from eko/fix-369
Browse files Browse the repository at this point in the history
Fixed duplicated __typename in response (fixes #369)
  • Loading branch information
pavelnikolov authored Mar 29, 2021
2 parents d2656e8 + fe4cb53 commit 21b77bd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
19 changes: 16 additions & 3 deletions gqltesting/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Test struct {
Variables map[string]interface{}
ExpectedResult string
ExpectedErrors []*errors.QueryError
RawResponse bool
}

// RunTests runs the given GraphQL test cases as subtests.
Expand Down Expand Up @@ -57,10 +58,22 @@ func RunTest(t *testing.T, test *Test) {
}

// Verify JSON to avoid red herring errors.
got, err := formatJSON(result.Data)
if err != nil {
t.Fatalf("got: invalid JSON: %s", err)
var got []byte

if test.RawResponse {
value, err := result.Data.MarshalJSON()
if err != nil {
t.Fatalf("got: unable to marshal JSON response: %s", err)
}
got = value
} else {
value, err := formatJSON(result.Data)
if err != nil {
t.Fatalf("got: invalid JSON: %s", err)
}
got = value
}

want, err := formatJSON([]byte(test.ExpectedResult))
if err != nil {
t.Fatalf("want: invalid JSON: %s", err)
Expand Down
24 changes: 24 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,30 @@ func TestTypeName(t *testing.T) {
}
`,
},

{
Schema: starwarsSchema,
Query: `
{
hero {
__typename
name
... on Character {
...Droid
name
__typename
}
}
}
fragment Droid on Droid {
name
__typename
}
`,
RawResponse: true,
ExpectedResult: `{"hero":{"__typename":"Droid","name":"R2-D2"}}`,
},
})
}

Expand Down
15 changes: 10 additions & 5 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,17 @@ func collectFieldsToResolve(sels []selected.Selection, s *resolvable.Schema, res
field.sels = append(field.sels, sel.Sels...)

case *selected.TypenameField:
sf := &selected.SchemaField{
Field: s.Meta.FieldTypename,
Alias: sel.Alias,
FixedResult: reflect.ValueOf(typeOf(sel, resolver)),
_, ok := fieldByAlias[sel.Alias]
if !ok {
sf := &selected.SchemaField{
Field: s.Meta.FieldTypename,
Alias: sel.Alias,
FixedResult: reflect.ValueOf(typeOf(sel, resolver)),
}
field := &fieldToExec{field: sf, resolver: resolver}
*fields = append(*fields, field)
fieldByAlias[sel.Alias] = field
}
*fields = append(*fields, &fieldToExec{field: sf, resolver: resolver})

case *selected.TypeAssertion:
out := resolver.Method(sel.MethodIndex).Call(nil)
Expand Down

0 comments on commit 21b77bd

Please sign in to comment.