rogerballard / nestjs-relay

A batteries-included toolkit for building Relay-compliant GraphQL APIs with NestJS v7
MIT License
58 stars 14 forks source link

Question: How does this library intend the use of the ID scalar? #216

Closed cedrickring closed 3 years ago

cedrickring commented 3 years ago

First of all, thanks for the great package :)

When I integrated nestjs-relay in our existing application, there was an issue, where all ID fields had to be of type ResolvedGlobalId (as far as I understood). All places where I used a simple @Field(type => ID) with e.g. a string type (though I think it doesn't matter), the schema builder complained about the ID type being defined multiple times with different types. However with only ResolvedGlobalId-IDs the schema builder accepts the schema.

Is this intended and should all IDs be converted to ResolvedGlobalIds or am I missing something...

Thanks in advance 😄

rogerballard commented 3 years ago

Hi, apologies for the 10 days delay on response - I hope you've already managed to resolve this 🙂

So @nestjs/graphql comes with its own ID scalar, however this gets overridden by nestjs-relay so that it complies with the relay specification (the custom ID scalar can be found here). It doesn't surprise me that the schema builder complained that there were multiple definitions for the ID scalar.

I would recommend using the @NodeType decorator when defining your schema types, which will automatically add the id field with the correct scalar type for you.

Hope that helps

cedrickring commented 3 years ago

Alright this makes sense... So does relay benefit from using ResolvedGlobalIds for any type it has? Then it makes sense to use @NodeType on all schema types (so by replacing ObjecType with NodeType). I'm still not able to completely get into the Relay ideology 😄

Thankfully the ID stuff wasn't a huge problem. But now it really does make sense 👍🏽 .

rogerballard commented 3 years ago

The ResolvedGlobalId type is useful for the node/nodes root queries, which can be used by Relay for refetching data. Relay will hit the node query with an ID and expect the server to return the correct type. In the star wars example in the tests, you can see that the node resolver will handle IDs that are of types Ship and Faction.

The ResolvedGlobalId type is useful for interpreting the value of the ID to determine which type the server needs to resolve.

The @NodeType decorator is just a thin wrapper around the @ObjectType decorator from @nestjs/graphql that adds the ID field (which will handle the serialization/deserialization automagically).

cedrickring commented 3 years ago

Alright thanks for the clarification 😄

Maybe the ID type replacement stuff should be added to the docs as this was not quite clear to me that the GlobalIdScalar replaces the existing ID implementation.