diff --git a/graphql/e2e/custom_logic/custom_logic_test.go b/graphql/e2e/custom_logic/custom_logic_test.go index 29a8169e32a..2ecce8b881f 100644 --- a/graphql/e2e/custom_logic/custom_logic_test.go +++ b/graphql/e2e/custom_logic/custom_logic_test.go @@ -2572,13 +2572,16 @@ func TestCustomDQL(t *testing.T) { common.SafelyDropAll(t) schema := ` - type Tweets { + interface Node { + id: ID! + } + type Tweets implements Node { id: ID! text: String! @search(by: [fulltext, exact]) user: User timestamp: DateTime! @search } - type User { + type User implements Node { screen_name: String! @id followers: Int @search tweets: [Tweets] @hasInverse(field: user) @@ -2596,6 +2599,17 @@ func TestCustomDQL(t *testing.T) { } type Query { + queryNodeR: [Node] @custom(dql: """ + query { + queryNodeR(func: type(Node), orderasc: User.screen_name) @filter(eq(Tweets.text, "Hello DQL!") OR eq(User.screen_name, "abhimanyu")) { + dgraph.type + id: uid + text: Tweets.text + screen_name: User.screen_name + } + } + """) + getFirstUserByFollowerCount(count: Int!): User @custom(dql: """ query getFirstUserByFollowerCount($count: int) { getFirstUserByFollowerCount(func: eq(User.followers, $count),orderdesc: User.screen_name, first: 1) { @@ -2708,6 +2722,11 @@ func TestCustomDQL(t *testing.T) { params = &common.GraphQLParams{ Query: ` query ($count: Int!) { + queryNodeR { + __typename + ... on User { screen_name } + ... on Tweets { text } + } queryWithVar: getFirstUserByFollowerCount(count: $count) { screen_name followers @@ -2740,6 +2759,10 @@ func TestCustomDQL(t *testing.T) { common.RequireNoGQLErrors(t, result) require.JSONEq(t, `{ + "queryNodeR": [ + {"__typename": "User", "screen_name": "abhimanyu"}, + {"__typename": "Tweets", "text": "Hello DQL!"} + ], "queryWithVar": { "screen_name": "abhimanyu", "followers": 5 diff --git a/graphql/schema/completion.go b/graphql/schema/completion.go index e16084a5d28..25a3f765196 100644 --- a/graphql/schema/completion.go +++ b/graphql/schema/completion.go @@ -72,6 +72,7 @@ func Unmarshal(data []byte, v interface{}) error { // At present, it is only used for building custom results by: // * Admin Server // * @custom(http: {...}) query/mutation +// * @custom(dql: ...) queries // // fields are all the fields from this bracketed level in the GraphQL query, e.g: // { @@ -117,12 +118,18 @@ func CompleteObject( seenField := make(map[string]bool) x.Check2(buf.WriteRune('{')) - // @custom(http: {...}) query/mutation results may return __typename in response for abstract - // fields, lets use that information if present. - typename, ok := res[Typename].(string) var dgraphTypes []string - if ok && len(typename) > 0 { + if typename, ok := res[Typename].(string); ok && len(typename) > 0 { + // @custom(http: {...}) query/mutation results may return __typename in response for + // abstract fields, lets use that information if present. dgraphTypes = []string{typename} + } else if dgTypeVals, ok := res["dgraph.type"].([]interface{}); ok { + // @custom(dql: ...) query results may return dgraph.type in response for abstract fields + for _, val := range dgTypeVals { + if typename, ok = val.(string); ok { + dgraphTypes = append(dgraphTypes, typename) + } + } } for _, f := range fields {