The current Nuget package for graphql-dotnet/relay doesn't work with the latest version of GraphQL (4.2.2). So, I cloned the GitHub repository and created a Nuget package out of it. The package is available at the root of the sample repository of this blog series.
Install the Nuget package with one of the following commands,
The graphql-dotnet/relay library includes a few GraphQL types and helpers for creating Connections, Mutations, and Nodes. After adding the library, you have to register Relay specific GraphQL types with the ASP.NET Core's dependency injection (DI) container,
The two core assumptions that Relay makes about a GraphQL server are that it provides:
A mechanism for refetching an object.
A description of how to page through connections.
Object identification feature is enabled by using the NodeGraphType. The class extends ObjectGraphType and adds a GetById field, which helps Relay (via the node() Query) to refetch nodes when it needs to. It also provides an Id() for defining a global id for the node. The incoming and outgoing ids are all Base64 encoded.
To make the ItemType a node type, ItemType is extended from AsyncNodeGraphType<Item>,
The library also provides a MutationInputGraphType. It's an abstraction over the regular mutation implemented using Field. The core implementation is as following,
We have to extend the GameStoreMutation from MutationInputGraphType in order to use the Mutation<IMutationPayload<object>, MutationInputGraphType>,
Followings are the CreateItemInput and CreateItemPayload files,
MutationInputGraphType adds a clientMutationId field on top of InputGraphType. Although clientMutationId was required for Classic Relay, Modern Relay doesn't need that anymore.
The pagination and data slicing model used in GraphQL is called the Connection Cursor Specification. The base library graphql-dotnet/graphql-dotnet provides utilities to work with Connection. On top of it, graphql-dotnet/relay adds additional utilities to make it work with Relay.
To make the items field in the GameStoreQuery a Connection field, modify the code as followings,
Practically, you would want to see a result of a mutation (add/update/delete) directly in your connection. In that case, you should return an EdgeType in your mutation payload. For example, I would want to see the newly added item in my connection, I would modify the CreateItemPayload as following,