Building and Deploying a GraphQL Server

GraphQL is a powerful API layer that allows you to query your database faster and more efficiently. This tutorial will walk you through creating a GraphQL server using Prisma, which provides a service for deploying GraphQL servers.

First, click here to launch a Nodejs sandbox. Then, install prisma using the following command:

sudo npm install -g prisma

Make sure to add the above line to the startup script as well so prisma is installed each time the sandbox is loaded. The startup script can be found by clicking the icon on the left in the lab.

Deploying a GraphQL server

Now, create a new project by running:

prisma init hello-world

Using the down arrow key, select the Demo Server option:

Next, copy the URL that is output and visit that URL in a new tab. You should see a screen similar to the following:

Login or click Sign up at the bottom to create a free account. After you’ve created an account, click Grant Permission

You can then close the browser tab and return to your sandbox. You will be prompted to select a region; select the region closest to you (Note that eu1 is in Europe and us1 is in the United States).

Next, press Enter to use hello-world as the name of your service, then hit Enter again to use the dev stage.

You’ll then want to select Prisma JavaScript Client for the programming language and hit Enter.

At this point the GraphQL datamodel and schema files have been created! To deploy them to the GraphQL server, run the following commands:

cd hello-world
prisma deploy

If the deploy is successful it will output a HTTP URL silimar to https://us1.prisma.sh/user-name-def9f6/hello-world/dev where you can go to query the GraphQL endpoint.

Running queries and mutations

We’ll finish off this tutorial by running a couple simple GraphQL queries. Go to the URL that was output earlier and run the following query to create a user:

mutation {
  createUser(data: { name: "Codey" } ) {
    name
  }
}

Note: To run the query, type the query and then press the round “play” button in the middle of the page.

Finally, run the following query to list all the users in the database:

query {
  users {
    id
    name
  }
}

The output should look similar to the output shown below:

That’s it! You just created and deployed a GraphQL server to Prisma’s cloud! Now you can customize your GraphQL schema and build a Node application to the GraphQL endpoint.

To learn more about getting started using GraphQL with Prisma, you can check out their guide here.