functions-and-graphql
functions-and-graphql copied to clipboard
How to use Netlify functions for a GraphQL API
Netlify Functions + GraphQL
This repo demonstrates how to use functions for a GraphQL API using apollo-server-lambda.
See the graphQL function code
For more information on functions. Checkout functions.netlify.com and the Netlify functions workshop
Setup & Deployment
Easy mode:
Simply click the deploy button:
Manual mode:
Follow the steps below to set this up via the netlify-cli
-
Fork this repo
Fork this repo and clone it down to your local machine
git clone repoURL -
Create this site in Netlify
Open your terminal and run the following command:
netlify initChoose "create & configure a new site", then add a site name or hit enter for one to be generated for you.
-
Install our graphQL dependencies
Change directories into our functions folder and create new package.json
cd functions && npm init -yNow install the
apollo-server-lambdanpm packagenpm install apollo-server-lambda -
Require the graphQL dependencies in your function
In
functions/graphql.js, require 'apollo-server-lambda' -
Define your GraphQL types
In
functions/graphql.js, define your graphQL types -
Define your GraphQL resolvers
In
functions/graphql.js, define your graphQL resolvers -
Create a new
ApolloServerIn
functions/graphql.js, create a newApolloServerand pass it your typeDefs and resolves -
Export the Lambda compatible ApolloServer
In
functions/graphql.js, turn your server into a lambda compatible function signatureserver.createHandler()Then export the handler
-
Deploy the site
Open your terminal and run the following command:
netlify deploy -pOpen the frontend and test your new graphQL endpoint
netlify open:site -
You did it! 🎉
You should see a site like https://graphql-functions.netlify.com/ where you can explore your graphQL schema.