Is it possible to set the return type in a DQL query for a GraphQL result?

I recently ran into the issue that if a type implements one or more interfaces, the return type from a DQL query will always be an array. If I know what kind of type I expect can I somehow “pick” the right one from the array manually?

Setup

Assume the following setup:

# Interface
interface TestInterface {
  id: ID!
  field: String
}
# First type which implements TestInterface
type FirstImplement implements TestInterface {
  id: ID!
  field: String
  firstField: String
}
# Second type which implements TestInterface
type SecondImplement implements TestInterface {
  id: ID!
  field: String
  secondField: String
}
# Type which has TestInterface as field
type HasInterfaceField {
  id: ID!
  interfaceField: TestInterface
}

Actual behaviour

If I now query HasInterfaceField for the “dgraph.type”

# DQL Query
{
  q(func: uid(0x1)) {
    HasInterfaceField.interfaceField {
      dgraph.type
    }
  }
}

the result would be

{
  data: {
    q: [
      0: {
        HasInterfaceField.interfaceField {
          dgraph.type: [
            "FirstImplement",
            "TestInterface"
          ]
        }
      }
    ]
  }
}

Assuming that HasInterfaceField has only been assigned with a TestInterface of type FirstImplement.

However, if I simply transform this result into GraphQL (so far only by removing the typenames before “.” and replacing “uid” with “id”), the typename for interfaceField will be TestInterface instead of FirstImplement in a GraphQL result.

I hope this makes sense :see_no_evil:

Any help appreciated!

This part is tripping me up… I tried

query {
    queryHasInterfaceField {
        __typename
        interfaceField {
            __typename
        }
    }
}

result:

{
  "queryHasInterfaceField": [
    {
      "__typename": "HasInterfaceField",
      "interfaceField": {
        "__typename": "FirstImplement"
      }
    }
  ]
}

I’ve got all this in a sandbox branch if you want to check it out/work there: GitHub - matthewmcneely/dgraph-v21.03-sandbox at explanations/interface-type-results

I’m not exactly sure what you mean but maybe I can clarify a few things here:

  • Direct GraphQL queries on the interface as well as the type result in the correct typename to returned
  • I am interested on returning the correct type name when performing a DQL query (eq. inside a custom lambda) and return the result.

I’m not sure how you guys transform a DQL query result into a GraphQL query result, so I have this probably very stupid approach of just stripping the typenames from the predicates in the return object. So
HasInterfaceField.interfaceField becomes interfaceField, which will then be returned.

I can imagine it has something to do with how I implement the interface, which I have asked here.

Asked this question again here