Deploying Apollo Server to Now

How to Deploy Your Apollo GraphQL Server to Now

In this guide, we will be deploying an Apollo GraphQL server with ZEIT Now.

Getting Started

The first step is to create a directory for the project and cd into it via your terminal. It can be done with the following command:

mkdir apolloserver && cd apolloserver

Next, install the apollo-server-micro and graphql packages via your terminal.

yarn add apollo-server-micro graphql

Once the dependencies are installed, go ahead and create an index.js file at the root of the project directory with the following code as its contents:

const { ApolloServer, gql } = require('apollo-server-micro')

const typeDefs = gql`
  type Query {
    hello: String
  }
`

const resolvers = {
  Query: {
    hello: (root, args, context) => {
      return "Hello world! It's your boy, how far now unicodeveloper"
    }
  }
}

const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: true,
  playground: true
})

module.exports = server.createHandler();

The code above shows a basic Apollo server with an enabled GraphQL playground for testing out queries.

Deploy with Now

Create a now.json configuration file in the project directory and add the following content.

{
  "version": 2,
  "builds": [{ "src": "index.js", "use": "@now/node" }],
  "routes": [{ "src": "/.*", "dest": "index.js" }]
}
  • The version property specifies Now 2.0.
  • The routes property allows Now to route your deployment either by using a source and destination, or by way of a source, status, and headers.
  • The builds property allows Now to use a builder with a specific source target.

We used the @now/node builder to enable a Node.js deployment.

Next, deploy the app with the Now CLI.

now

Another option available is to deploy from GitHub with Now for GitHub. Now for GitHub allows you to deploy apps as you push code to repositories that have it installed and configured.

Now for GitHub provides the ability to deploy every push in branches and pull requests to preview changes live!

Resources

For more information on Apollo Server, please refer to the Apollo Server documentation.

To configure Now further, please see these additional topics and guides:



Written By
Written by unicodeveloperunicodeveloper
on January 24th 2019