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 and graphql packages via your terminal.

yarn add apollo-server 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')

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
})

server.listen({ port: 3000 }).then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`)
})

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-server" }],
  "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-server builder to enable a Node.js server 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:


How Was This Guide?

Written By
Written by unicodeveloperunicodeveloper
on January 24th 2019