Deploying with Azure Functions
Deploying your GraphQL server to Azure Functions
This is the Azure Functions integration for the Apollo community GraphQL Server. Read Docs
All examples below was created using Linux environments, if you are working with Windows-based platforms some commands couldn’t work fine.
Prerequisites
The following must be done before following this guide:
Setup an Azure account.
Install the Azure CLI 2.0.x to deploy to Azure.
Setting up the project
We will need to create our local project, installing the node.js dependencies and testing locally to make sure that all process will happen fine.
1func init apollo-example --worker-runtime node
2cd apollo-example
3func new --template "Http Trigger" --name graphql
Now, our project is prepared to start! Run func host start
command to see the output below.
1Hosting environment: Production
2Content root path: /root/apollo-example
3Now listening on: http://0.0.0.0:7071
4Application started. Press Ctrl+C to shut down.
5
6Http Functions:
7 graphql: http://localhost:7071/api/graphql
Go to http://localhost:7071/api/graphql?name=Apollo and verify if the text with the content: Hello Apollo is appearing at your browser.
If you would like to remove the api
from the url structure, set the prefix in your host.json
file like below:
1{
2 "version": "2.0",
3 "extensions": {
4 "http": {
5 "routePrefix": ""
6 }
7 }
8}
This will make your url look like http://{my-url}/graphql
like we would expect for most GraphQL projects.
Sample Code
We will now install the dependencies and test our azure function app using apollo server and graphql. Run the following commands to perform the node.js initialization project and to install the node.js dependencies.
1cd apollo-example
2npm init -y
3npm install apollo-server-azure-functions graphql
Copy the code below and paste in your index.js file.
1const { ApolloServer, gql } = require('apollo-server-azure-functions');
2
3// Construct a schema, using GraphQL schema language
4const typeDefs = gql`
5 type Query {
6 hello: String
7 }
8`;
9
10// Provide resolver functions for your schema fields
11const resolvers = {
12 Query: {
13 hello: () => 'Hello world!',
14 },
15};
16
17const server = new ApolloServer({ typeDefs, resolvers });
18exports.graphqlHandler = server.createHandler();
It is important to set output binding name to $return to work correctly at the function.json
file.
1{
2 "disabled": false,
3 "bindings": [
4 {
5 "authLevel": "function",
6 "type": "httpTrigger",
7 "direction": "in",
8 "name": "req",
9 "methods": [
10 "get",
11 "post"
12 ]
13 },
14 {
15 "type": "http",
16 "direction": "out",
17 "name": "$return"
18 }
19 ]
20}
Finally, we need to return to the base folder and run the func host start
command again after that, go back to your browser and refresh your page to see the apollo server running.
1func host start
Debugging the project locally in VS Code
Prerequisites
You will need to install the Azure Functions extension in VS Code (extension is currently in preview from Microsoft).
Configuring the project
In VS Code, open the root folder of your project and accept dialogs about setting up your project to work with VS Code. You should have one available debug configuration automatically created for you Attach to JavaScript Functions. If not, make sure your launch.json in the .vscode folder is setup correctly to launch the functions runtime:
1{
2 "version": "0.2.0",
3 "configurations": [
4 {
5 "name": "Attach to JavaScript Functions",
6 "type": "node",
7 "request": "attach",
8 "port": 5858,
9 "preLaunchTask": "runFunctionsHost"
10 }
11 ]
12}
Deploying the project to Azure using the Azure CLI
Setting up resources in Azure for deployment
Before deploying, a new application must be setup. To do this, we need to create some azure requirements. First, you will need a resource group, to create one run the code below on your terminal, where the --name is the name for the group and --location the region.
1az group create --name apollo-examples --location eastus
After creating a resource group, we need to create a storage account to store our code on Azure.
1az storage account create \
2 --name apolloexample \
3 --location eastus \
4 --resource-group apollo-examples \
5 --sku Standard_LRS
We will publish our application to Azure now using the CLI as well. We need to create a functionapp
running the following command.
Note: The your function name must be unique.
1az functionapp create \
2 --resource-group apollo-examples \
3 --name apollo-example \
4 --consumption-plan-location eastus \
5 --runtime node \
6 --storage-account apolloexample
Publishing our project to the function app
After creating a functionapp, it is just to publish our function to azure. The command below could be used to perform releases to all of your functions.
1func azure functionapp publish apollo-example
1Getting site publishing info...
2Preparing archive...
3Uploading 4.45 MB [###############################################################################]
4Upload completed successfully.
5Deployment completed successfully.
6Syncing triggers...
7Functions in apollo-example:
8 graphql - [httpTrigger]
9 Invoke url: https://apollo-example.azurewebsites.net/graphql?code=4aB9bka0fXFyTVeO8jAiHTc8bmyoqx2mEabk/QDA6gu2xLcqEAJRiw==
Finally, going to the Invoke URL shown at the output above, we will see our result.
Note: When GraphQL Playground starts, It won't have the correct URL containing the security code
, and a message "Server cannot be reached" as shown at your browser.
We just need to put the full URL that includes the security code
in the Playground url box. The background polling should refresh the screen momentarily. Click the Schema button to see if the docs are loaded correctly as shown in the image below.
Cleaning Up
After completing this tutorial, you can delete all the resources you created during this example from your Azure account by removing the Azure Resource Group called apollo-examples with the az group commmand. We can manually delete each resource using the following commands:
1az functionapp delete \
2 --resource-group apollo-examples \
3 --name apollo-example
4
5az storage account delete \
6 --name apolloexample \
7 --resource-group apollo-examples \
8 --yes
9
10az group delete \
11 --name apollo-examples \
12 --yes
Deploying to Azure from VS Code
It is also possible to publish your project from VS Code using the Azure Functions Extension, we recommend referring to Microsoft's documentation on publishing to Azure from VS Code.
Once deployment is complete, view the output in VS Code and you should be able to see the url of your GraphQL endpoint. It will look something like https://our-graphql-project.azurewebsites.net/api/graphql. Navigate to the url and you should find GraphQL Playground.
Note: When GraphQL Playground starts, It won't have the correct URL containing the security code
, and a message "Server cannot be reached" as shown at your browser.
We just need to put the full URL that includes the security code
in the Playground url box. The background polling should refresh the screen momentarily. Click the Schema button to see if the docs are loaded correctly as the image below.
Need more details? See the Docs at the NPM repository.
See ya!