Skip to content

node graphql firestore api tutorial

Kamau.Brian edited this page Feb 22, 2019 · 11 revisions

Introduction

This tutorial tries to elaborate, "How to create a graphQL-API with node js".

Grapqhl is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. Documentation for the same can be found here

Prerequisites

We shall use the following resources for the purposes of this tutorial. It would be helpful if you are conversant with them.

This is a node-js based tutorial and therefore prior knowledge of it is helpful 😊

Getting Started ✊

We need to setup a node project using node's package manager, therefore create a directory and cd into it.

* mkdir node-graphql-tutorial

* cd node-graphql-tutorial

* then run npm init

kamau-brian@mtotodev:~/WebstormProjects/node-grapqhl$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (grapqhl) grapqhl
version: (1.0.0) 1.0.0
description: this is a graphql tutorial
entry point: (index.js) index.js
test command: 
git repository: push your code to a git repository
keywords: node, grapqhl
author: Write your name here
license: (ISC) ISC
About to write to /home/kamau-brian/WebstormProjects/node-grapqhl/package.json:

{
  "name": "grapqhl",
  "version": "1.0.0",
  "description": "this is a graphql tutorial",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "push your code to a git repository"
  },
  "keywords": [
    "node",
    "grapqhl"
  ],
  "author": "Write your name here",
  "license": "ISC"
}


Is this OK? (yes) yes
 

Installing project dependencies

Run the following:

npm install --save express express-graphql body-parser cors firebase-admin firebase-functions graphql morgan graphql

Note after successfully running that command, changes to the dependencies node on the package.json file

Project Structure.

For the purposes of this tutorial, use the following structure for your project packages.,Do not worry about the code for now, it shall be covered later in the tutorial.

node-grapqhl
├── api
│   ├── config
│   │   └── service-account.json
│   ├── grapqhl
│   │   ├── resolvers
│   │   │   ├── index.js
│   │   │   └── PostResolver.js
│   │   └── schemas
│   │       └── Post.js
│   ├── model
│   │   └── database.js
│   ├── routes
│   │   └── index.js
│   └── test
│       └── index.js
├── app.js
├── index.js
├── package.json
├── package-lock.json
└── server.js

Clone this wiki locally