GraphQL tutorial: How to get started
Tags: graphql, beginners, node, tutorial
Published: 2022-01-12 20:22:15

The pace of innovation is accelerating and business efficiency is becoming more critical than ever. To stay competitive, businesses need continuous access to data through connected applications.

**Application Programming Interfaces (APIs)** serve as the digital glue, connecting applications together and breaking down data silos. With APIs in place, developers can easily build new application features without having to build them from scratch.

[GraphQL](https://fauna.com/blog/what-is-graphql-use-cases-applications-and-databases) is a query language for interacting with APIs. It has grown in popularity in recent years as a more flexible and performant [alternative to traditional REST APIs](https://fauna.com/blog/graphql-vs-rest).

This guide will outline how to get started with GraphQL for beginners. With immersive code examples, we will explain how to set up and get started in GraphQL, as well as tips and best practices for building your first service.

## What is GraphQL and how does It work?

GraphQL has its [roots at Facebook](https://engineering.fb.com/2015/09/14/core-data/graphql-a-data-query-language/). In the mid-2010s, Facebook was searching for a data-fetching API that would be powerful enough to "describe all of Facebook," while at the same time simple enough for developers to easily work with, especially when building [microservices-based apps](https://fauna.com/blog/how-to-build-microservices-with-node-js).

Faced with internal requirements, Facebook developed the **Graph Query Language** (or GraphQL) with a few key features:

1. GraphQL was designed as an abstraction over an API, accessible using a single endpoint. This made it language-agnostic and easy to use since the underlying API could be built using any preferred language.
2. GraphQL is accessible over a single endpoint using a composable query syntax. In comparison to REST, this design hides all underlying complexities and ensures you only get the data you need in a single round-trip without any underfetching or overfetching. Further, there is no need to deal with API versioning, so aging fields can be removed without affecting existing queries.

## Getting started with your first GraphQL API
In this section, we will walk through a simplified version of the steps needed to create a GraphQL API.

### Prerequisites
To follow along with this tutorial, make sure you have the following setup on your environment:

1. Node.js [version >= 16.0.0](https://nodejs.org/en/download/releases/)
2. [Fauna’s GraphQL  ](https://docs.fauna.com/fauna/current/learn/quick_start/gql_quick_start)

You can install Node.js by following the instructions at the link [here](https://nodejs.org/dist/v16.13.0/node-v16.13.0.pkg). You can also register for a Fauna account [here](https://dashboard.fauna.com/accounts/register).

### Initializing your project
Next, create a directory to place the GraphQL application and initialize your node project by running the following commands:

```bash
> mkdir fauna-graphql
> cd fauna-graphql
> npm init -y
```

Once these steps are executed, you should see a package.json file in the created directory.

Next, install the dependencies required for GraphQL as well as your server that will provide the GraphQL endpoint. In this example, we will be using [Apollo Server](https://www.apollographql.com/), but you can also use other GraphQL servers such as [Graphpack](https://github.com/glennreyes/graphpack).

We will also be using the [Fauna Javascript driver](https://docs.fauna.com/fauna/current/drivers/javascript) to interact with our database.  Install the dependencies by running the following commands:

```bash
> npm install --save apollo-server graphql faunadb
> npm install -D nodemon
```

In your package.json file, update the following:

```json
"scripts": {
   "serve": "nodemon index.js"
 },
"type": "module",
```

Next create the index.js file, which we will use as the entry point for our GraphQL APIs, by running:

```bash
> touch index.js
```

### Create GraphQL schema
Next, we will create our schema that defines the structure of the data we will want to interact with. For this example, we will use a simple todo list, with each item containing the title for that todo item and a boolean flag to mark it as complete or incomplete.

In your index.js file, import the gql class and define the schema by adding the following. We will also define a query that will return a list of 0 or more todo items from our database.

```js
import { ApolloServer, gql } from 'apollo-server-express';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import express from 'express';
import http from 'http';
 
const typeDefs = gql`
   type Todo {
       title: String
       completed: Boolean
   }
 
   type Query {
       allTodos: [Todo]
       todosByCompletedFlag(completed: Boolean): [Todo]
    }
;
```

### Writing resolvers
Next, we will define our resolvers. A resolver is a collection of functions that generate responses for GraphQL queries. While we can think of schema as defining the structure of our API, resolvers define where and how data is interacted with.

First, we will use static data that we define in our index.js file to test our queries. Add the resolvers and static data using the following:

```js
const resolvers = {
   Query: {
     allTodos: () => todos,
     todosByCompletedFlag(parent, args, context, info) {
        return todos.filter(todo => todo.completed === args.completed)
     }
   },
 };
 const todos = [
   {
     title: 'Install Dependencies',
     completed: false,
   },
   {
     title: 'Define Schemas',
     completed: true,
   },
   {
       title: 'Define Resolvers',
       completed: false
   }
 ];
```

### Create your server instance and run it

Next, instantiate Apollo server by adding the following:
```js
const app = express();
 const httpServer = http.createServer(app);
 
 const server = new ApolloServer({
   typeDefs,
   resolvers,
   plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
 });
 
 await server.start();
 server.applyMiddleware({
   app,
   path: '/',
 });
 
 // Modified server startup
 await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve));
 console.log(`Server ready at http://localhost:4000${server.graphqlPath}`);
```

Start the server by running the following in your terminal:

```bash
> npm run serve
```

### Interact with your data: Query to read or fetch values
You should now be able to access the GraphQL studio which lists out the queries just defined.

![GraphQL Studio](//images.ctfassets.net/po4qc9xpmpuh/2jHnlyUoKIqvnHv0PsqCHa/34f78ba8d6b0d037f44598b0fe3419af/GraphQL_Studio.png)

Run the first query by adding the following in the middle section and hitting the Run button. This query reads all the Todo items, and fetches the title and completed status

```graphql
query AllTodos {
 allTodos {
   title
   completed
 }
}
```
![First query](//images.ctfassets.net/po4qc9xpmpuh/2ipfH7IrYvsv51xSqTPHAp/0873434b149d761c81b115dacf1bfab6/First_query.png)

The second query takes the ‘completed’ argument and  returns the Todo items based on whether they are completed or not. Run the second query to return incomplete todo items by adding the following and hitting run.

```graphql
query TodosByCompletedFlag {
 todosByCompletedFlag(completed: false) {
   title
   completed
 }
```

![Run first query GraphQL](//images.ctfassets.net/po4qc9xpmpuh/432LSmTI11fylgwyPG91NU/b16051959bd61108402ba9e3bc23b10e/Run_first_query_GraphQL.png)

Now that we have verified that the server is working, we can update the resolvers to query Fauna instead of static data. Add the following to the index.js file to configure your connection settings.

```js
const faunadb = require('faunadb');
 
const client = new faunadb.Client({
   secret: 'YOUR_FAUNA_SECRET',
   domain: 'db.us.fauna.com',
   // NOTE: Use the correct domain for your database's Region Group.
   port: 443,
   scheme: 'https',
 })
```

### Interact with your data: Populate with sample data
Next add the following query to write sample Todo items to the Todo collection of your Fauna Database.

```js
query  = faunadb.query;
 
 client.query(
   query.Map(
     [
       {
           title: 'Install Dependencies',
           completed: false,
       },
       {
           title: 'Define Schemas',
           completed: true,
       },
       {
         title: 'Define Resolvers',
         completed: false
       },
     ],
     query.Lambda(
       'todos',
       query.Create(
           query.Collection('todos'),
         { data: query.Var('todos') },
       )
     ),
   )
 )
 .then((ret) => console.log(ret))
 .catch((err) => console.error('Error: %s', err))
```

Log into Fauna and view the todos collection to see the Todo items inserted as documents.

![todo documents](//images.ctfassets.net/po4qc9xpmpuh/6t356mswmRyvw2AOL4i0Gm/044de076fdb255299c7b70de94739255/todo_documents.png)

Next create an index on the todo title field by adding the following:

```js
client.query(
   query.CreateIndex({
     name: 'todos_by_title',
     source: query.Collection('todos'),
     terms: [{ field: ['data', 'title'] }],
   })
 )
 .then((ret) => console.log(ret))
 .catch((err) => console.error('Error: %s', err))
 ```

Next, update the resolvers to query the database:

```js
const resolvers = {
   Query: {
     allTodos() {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => todos.push(todo.data));
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     },
     todosByCompletedFlag(parent, args, context, info) {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => {
               if(todo.data.completed === args.completed){
                   todos.push(todo.data)
               }
           });
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     }
   },
 };
```

Rerun the previous queries using the GraphQL studio to verify that the data is coming from the database.

![Todos by completed flag](//images.ctfassets.net/po4qc9xpmpuh/1jpVsExwTznlDyTnaLxUP5/b0a9be7003d54d7ca413d4336ad2d385/Todos_by_completed_flag.png)

### Interact with your data: Add mutations to modify data
Next, let’s add a mutation to add new Todo items to our database. Define the mutation by adding the following to the GraphQL schema.

```js
const typeDefs = gql`
   type Todo {
       title: String!
       completed: Boolean
   }
 
   type Query {
       allTodos: [Todo!]
       todosByCompletedFlag(completed: Boolean!): [Todo!]
    }
 
    type Mutation {
        addTodo(title: String!, completed:Boolean!): Todo!
    }
`;
```

Next, add the resolver that will write the todo item to the database and return the todo item once inserted.

```js
const resolvers = {
   Query: {
     allTodos() {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => todos.push(todo.data));
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     },
     todosByCompletedFlag(parent, args, context, info) {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => {
               if(todo.data.completed === args.completed){
                   todos.push(todo.data)
               }
           });
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     }
   },
   Mutation: {
       addTodo(parent, args, context, info) {
           return client.query(
               query.Create(
                 query.Collection('todos'),
                 { data: {
                     title: args.title,
                     completed: args.completed
                   }
                },
               )
             )
             .then((ret) => {return ret.data})
             .catch((err) => console.error('Error: %s', err))
       }
   }
 };
```


From the GraphQL studio, run the following mutation.

```graphql
mutation AddTodo {
 addTodo(title: "Add Mutations", completed: false) {
   title,
   completed
 }
}
```

![Mutation addtodo](//images.ctfassets.net/po4qc9xpmpuh/4C7gXzCGDjq1l0uTpTTW8J/b3d291c622d170d0ebc23f8598ce79c4/Mutation_addtodo.png)

You can now see the todo item added to your database collection.

![Database collection](//images.ctfassets.net/po4qc9xpmpuh/2bGTqkco6glAPdMnOUU2xy/6991eb58dad2bba6bfd4cbf7feb778d5/Database_collection.png)

Next we will add two more mutations,: one to update an existing todo items status, and one to delete the item. Update the schema definition by adding the following:

```js
const typeDefs = gql`
   type Todo {
       title: String!
       completed: Boolean
   }
 
   type Query {
       allTodos: [Todo!]
       todosByCompletedFlag(completed: Boolean!): [Todo!]
    }
 
    type Mutation {
        addTodo(title: String!, completed:Boolean!): Todo!
        updateTodo(title: String!, completed:Boolean!): Todo!
        deleteTodo(title: String!): Todo!
    }
`;
```

Define the mutations by adding the following:

```js
const resolvers = {
   Query: {
     allTodos() {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => todos.push(todo.data));
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     },
     todosByCompletedFlag(parent, args, context, info) {
       return client.query(
           query.Map(
               query.Paginate(query.Match(query.Index('todos'))),
               query.Lambda(x => query.Get(x))
             )
         )
         .then((ret) => {
           var todos = [];
           ret.data.filter(todo => {
               if(todo.data.completed === args.completed){
                   todos.push(todo.data)
               }
           });
           return todos;
       })
         .catch((err) => console.error('Error: %s', err))
     }
   },
   Mutation: {
       addTodo(parent, args, context, info) {
           return client.query(
               query.Create(
                 query.Collection('todos'),
                 { data: {
                     title: args.title,
                     completed: args.completed
                   }
                },
               )
             )
             .then((ret) => {return ret.data})
             .catch((err) => console.error('Error: %s', err))
       },
       updateTodo(parent, args, context, info){
           return client.query(
             query.Update(
               query.Select(['ref'], query.Get(query.Match(
                   query.Index("todos_by_title"), args.title
               ))),
               { data: { title: args.title, completed: args.completed } }
             ))
             .then((ret) => {return ret.data})
             .catch((err) => console.error('Error: %s', err))
       },
       deleteTodo(parent, args, context, info){
           return client.query(
               query.Delete(
                 query.Select(['ref'], query.Get(query.Match(
                     query.Index("todos_by_title"), args.title
                 )))
               ))
               .then((ret) => {return ret.data})
               .catch((err) => console.error('Error: %s', err))
        }
   }
 };
```

Run the following query to update the todo item.

```graphql
mutation UpdateTodo {
 updateTodoStatus(title: "Add Mutations", completed: true) {
   title,
   completed
 }
}
```

![Query to update the todo item](//images.ctfassets.net/po4qc9xpmpuh/6AZ3R4v3NMlZV2aUoCGr3q/5754242db15bae1a4d9cfbfdce1b5ad6/Query_to_update_the_todo_item.png)

Verify that the document has been updated in the database.

![Database collection 2](//images.ctfassets.net/po4qc9xpmpuh/3oJqSy88wsYSBuM33vXAGH/204422bb02073b8507d5b51133cf22fb/Database_collection_2.png)

Run the following query to delete the todo item.

```graphql
mutation DeleteTodo {
 deleteTodo(title:"Add Mutations") {
   title,
   completed
 }
}
```

![Mutation delete todo](//images.ctfassets.net/po4qc9xpmpuh/3EnWOL5hEdBOawS9qYrvLX/666a2563cef8cf52362558474864fd7a/Mutation_delete_todo.png)

Verify that the document has been deleted in the database.

![Database collection 3](//images.ctfassets.net/po4qc9xpmpuh/IToH4RWXXnTB3HGwSFNlR/fecb9f8ee97f3e174174cc294f70031b/Database_collection_3.png)

### Interact with your data: Setting up subscriptions
Subscriptions are used to establish a real-time connection with the server. Whenever an event occurs in the server and whenever that event is triggered, the server will send the corresponding data to the client.

Next we will set up  a subscription that publishes a new Todo when it is added with the AddTodo mutation. First install the packages required by running the following:

```bash
> npm install subscriptions-transport-ws @graphql-tools/schema
```

Include the required packages by adding the following import statements

```js
import { ApolloServer, gql } from 'apollo-server-express';
import express from 'express';
import { PubSub } from 'graphql-subscriptions';
import { createServer } from 'http';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { makeExecutableSchema } from '@graphql-tools/schema';
import faunadb from 'faunadb';
 
const pubsub = new PubSub();
const NEW_TODO = "NEW_TODO";
```

Update the addTodo mutation to publish newly added Todo items:

```graphql
addTodo(parent, args, context, info) {
           return client.query(
               query.Create(
                 query.Collection('todos'),
                 { data: {
                     title: args.title,
                     completed: args.completed
                   }
                },
               )
             )
             .then((ret) => {
               pubsub.publish(NEW_TODO, { newTodo: ret.data }); 
               return ret.data})
             .catch((err) => console.error('Error: %s', err))
       }
```

Next, add the update the resolvers to include the subscription:

```graphql
Subscription: {
       todoAdded: {
           subscribe:
      
             () => pubsub.asyncIterator([NEW_TODO]),
           resolve: (payload) => ({
             title: payload.newTodo.title,
             completed: payload.newTodo.completed
           })
       }
   }
```

Update the server config by replacing it with:

```js
const app = express();
 const httpServer = createServer(app);
 
 const schema = makeExecutableSchema({
   typeDefs,
   resolvers,
 });
 
 const server = new ApolloServer({
   schema,
   plugins: [{
     async serverWillStart() {
       return {
         async drainServer() {
           subscriptionServer.close();
         }
       };
     }
   }],
 });
 
 const subscriptionServer = SubscriptionServer.create(
   { schema, execute, subscribe },
   { server: httpServer, path: server.graphqlPath }
 );
 
  await server.start();
 server.applyMiddleware({ app });
 
 const PORT = 4000;
 httpServer.listen(PORT, () =>
   console.log(`Server is now running on http://localhost:${PORT}/graphql`)
 );
```

Save the file and reload the Sandbox Studio page. Run the Subscription to start listening for AddTodo mutations by running the following:

```graphql
subscription Subscription {
 todoAdded {
   title,
   completed
 }
}
```

The Subscriptions tab should open and listening

![Subscription](//images.ctfassets.net/po4qc9xpmpuh/WzDYdVMVZN1jVMr6ZYGpO/a9a2bbee770e269f604f810d3c2e3ecb/Subscription.png)

In a new query tab, run another AddTodo mutation like this:

```graphql
mutation AddTodo {
 addTodo(title: "Test Subscriptions", completed: false) {
   title,
   completed
 }
}
```

The mutation should be executed as well as published to the subscription.

![Publish to subscription](//images.ctfassets.net/po4qc9xpmpuh/1Ebg2sl6zWmoihTGbNO1kD/806b5bff1ce03ae7cfcc1cf294814722/Publish_to_subscription.png)

### Keep learning
[GraphQL](https://graphql.org/) is a new technology, and in this guide, we have only explored a few basics. Here are some additional links you might find useful as you explore GraphQL:

1. How to GraphQL - [https://www.howtographql.com/](https://www.howtographql.com/)
2. The GraphQL Language Spec - [https://github.com/graphql/graphql-spec](https://github.com/graphql/graphql-spec)
3. GraphQL.org - [https://graphql.org/](https://graphql.org/)

## Looking for a serverless database with native GraphQL? 

Fauna is a flexible, developer-friendly, transactional database delivered as a secure and scalable cloud API with native GraphQL. Never again worry about database provisioning, scaling, sharding, replication, or correctness. Fauna has an [extensive hub of GraphQL documentation](https://docs.fauna.com/fauna/current/api/graphql/), ideal for GraphQL novices and experts alike.

**Sign-up for free**
The data API for modern applications is here. Sign-up for free without a credit card and get started instantly.
[Sign-up now](https://dashboard.fauna.com/accounts/register)  

**Quick start guide**
Try our quick start guide to get up and running with your first Fauna database, in only 5 minutes!
[Read more](https://docs.fauna.com/fauna/current/learn/quick_start/gql_quick_start)