Relay, & GraphQL for .NET

I am very interested in GraphQL and I am currently working with GraphQL in ASP.NET Core 3.1 using GraphQL for .NET and Relay. It hasn’t been immediately clear what I needed to do in order to get up and running.

If you ignore Relay, things can be a lot easier. There’s a good example in the source for GraphQL.EntityFramework which includes GraphQL subscription too – which is nice. However, if you want to use Relay properly it isn’t immediately obvious that this isn’t currently possible with Entity Framework. If you are aiming to use Relay, this article provides a paging solution using SqlKata.

Getting started with Relay, the Quick Start Guide is fairly helpful, particularly Installation and Setup for the npm packages and build configuration.

Having created a GraphQL schema on the server using a code-first approach I was initially a bit confused by the requirement for a schema.graphql file. For a little time I thought I was going to have to recreate the schema manually, and might have been best going initially with a schema-first approach instead. However, reading How to Retrieve a GraphQL Schema was very helpful, once I discovered that the schema file can be a .graphql or a .json file!

To use the Relay Compiler, you need either a .graphql or .json GraphQL schema file, describing your GraphQL server’s API. Typically these files are local representations of a server source of truth and are not edited directly.

https://relay.dev/docs/en/graphql-in-relay.html#graphql-schema

We can use a GraphQL query to get the JSON version of the schema directly from the server endpoint.

4 simple ways to call a GraphQL API

Now, with the following relay-compiler command you can generate the framework that’s required for the Relay queries to work.

relay-compiler --src ./src --schema ./schema.json

This doesn’t replace a step-by-step guide, but a quick summary of some of the important details I found were not clearly noted.

I will write more about GraphQL again later. I think sometimes it is easier when there is a working example to learn from. I will think about providing an example to illustrate a step-by-step guide in more detail, perhaps.

It really is nice working with GraphiQL and Voyager. There are other alternative interfaces too, but I like these two.

This is the Voyager UI. It’s a pleasure to interact with. Very useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s