How to reference types in DQL schema definition?

Suppose my DQL schema has two nodes with a name predicate…

type User {
  name
}

type Pet {
  name
}

Many examples in the Dgraph documentation define a single index that is shared between types:

name: string @index(hash) .

So how would I declare a hash index for User.name and a term index for Pet.name? My first thought was to try this…

User.name: string @index(hash) .
Pet.name: string @index(term) .

…but the /alter endpoint returns an error:

Schema does not contain a matching predicate for field name in type User

Cuz your schema needs to be like this

type User {
  User.name
}

type Pet {
  Pet.name
}

User.name: string @index(hash) .
Pet.name: string @index(term) .

DQL Schema is different from GraphQL Schema. In GraphQL this pattern comes automatically, in DQL you have to do it manually.

Thank you, @MichelDiz. That worked perfectly.

For anyone else that may find this post helpful, it’s noteworthy to point out that the above schema doesn’t require User.name as the predicate in queries—you just use name:

{
  user(func: type(User)) {
    name // User.name doesn't work here
  }
}

I think something is off here then. If I want to access my GraphQL schema (without any mapped predicates) values I have to use the type dotted notation in DQL. In other words, for me it is opposite:

{
  user(func: type(User)) {
    User.name // name doesn't work here
  }
}

I think something is off here then.

You’re right, I made a mistake. I inadvertently created a user on the mutate tab of Ratel like this:

{
  set {
    _:user <dgraph.type> "User" .
    _:user <name> "Tron" .
  }
}

This is what I should have done:

{
  set {
    _:user <dgraph.type> "User" .
    _:user <User.name> "Tron" .
  }
}

Sorry for the confusion. I’m still trying to get my Dgraph sea legs.

oh, yes, I imported 7 million quads of data and then realized I made a similar mistake :grimacing:

It is good to see someone else using both graphql and DQL together. I will watch you for any future topics.