Back to Prisma1

Deploy a GraphQL server with Now

docs/1.0/03-Tutorials/02-GraphQL-Server-Development/01-Deployment-with-Now.md

1.34.125.2 KB
Original Source

Deploy a GraphQL server with Now

Once you're done implementing your GraphQL server and have tested it enough locally to make it available to the general public, you need to deploy it to the web.

In this tutorial, you're going to learn how you can use Now - an amazing one-click deployment tool from the Zeit team - to deploy your GraphQL server.

The tutorial has two parts:

  1. Basic: Learn how to do a simple and straightforward deployment with now based on the node-basic GraphQL boilerplate project
  2. Advanced: Learn how to deploy with now with environment variables based on the node-advanced GraphQL boilerplate project

Install Now Desktop

The first thing you need to do is download the Now Desktop and login.

<Instruction>

Open https://zeit.co/download in your browser and hit the DOWNLOAD-button.

</Instruction>

Note: Now Desktop includes the now CLI.

Basic

Bootstrap the GraphQL server

In this tutorial, you'll use the node-basic GraphQL boilerplate project as a sample server to be deployed. The easiest way to get access to this boilerplate is by using the graphql create command from the GraphQL CLI.

The boilerplate project is based on graphql-yoga, a lightweight GraphQL server based on Express.js, apollo-server and graphql-tools.

<Instruction>

If you haven't already, go ahead and install the GraphQL CLI. Then, bootstrap your GraphQL server with graphql create:

npm install -g graphql-cli
graphql create hello-basic --boilerplate node-basic
</Instruction> <Instruction>

When prompted where (i.e. to which cluster) to deploy your Prisma service, choose one of the public cluster options: prisma-eu1 or prisma-us1.

</Instruction>

The above graphql create command creates a new directory called hello-basic where it places the source files for your GraphQL server as well as the configuration for the belonging Prisma service.

Deploy the server with now

The now command uploads your source files and invokes the start script defined in your package.json to started the remote server.

You now have a proper foundation to deploy your GraphQL server.

<Instruction>

All you need to is navigate into the hello-basic directory and invoke now:

cd hello-basic
now
</Instruction>

Note: If this is the first time you're using now, it will ask you to authenticate with their service.

That's it, your GraphQL server is now deployed and available under the URL printed by the CLI 🎉 The URL looks similar to https://hello-basic-__ID__.now.sh (where __ID__ is a random ID for your service generated by now).

Advanced: Using environment variables

Bootstrap the GraphQL server

<Instruction>

In your terminal, navigate into a new directory and download the node-advanced boilerplate as follows:

sh
graphql create hello-advanced --boilerplate node-advanced
</Instruction> <Instruction>

Like before, when prompted where to deploy the service, choose either prisma-eu1 or prisma-us1.

</Instruction>

The service is now deployed to a public cluster.

Deploy the server with now

This time, your service requires certain environment variables to be set. If you just ran now like in the previous section, the deployment would not succeed - or rather the Playground that would be deployed wouldn't work correctly because it doesn't know against which Prisma service it should be running. Because this information is now provided in terms of environment variables.

That's what you can use the --dotenv option of the now command for! It takes as an argument a .env file where environment variables are specifed.

.env files are a convention / best practice for specifying environment variables. Many tools (such as Docker or other deployment tooling) "understand" .env files - and so does now when using the --dotenv flag.

<Instruction>

All you need to is navigate into the hello-advanced directory and invoke now with that argument:

cd hello-advanced
now --dotenv .env
</Instruction>

If you deployed your Prisma service locally with Docker, your .env file will contain local references for the environment variables PRISMA_ENDPOINT and PRISMA_CLUSTER (such as http://localhost:60000/hello-advanced/dev). In that case you can create another .env file (e.g. called .env.prod) and make the PRISMA_ENDPOINT and PRISMA_CLUSTER are set to proper remote URLs (of course that requires that you properly deploy the Prisma service to some public cluster on the web before) and then refer to that one during deployment:

sh
now --dotenv .env.prod

You can find some documentation about how to handle environment variables and secrets when using now here.