GraphQL/DQL query that returns various types ordered by DateTime across type?

Hello,

Given,

interface Animal {
  id: ID!
  adoptedAt: DateTime!
}

type Dog implements Animal {
  size: String!
}

type Cat implements Animal {
  name: String!
}

type Llama implements Animal {
  lastSheared: DateTime!
}

How does one query so that the return payload has animals sorted by adoptedAt regardless of type, and for each type includes predicates that are not defined in the interface?

If this is not possible via Dgraph’s GraphQL endpoint is it possible via DQL, and if so, how?

Thank you

First of all Type needs to be type

Adding some data:

mutation addData {
  addDog(input:[{
    adoptedAt:"2020-01-04"
    size:"large"
  }{
    adoptedAt:"2020-01-01"
    size:"small"
  }]) {
    dog {
      id
      adoptedAt
      size
    }
  }
  addCat(input:[{
    adoptedAt:"1980-12-17"
    name:"Arlene"
  }{
    adoptedAt:"1978-06-19"
    name:"Garfield"
  }]) {
    cat {
      id
      adoptedAt
      name
    }
  }
  addLlama(input:[{
    adoptedAt: "2020-01-02"
    lastSheared: "2020-01-02"
  }{
    adoptedAt: "2020-01-03"
    lastSheared: "2000-01-01"
  }]) {
    llama {
      id
      adoptedAt
      lastSheared
    }
  }
}

We can then use the ...on method to query the interface and get the related type attributes:

query QueryAnimalByAdoptedDate {
  queryAnimal(order:{asc:adoptedAt}) {
    id
    adoptedAt
    ...on Dog {
      size
    }
    ...on Cat {
      name
    }
    ...on Llama {
      lastSheared
    }
  }
}

Official GraphQL docs on Interfaces:

Note: Unions are not yet supported.

1 Like