Spin up your own local GraphQL server within 15 min
Tags: beginners, graphql, postgraphile
Published: 2019-09-14 17:29:37

---
title: Spin up your own local GraphQL server within 15 min
published: true
tags: beginner, GraphQL, PostGraphile
canonical_url: https://techformist.com/spin-up-your-own-local-graphql-server-within-15-min/
---

Create your own GraphQL APIs in 15 min flat using a Postgres database and a bit of help from PostGraphile. No GraphQL server configuration needed.

No frontend beginner developer ever said - "I want to play around with servers to understand their internals byheart before I venture forth with my beautiful Vue application".

If you are building on REST APIs, you have public services like [JSONPlaceholder](https://jsonplaceholder.typicode.com/). These services provide quick access to much-loved sample APIs for to-do, posts, users etc.

There are not a lot of choices if you are building on GraphQL. There have been efforts towards providing such an API in the past but none last long. The solution - create them locally.

Local GraphQL can be cumbersome.

When you start, one may be left wondering on Express + GraphQL + a bit of configuration + write more than few lines of code to get started to serve any new API. There are tools like [json-graphql-server](https://github.com/marmelab/json-graphql-server) that claim to speed up this process. However, they are not kept up to date and seemingly are not meant for Windows(?).

So, I did the next logical thing that was easy and quick. Create an environment of your own using [PostgreSQL](https://www.postgresql.org/) and [PostGraphile](https://www.graphile.org/postgraphile/).

### Initial Setup

1. [Download and install PostgreSQL](https://www.postgresql.org/download/). <br>Alternatively use [Laragon with Postgres](https://laragon.org/download/) - it is a really good control centre for local development. <br><br>

1. Open pgAdmin - the DB admin application for Postgres. Use an existing database or create a new one. Right-click on Schema > Tables and click on 'New Table'. I will assume that you will use a database called 'test'. Create a table called 'todo'.
   ![postgres-create-table](https://thepracticaldev.s3.amazonaws.com/i/r1zgwqkey67dq4226gnd.png) <br><br>

1. Create a few columns
   ![pgadmin-create-table-columns](https://thepracticaldev.s3.amazonaws.com/i/vct03eyk1haeei1ksgb6.png) <br><br>

1. Start your very own GraphQL server. The server uses port 5000 by default, but we will change it to 5050 for the heck of it (also because 5000 is used by a few other front-end apps)
   ```cmd
   npx postgraphile -c test --watch -p 5050
   ```
   ![postgraphile-server-start](https://thepracticaldev.s3.amazonaws.com/i/x36h4bmkwlb2jjk3pvb7.png)

### Start playing around

Go to [http://localhost:5050/graphiql]. Copy/paste the GraphQL statements to perform different operations.

##### Query data

Query for data. Of course, you will not find anything at this time.

```graphql
{
  allTodos {
    nodes {
      id
      title
      done
    }
  }
}
```

The output will be -

```js
{
  "data": {
    "allTodos": {
      "nodes": []
    }
  }
}
```

![graphiql-graphql-server-simple](https://thepracticaldev.s3.amazonaws.com/i/kheuy9t4ij6rieeycqe1.png)

##### Insert data

Insert 'to-do' data in your database.

```graphql
mutation {
  createTodo(input: { todo: { title: "Create GraphQL server", done: true } }) {
    todo {
      id
      title
      done
    }
  }
}
```

This statement will create a single to-do, query back `id`, `title`, & `done` fields, and display them at the right-hand side of the screen.

![graphiql-graphql-mutation](https://thepracticaldev.s3.amazonaws.com/i/46zxspfw0x8a1qv7phuq.png)

Output -

```js
{
  "data": {
    "createTodo": {
      "todo": {
        "id": 10,
        "title": "Create GraphQL server",
        "done": true
      }
    }
  }
}

```

You can repeat the `query` operation and find that it can display data too!

### Configure additional APIs

By now you have a full-fledged server that is serving to-dos. You can add filters, do magic on inserts/updates using functions, and eventually find that the world is the limit.

But, what about additional APIs? Similar to to-dos, you can start creating entities and find them available to your GraphQL without any additional effort. That is the magic of PostGraphile.

### Why use this setup for local GraphQL environment?

- Quicker setup. Type in a few commands + configure DB table - and you are done
- Minimal effort to setup new APIs - you are not limited to standard APIs but can go all wild
- You can take this to your production environment too. I have used Postgraphile for projects and it rocks!
- PostGraphile uses your friendly, neighbourhood Javascript. So we are right at home

### You say?

What do you say about this folks? Super..? Stupid? Somewhere in between? 
Or, simply "what am I doing on this planet when people like this exist"..?


> Originally published at https://techformist.com/spin-up-your-own-local-graphql-server-within-15-min/