Hosting GraphQL endpoints with Azure Functions
What is GraphQL?
GraphQL was created by Facebook as a way for clients to fetch the data they need at runtime. The website describes it with 3 simple bullets:
- Describe your data
- Ask for what you want
- Get predictable results
With GraphQL you write a schema that describes all of your data (names, types, nesting, etc.) like below:
When you write a query, you ask for the specific data that you're looking for. Making a RESTful API call, you get back what the server decides to send you. With GraphQL if you're only interested in the name, then that's all you receive in the payload (similar to an SQL query).
If you're writing a web application that leverages a vast array of micro-services, GraphQL can simplify the calls you make to your backend; and they're also easier for developers to interact with.
What are Azure Functions?
Azure Functions are one of the many ways of hosting code in Azure. They host small pieces of code which are designed to execute quickly to perform some task. You can create multiple functions within a single hosted project.
For example, if you wanted to simulate a traditional server with multiple RESTful routes you could create a function for each route. In this instance, you can create one Azure Function to host a single GraphQL endpoint.
Pay-Per-Use (Consumption Plan)
One nice thing about these types of functions is that they don't require dedicated compute. Which means you can opt for a cheaper hosting plan and only pay for the compute time you use. If in a single month, you only call your endpoint 5,000 times you only pay for those 5,000 executions.
The downside to this is, when you're not using the function it'll be turned off. That means that the first time you call the function, it'll take an extra bit of time to turn on and get going. This is called a "cold start".
Dedicated Compute (Premium Plan)
If you've got a monthly budget and want to dodge those cold starts, you can use a dedicated hosting plan. This means that there's always at least 1 worker ready to take requests. If you're expecting high volumes of requests, you can also specify more "warm" instances to ensure your users aren't experiencing long wait times while your app scales up to the demand.
One of the great things about Azure Functions is that they're super easy to modify. Changing some of these settings is as easy as adjusting a radio button in the Azure Portal.
Creating a GraphQL Endpoint with Azure Functions
To create your node based Azure Function, follow the documentation provided by Microsoft.
If you're using Apollo GraphQL you can deploy your application using their documentation.
I'll update this section in the future with an update of my own. :^)
Securing your GraphQL Endpoint with Authentication & Authorization
In order to add authentication to your GraphQL endpoint, navigate to the "Authentication / Authorization" portion of your Azure Function App.
Then, set your "App Service Authentication" to "On" and choose an identity provider. If you're using Azure Active Directory for your other applications, you can easily wire it up within this view.
With that, you have an Azure Function application running with AAD authentication built into it! Now when you call your GraphQL endpoint, all you have to do is specify your JWT token like you would on a REST API call!
This was a very brief overview of hosting your GraphQL endpoint in an Azure Function application. I will be updating this content as time goes on, but if you have any questions please feel free to reach out to me on Twitter @airercode500!