Custom DQL Query Result is Always Empty

Hi,

I am trying to get a custom DQL query to work in a GraphQL setting, but my response is always empty.
I have tried to shrink it down to a minimal example and still no success.

This is my schema:

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

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

type Query {
  queryPersonCustom : [Person] @custom(dql: """
    query {
      queryPersonCustom(func: uid(0x2)) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
      }
    }
  """)
}

I am adding a person like this:

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

mutation {
  addPerson(input: [{ firstName: "Harry", lastName: "Potter" }]) {
    person {
      firstName
      lastName
    }
  }
}

and it is assigned the uid 0x2, as I can check with this query:

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

query {
  queryPerson(filter: {id: "0x2"}) {
    id
    firstName
    lastName
  }
}

This returns { "id": "0x2", "firstName": "Harry", "lastName": "Potter" } as expected.
Also, my DQL is working in theory:

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

query {
  q(func: uid(0x2)) {
    id : uid
    firstName : Person.firstName
    lastName : Person.lastName
  }
}

This returns { "id": "0x2", "firstName": "Harry", "lastName": "Potter" } as well.

But calling the custom query via GraphQL always produces an empty result:

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

query {
  queryPersonCustom {
    id
    firstName
    lastName
  }
}

Can anyone tell me why? What am I missing?
Also: Is there a way to see what’s going on as dgraph processes my query? Can I somehow find out whether the DQL query’s result is empty or whether something goes wrong while mapping to GraphQL?

Thanks in advance!
Stefan

I’ve just followed your steps and it is working as you expected.

Thanks for the quick reaction.
For me it isn’t :frowning:

Your screenshot seems to show a regular GraphQL query. That’s working for me as well.
It is my queryPersonCustom DQL query (see schema) that’s not working.

What can I do to find out what is going wrong?

humm, I think you have to use has instead of func: uid(0x2)

Yep, I can confirm that using has(Person.firstName) works. The problem is the following.
func: uid(0x2) Returns a single object, and has(Person.firstName) returns an array. If you wanna just a single object you need to turn that query single instead of array.

1 Like

Cool, yes that works :slight_smile:

But why does func: uid(0x2,0x3) not work then? This also returns an array (I even added another person, so in my DB there is now also a person with UID 0x3 and still the query with func: uid(0x2,0x3) is empty.

What I actually want to achieve, is to pass a list of uids to the DQL, as in this post.

So this is my schema with DQL:

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

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

type Query {
  queryPersonCustom(ids: String!) : [Person] @custom(dql: """
    query q($ids : string) {
      queryPersonCustom(func: uid($ids)) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
      }
    }
  """)
}

and this is my concrete request:

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

query {
  queryPersonCustom(ids: "[0x2,0x3]") {
    id
    firstName
    lastName
  }
}

Again, the result is empty :frowning:

dunno, there are some dark magic in the GraphQL code that I’m not worthy to understand LOL
I didn’t work on that code, so I don’t know how it parses it.

Try this please

  query q($ids : string = "[0x2712, 0x1]") {
      queryPersonCustom(func: has(Person.firstName)) @filter(uid($ids)) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
        dgraph.type
      }
}

BTW, can you do something for me?
Can you try without square brackets?
It should return an error or the complete error like this

strconv.ParseUint: parsing \"0x2712,0x1\": invalid syntax

Also add the default value after this test

query q($ids : string = "[0x2712, 0x1]") {

This above is the sintax for default value in DQL.

1 Like

Ha! I got it working:

This is the schema / DQL:

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

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

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

and this is the query:

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

query {
  queryPersonCustom(ids: "[0x2,0x3]") {
    id
    firstName
    lastName
  }
}

About your question:

If I use queryPersonCustom(ids: "0x2,0x3") { ... } I get the error message resolving queryPersonCustom failed because got error while rewriting DQL query because strconv.ParseUint: parsing “0x2,0x3”: invalid syntax.

And the same error message is also shown if I use "0x2,0x3" as default value in the DQL query in the schema and leave out the ids parameter in the query execution.

Thank you so much for your quick support @MichelDiz !

Cool, I would prefer to use eq(dgraph.type, "Person") or has(with a limited set of objects) due to performance. But that is just a choice for the future. It doesn’t make any difference now.

1 Like