From fb3c43f9ab822423cd86bdfeb9a8ed1e72ef7699 Mon Sep 17 00:00:00 2001 From: Jatin Dev <64803093+JatinDevDG@users.noreply.github.com> Date: Wed, 13 Jan 2021 15:11:21 +0530 Subject: [PATCH] fix(GraphQL): This PR allow to use __typename in mutation. (#7285) 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 b6edc7e0de203241f8f205e1b6ed6517dc3e149b) --- graphql/e2e/common/common.go | 2 +- graphql/e2e/common/mutation.go | 6 +++++- graphql/resolve/resolver.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/graphql/e2e/common/common.go b/graphql/e2e/common/common.go index 63f221779a4..3bcb8328b33 100644 --- a/graphql/e2e/common/common.go +++ b/graphql/e2e/common/common.go @@ -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) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 6058e55ead5..818d340cc92 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -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 @@ -3501,6 +3503,8 @@ func queryTypenameInMutationPayload(t *testing.T) { RequireNoGQLErrors(t, gqlResponse) addStateExpected := `{ + "__typename":"Mutation", + "a":"Mutation", "addState": { "state": [{ "__typename": "State", diff --git a/graphql/resolve/resolver.go b/graphql/resolve/resolver.go index 54a79ea4eae..ba85f746527 100644 --- a/graphql/resolve/resolver.go +++ b/graphql/resolve/resolver.go @@ -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 + }) }) } @@ -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 } @@ -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 }