Lighthouse: How do you resolve global id's in v2?

Created on 14 May 2018  路  4Comments  路  Source: nuwave/lighthouse

So I'm currently upgrading an old project from lighthouse v1 to v2 and the seemingly last thing I am getting stuck on is resolving global ID's for data types. I'm implementing Node on my type like this:

type User implements Node {
    id: ID!
    email: String!
    name: String!
    gravitarUrl: String
}

but then when I go to query _id which is defined on the node I get this error: Cannot query field \"_id\" on type \"User\".

I looked at the tests and saw the @node directive, but I could not figure out how to use that. I also could not tell if that is for resolving the nodes from the node query or for resolving the global ID. I also have tried defining the _id: ID field on the User. I'm pretty stumped. Any help with this would be greatly appreciated. I can get the DB ID to resolve, but that causes uniqueness issues with Relay.

question

All 4 comments

Hey @alexwhb, so you just need to add the following to your schema file:

type User implements Node {
    _id: ID! @rename(attribute: "id") @globalId
    id: Int
    # ...
}

We're using 2 directives here. The first one is rename which will return whatever attribute you provide it (in this case it will use the id on the User model). The second directive globalId will convert the id to a Global ID that Relay should understand.

That's it! Let me know if you have any other questions!

@chrissm79 Thank you so much!! This worked like a dream! Seriously v2 is by far the best graphQL library I've used!

Thanks @alexwhb, I'm really happy with the way it turned out too 馃槃

I just use the id, but change it on the lighthouse configuration file, it defaults to _id,

Was this page helpful?
0 / 5 - 0 ratings