Passing Sorting Criteria to Custom DQL

What I want to do

I have a custom DQL in my GraphQL schema.
I want to apply variable sorting/filtering on the result.
Is that possible?

Example:
Given the schema

POST http://localhost:8080/admin/schema

type Person {
  id: ID!
  firstName: String! @search
  lastName: String! @search
}

type Query {
  queryPersonCustom(ids: String) : [Person] @custom(dql: """
    query q($ids : string = "0x2,0x3") {
      queryPersonCustom(func: has(Person.firstName)) @filter(uid($ids)) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
      }
    }
  """)
}

I want to query queryPersonCustom() in such a way that the results are sorted by firstName or lastName or filtered by firstName or lastName.

For example, I want to write:

POST http://localhost:8080/graphql
Content-Type: application/graphql

query {
  queryPersonCustom(ids: "[0x2,0x3]", filterBy: "Person.firstName", filterTerm: "Harry", orderBy: "Person.lastName") {
    id
    firstName
    lastName
  }
}

to get all Persons in the given set of uids with firstName containing “Harry” to be returned in sort order of last name.

What I did

POST http://localhost:8080/query
Content-Type: application/dql

query q($p : string = "Person.firstName", $terms: string = "Harry") {
      queryPersonCustom(func: anyofterms($p, $terms)) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
      }
    }

But this leads to an error - I guess it’s not possible to pass an attribute/predicate name as a query parameter, so there is no solution for this one except to have one DQL per filter criteria and sort criteria, correct?

Note 1: I want to apply the filter on top of the uid(…) filtering. Myual usecase uses a var() query to determine the uids of the basic set. On top of that I want to filter by, e.g., firstName.

Note 2: I cannot/do not want to sort or filter on the client side, because I also want to do paging; this I managed to do by having query parameters for first and offset. It’s just left out of my example above for simplicity.

Dgraph metadata

dgraph version

Dgraph version : v21.12.0
Dgraph codename : zion
Dgraph SHA-256 : 078c75df9fa1057447c8c8afc10ea57cb0a29dfb22f9e61d8c334882b4b4eb37
Commit SHA-1 : d62ed5f15
Commit timestamp : 2021-12-02 21:20:09 +0530
Branch : HEAD
Go version : go1.17.3
jemalloc enabled : true

Add your experience in this issue

Share your thoughts there and like and follow the issue.
If a feature is not in demand, we will rarely care about it.

1 Like

See:

https://dgraph.io/docs/tips/#sort-edge-by-nested-node-values

J

1 Like