Skip to content

Commit

Permalink
fix(GraphQL): This PR allow to use __typename in mutation. (#7285)
Browse files Browse the repository at this point in the history
Fixes GRAPHQL-921

The first mutation below will now return the "Mutation" type, which previously was giving an error.
Mutation:

mutation {
                       __typename
			addpost1(input: [{title: "Dgraph", numLikes: 92233720 }]) {
				post1 {
					title
					numLikes
				}
			}
		}

`Response:`
      {
                "__typename":"Mutation",
		"addpost1": {
			"post1": [{
				"title": "Dgraph",
				"numLikes": 92233720

			}]
		}
	}

(cherry picked from commit b6edc7e)
  • Loading branch information
JatinDev543 committed Jan 15, 2021
1 parent f5295c1 commit fb3c43f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func RunAll(t *testing.T) {
t.Run("numUids test", testNumUids)
t.Run("empty delete", mutationEmptyDelete)
t.Run("duplicate xid in single mutation", deepMutationDuplicateXIDsSameObjectTest)
t.Run("query typename in mutation payload", queryTypenameInMutationPayload)
t.Run("query typename in mutation", queryTypenameInMutation)
t.Run("ensure alias in mutation payload", ensureAliasInMutationPayload)
t.Run("mutations have extensions", mutationsHaveExtensions)
t.Run("alias works for mutations", mutationsWithAlias)
Expand Down
6 changes: 5 additions & 1 deletion graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3483,9 +3483,11 @@ func getXidFilter(xidKey string, xidVals []string) map[string]interface{} {
return filter
}

func queryTypenameInMutationPayload(t *testing.T) {
func queryTypenameInMutation(t *testing.T) {
addStateParams := &GraphQLParams{
Query: `mutation {
__typename
a:__typename
addState(input: [{xcode: "S1", name: "State1"}]) {
state {
__typename
Expand All @@ -3501,6 +3503,8 @@ func queryTypenameInMutationPayload(t *testing.T) {
RequireNoGQLErrors(t, gqlResponse)

addStateExpected := `{
"__typename":"Mutation",
"a":"Mutation",
"addState": {
"state": [{
"__typename": "State",
Expand Down
13 changes: 11 additions & 2 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ func (rf *resolverFactory) WithSchemaIntrospection() ResolverFactory {
WithQueryResolver("__typename",
func(q schema.Query) QueryResolver {
return QueryResolverFunc(resolveIntrospection)
}).
WithMutationResolver("__typename",
func(m schema.Mutation) MutationResolver {
return MutationResolverFunc(func(ctx context.Context, m schema.Mutation) (*Resolved, bool) {
return &Resolved{
Data: map[string]interface{}{"__typename": "Mutation"},
Field: m,
Err: nil,
Extensions: nil,
}, resolverSucceeded
})
})
}

Expand Down Expand Up @@ -368,7 +379,6 @@ func (rf *resolverFactory) queryResolverFor(query schema.Query) QueryResolver {
if resolver, ok := rf.queryResolvers[query.Name()]; ok {
return mws.Then(resolver(query))
}

return rf.queryError
}

Expand All @@ -377,7 +387,6 @@ func (rf *resolverFactory) mutationResolverFor(mutation schema.Mutation) Mutatio
if resolver, ok := rf.mutationResolvers[mutation.Name()]; ok {
return mws.Then(resolver(mutation))
}

return rf.mutationError
}

Expand Down

0 comments on commit fb3c43f

Please sign in to comment.