In the previous article, we have finished creating mutations in the GraphQL application, thus finishing the GraphQL integration in ASP.NET Core Web API. In this article, we are going to consume GraphQL API by creating an ASP.NET Core app.

To consume GraphQL API with the ASP.NET Core app, we are going to use the GraphQL.Client library which will help us in the process.

We won’t dive deep into the ASP.NET Core logic, just as much as we need to create our consuming application. But if you want to read in great detail about ASP.NET Core project development you can do that by reading ASP.NET Core Tutorial.

To download the source code, visit the Consuming a GraphQL API with ASP.NET Core Source Code.

For the complete navigation of this tutorial visit GraphQL ASP.NET Core Tutorial.

We are going to divide this article into the following sections:

Preparing the ASP.NET Core Client Project

Let’s start, by creating new ASP.NET Core Web API project. As soon as the project is created, we are going to modify the launchsettings.json file, by setting the launchBrowser property to false and applicationUrl property to https://localhost:5003;http://localhost:5004.

To learn more about ASP.NET Core project configuration, you can read the ASP.NET Core Project Configuration article.

To continue on, let’s modify the appsettings.json file, by adding address towards the GraphQL application:

Excellent. Now, we can install the required library:

GraphQL Client Library - Consume GraphQL API

Or by using the Package Manager Console: PM> Install-Package GraphQL.Client -Version 1.0.3

After the installation, we are going to register it in the Startup class:

Our application’s name is GraphQLClient, thus the full name for registering the library GraphQL.Client.GraphQLClient. If you named your app differently, just simple GraphQLClient would be enough.

The next step is creating the OwnerConsumer class, which will store all the queries and mutations:

Now let’s register this class:

That is it regarding configuration.

Creating Model Classes

In the first article of this tutorial, Getting Started with GraphQL in ASP.NET Core, you can see that we have the model classes in the starting project. Those classes are also required for the client application, so let’s create them:

We are not done yet. In the third part of this tutorial, GraphQL Mutations, we have created the Input type class for the mutation actions. This class is required as well, so let’s create it:

Awesome.

Now we have everything prepared and are ready to start creating queries and mutations.

Creating Queries and Mutations to Consume GraphQL API

Let’s open the OwnerConsumer class and add the GetAllOwners method:

As you can see, we are creating a new GraphQLRequest object which contains a Query property for the query we want to send to the GraphQL API. This query is the same as the one we used with the UI.Playground tool in previous articles.

To execute the query, we call the PostAsync method which returns a response. Finally, we convert our result in the required type by using the GetDataFieldsAs method.

We need to pay attention to the GetDataFieldAs method’s argument though. That value must match the query name:

Query match names - Consume GraphQL API

Here are all the other queries and mutations from the same class:

Get Query:

Create Mutation:

Update Mutation:

Delete Mutation:

Implementing a Controller

We are going to use the default ValuesController for the testing purpose:

Now we can test this via Postman:

Postman Result

Excellent, it works like a charm. Of course, you can test all the other queries and mutations by using Postman as well.

Conclusion

In this article we have learned:

  • How to prepare ASP.NET Core Client application to consume the GraphQL app
  • What library do we need to help us with the consuming process
  • How to create queries and mutations from a consuming application

In the next article, we are going to create an Angular application to consume the GraphQL API, by using the Apollo library.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.