Interact with a GraphQL API from a .NET Core Application
Rate this tutorial
RESTful APIs are great, but what happens when you want to work with GraphQL and query your APIs rather than just consume them?
In this tutorial, we'll see how to use .NET Core to interact with a GraphQL API. To make things easier, we'll generate our API with MongoDB App Services.
For this particular tutorial, you'll need to have a few things available and configured prior to getting started.
For reference, I'm using .NET Core 6.0.101, but any version of 6 or higher should work fine. We'll be using the CLI, but if you feel more comfortable using Visual Studio, that works too.
While it isn't 100% necessary to use MongoDB in this tutorial, MongoDB makes it extremely easy to get started with a GraphQL API because you can automatically generate an API from any of your databases with a few mouse clicks. For this reason, it will power the API for this example.
To keep things simple and easy to understand, we're going to create and work with a console based .NET Core application. We'll be able to spend less time with the configuration, and more with the code that matters.
Assuming .NET Core is installed and you have access to the CLI, execute the following command:
The above line will generate a new console application inside a MongoExample directory.
We're not done yet though. We need to install a GraphQL dependency for querying an actual GraphQL API as well as a dependency for working with JSON data. From the CLI, execute the following:
At the end of the day, GraphQL is still accessed over HTTP, so a package isn't absolutely necessary, but having one will make life much easier.
We have a starting point for our project. The next time we visit our project, we'll be writing code to do all the magic.
Getting a GraphQL API deployed is easy with MongoDB. We're going to assume that you have a MongoDB Cloud account with an Atlas cluster deployed. If you have no databases in your cluster, don't worry because you can use one of the sample sets that Atlas offers.
When we start writing code, we're going to refer to the "sample_mflix" database that is part of the sample dataset. Feel free to adapt the tutorial to something of your own as well.
With the database ready to go, click on the "App Services" navigation tab and choose to create a new application. The name isn't important as long as you are pointing to the correct cluster.
Within the MongoDB App Services portal, click the "GraphQL" tab and choose the "Generate JSON Schema" button. Find the database and collection you want to use with GraphQL and click the "Generate a Schema" button. We're using the "sample_mflix" database and "movies" collection in this example.
Don't forget to save after it generates!
Back in the "GraphQL" tab, you can now start querying your collection with GraphQL queries. You'll also notice your public GraphQL URL on the same page.
We're not done yet because we also need to generate a key for our API.
Click the "Authentication" tab and choose to edit the "API Keys" in the list. Enable the provider and generate a key. Don't forget your key because it will be used when we start writing code.
Since we're planning to use authentication, we need to click the "Rules" tab and define a new access rule for the collection that we're using. For this example, add a new rule that says everyone can read, but not write. Change the rules to best meet your needs.
Your GraphQL API is ready to go and you only had to do a few clicks within MongoDB!
We have a boilerplate .NET Core project and we have a GraphQL API to work with. Let's spend a little time putting some code together to query that API from the project.
It isn't a requirement, but it's a good idea to define a class for the GraphQL results. This will make your application more maintainable for the future. Within your project, create a new GraphQLMovieResponse.cs file with the following C# code:
The idea behind this class is that we'll be working with data that looks like the following:
Yes, there are more fields than just the
plotfields in our collection and GraphQL schema. For the sake of this example, we're only going to map the fields we plan to use.
Open the project's Program.cs file and replace the contents with the following:
A few things are happening in the above code.
First, we're defining our endpoint URL, which in this example is the endpoint the MongoDB App Services provides when configuring GraphQL. Next we're adding an
apiKeyheader to the request with our MongoDB App Services key that we defined for authentication. If your API doesn't require authentication, you won't need to add this.
Using the endpoint and key information, a client can be created. With the client, we can create a standard GraphQL query and send it.
The result of our request will be returned as a
GraphQLMovieResponselike we had previously created. With this response, we can access each of the fields and use them however we want.
To make things more exciting, we also saw how to deploy a GraphQL API using a few mouse clicks and MongoDB. This was made possible because of the MongoDB App Services.