>> Learn about using a GraphQL API in
Azure in this episode of Azure Tips and Tricks.
GraphQL is a great way of creating and querying APIs.
A GraphQL API is different from a REST API in that it
allows the client application to query
for certain fields of resources,
like the name of the user and only receive that data.
The client controls what data it receives, not the server.
Also, GraphQL provides an abstraction layer to the client,
which means that clients don't need to query
multiple URLs to access different data.
GraphQL APIs get all the data clients need in
a single request when you use
multiple GraphQL queries in one request.
GraphQL itself is not a framework or runtime that you can use.
It is a specification of how to implement
an API server site and how to query it from a client application.
We will use the Hot Chocolate GraphQL libraries
as the implementation.
This is just one of many libraries that you can use for GraphQL.
We will use a sample ASP.NET Core application that
implements the GraphQL specification to expose an API.
I'll first create a new directory,
like this one, and navigate to it to store the sampling.
Now, I'll create it with the Hot Chocolate templates.
There it goes. I'll open it in Visual Studio.
The application doesn't have any controllers,
but does have characters,
as in Star Wars characters,
repositories, reviews, and all that
comes together in the startup class, this one.
Here, you can see that it uses
the Hot Chocolate libraries for GraphQL.
Here, in the configure services method,
we see how the API is created.
It uses an in-memory subscription provider
that serves in-memory Star Wars data.
In a real-world scenario,
you would use another provider that gets data from,
let's say an Azure SQL database.
Here is the magic that makes the API work,
the "Add GraphQL" middleware.
This defines the GraphQL schema,
which contains the resource that we can
use as the queries that can get them.
Here, in the configure method,
we see that the app uses
the GraphQL route to expose the API endpoint.
It also uses a playground,
which we will use later to test the API.
If we go back for a second,
we can take a look at the repository here.
This acts as a datastore for the API and as you can see,
loads Star Wars data into memory that we can query with the API.
Now let's publish this to Azure.
I do right-click "Publish" here and here,
select "Azure" and "App Service on Windows".
I want to create a new App Service for this. Here we go.
I'll leave the name and I'll pick
a resource group and create a new hosting plan as well.
Now create it. There it is.
Now select it and publish the app to Azure.
I'll skip to when it is done.
This is the URL of the App Service that
we just created and published the app to.
There's nothing to see here,
but remember the GraphQL route.
Let's use that and let's navigate to the playground like this.
We can use this to test the API.
In a real app, you use the API by using http requests for it.
Now here, I can see the GraphQL schema for
the API and also the queries that I can use. Cool, right?
Like the "Hero" query,
which requires an episode and returns a character. Let's try that.
I can create a query like this and use
the "Hero" query and pass it as Star Wars episode.
I want to retrieve the name of the character.
There it goes, R2-D2.
Cool. The beauty of this
is that I can control the response of the query.
So I can also ask for the ID for example,
or in which episodes the character appears in.
The GraphQL specification is great for creating
APIs with and you can easily run them in Azure,
like we just did.
Go and check it out.