Creating our first GraphQL server
For this recipe, we are going to create a contacts list in which we will save the name, phone, and email addresses of our friends.
Getting ready
The first thing we need to do is to create a directory for our project and initialize a new package.json
file installing express
, graphql
, and express-graphql
:
mkdir contacts-graphql cd contacts-graphql npm init --yes npm install express graphql express-graphql babel-preset-env npm install -g babel-cli
We need to install babel-preset-env
and babel-cli
to use ES6 syntax in Node. Also, we need to create a .babelrc
file :
{
"presets": ["env"]
}
File: .babelrc
How to do it...
Let's create our first GraphQL server:
- First, we need to create an
index.js
file for our Express server:
import express from 'express'; const app = express(); app.listen(3000, () => console.log('Running server on port 3000'));
File: index.js
- If you run
babel-node index.js
in your terminal, you should be able to see the node server...