Skip to content

Commit cbbc608

Browse files
Added Travis CI and added flow implementation to tutorial (#7)
* Added flow implementation to the tutorial * Added Travis CI integration * Added Travis CI support * Removed the ‘|| exit 0’ from the test and check script so that it correctly works with Travis CI * Fixed typo in mochaSetup in package.json * Added babel-polyfill to make project work with node.js 4 * Updated license tag in package.json * Update copyright notice at the top of files * Update the author field * Added the Travis CI Build status to the README
1 parent 72aa291 commit cbbc608

File tree

8 files changed

+132
-18
lines changed

8 files changed

+132
-18
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
3+
node_js:
4+
- "6"
5+
- "5"
6+
- "4"
7+
8+
script: npm run travis

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Example Node server with a GraphQL API
22

3+
[![Build Status](https://travis-ci.org/HiThereCommunity/example-graphql-server.svg?branch=master)](https://travis-ci.org/HiThereCommunity/example-graphql-server)
4+
35
This repository contains an example of a [Node.js](https://nodejs.org/en/) server
46
that exposes a [GraphQL](http://graphql.org/) API.
57

docs/tutorial.md

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
So you want to implement a [graphQL](http://graphql.org/) api on node.js but are unsure where to begin? Then keep reading:)
44

5+
In this document I want to share what I have learned about setting up a node.js project that contains a graphQL API.
6+
57
In this tutorial I want to share what I have learned about setting up a node.js project that contains a graphQL API.
68

79
More specifically, I want to explain how to:
810

911
- Set up a simple `Hello, world` graphQL API in node.js
1012
- Create a workflow for developing, testing and deploying your node.js codebase.
1113

12-
This turorial is based on a node.js GraphQL starter project on [Github](https://github.com/HiThereCommunity/example-graphql-server).
14+
> Note, this tutorial is based on the source code in this repository but does not follow it completely...
1315
1416
I have used this starter project several times and it has allowed me to develop an API relatively quickly. In addition, the workflow has lowered my development time and increased the stability of my codebase.
1517

@@ -201,7 +203,7 @@ For more information on how to use `graphql-js` to build up a schema, I urge you
201203

202204
The final step is to link the schema (in `src/GraphQL/index.js`) to an endpoint.
203205

204-
Open `src/index.js` and import the schema from `./GraphQl`. Next, we will make the schema available using `express-graphql` under the path `/`. Make sure to set the option `graphiql` to yes. This will allow us to play with our graphQL schema using [graphiQL](https://github.com/graphql/graphiql)
206+
Open `src/index.js` and import the schema from `./GraphQL`. Next, we will make the schema available using `express-graphql` under the path `/`. Make sure to set the option `graphiql` to yes. This will allow us to play with our graphQL schema using [graphiQL](https://github.com/graphql/graphiql)
205207

206208
```js
207209
import express from 'express';
@@ -282,7 +284,7 @@ Like the previous sections we will again write a npm script to run all our tests
282284
```diff
283285
"scripts": {
284286
"start": "nodemon src/index.js --exec babel-node",
285-
+ "test": "mocha --require resources/MochaSetup src/**/__tests__/**/*.js || exit 0"
287+
+ "test": "mocha --require resources/mochaSetup src/**/__tests__/**/*.js"
286288
}
287289
```
288290

@@ -346,6 +348,87 @@ This should output a message indicating that the test has passed.
346348

347349
This testing workflow makes it really easy to write tests for your node.js code (and by extension your graphQL layer). As you extend your GraphQL layer you can create new test files or extend `testHello` with additional tests to make sure that your code is functioning as expected.
348350

351+
## Add [flow](https://flowtype.org/) to project
352+
353+
[Flow](https://flowtype.org/) is a static type checker for javascript that helps in finding bugs without actually running your code. This is very useful and helps tremendously in writing stable code.
354+
355+
Implementing Flow is easy! All we have to do is install it as a dependency:
356+
357+
```
358+
$ npm install --save-dev flow-bin
359+
```
360+
361+
In Flow you can add [type annotations](https://flowtype.org/docs/type-annotations.html#_) to your javascript to add type safety to the code, however this is not standard javascript so we need to tell babel to automatically remove these types. This is done by adding a babel plugin:
362+
363+
```
364+
$ npm install --save-dev babel-plugin-transform-flow-strip-types
365+
```
366+
367+
Next, update the `.babelrc` with the plugin
368+
369+
```diff
370+
{
371+
"presets": ["es2015", "stage-2"],
372+
+ "plugins": ["transform-flow-strip-types"]
373+
}
374+
```
375+
376+
The next step is to create a simple script for performing the flow check. Add the following script to the `package.json` file:
377+
378+
```diff
379+
"scripts": {
380+
"start": "nodemon src/index.js --exec babel-node",
381+
"test": "mocha --require resources/mochaSetup src/**/__tests__/**/*.js",
382+
+ "check": "flow check"
383+
}
384+
```
385+
386+
Finally, we need to add a `.flowconfig` file that contains the [configurations](https://flowtype.org/docs/advanced-configuration.html). This file can be generated by running the command:
387+
388+
```
389+
$ flow init
390+
```
391+
392+
A `.flowconfig` should appear at the root of your project.
393+
394+
We have now successfully implemented Flow into the project and we can begin using it. Make sure to add `// @flow` to the top of any file that we want to be checked with Flow.
395+
396+
Let's add a type annotation to our GraphQL schema in `src/graphQL/index.js`. In the code below we have added `// @flow` to the top of the file and added the type annotation `?string` to the resolve function of the `hello` query indicating that this resolve function should return a string or null.
397+
398+
```js
399+
// @flow
400+
401+
import {
402+
GraphQLString,
403+
GraphQLObjectType,
404+
GraphQLSchema,
405+
} from 'graphql';
406+
407+
export default new GraphQLSchema({
408+
query: new GraphQLObjectType({
409+
name: 'Queries',
410+
description: "All the queries that can be used to retrieve data",
411+
fields: () => ({
412+
hello: {
413+
type: GraphQLString,
414+
description: "Hello, world",
415+
resolve: (): ?string => "hello, world"
416+
}
417+
})
418+
})
419+
});
420+
```
421+
422+
Run the flow check command
423+
424+
```
425+
$ npm run check
426+
```
427+
428+
Flow will check your file for any type errors. Flow should indicate that it has not found any type errors: `Found 0 errors`.
429+
430+
Simply add `// @flow` to any other files that you wish to be checked by flow.
431+
349432
# Deploying to production
350433

351434
In the development workflow we use `babel-node` for compiling javascript and starting the server. However, Babel explicitly [states](https://babeljs.io/docs/usage/cli/) that this should not be used for running node.js because it is very heavy to run and will add unnecessary overhead to the server.
@@ -363,7 +446,8 @@ For this we will write a new `build` script in the `package.json` file:
363446
```diff
364447
"scripts": {
365448
"start": "nodemon src/index.js --exec babel-node",
366-
"test": "mocha --require resources/MochaSetup src/**/__tests__/**/*.js || exit 0"
449+
"test": "mocha --require resources/mochaSetup src/**/__tests__/**/*.js",
450+
"check": "flow check",
367451
+ "build": "babel src --ignore __tests__ --out-dir dist/"
368452
}
369453
```
@@ -383,7 +467,8 @@ Finally, we need to start the compiled production server code. For this we will
383467
```diff
384468
"scripts": {
385469
"start": "nodemon src/index.js --exec babel-node",
386-
"test": "mocha --require resources/MochaSetup src/**/__tests__/**/*.js || exit 0"
470+
"test": "mocha --require resources/mochaSetup src/**/__tests__/**/*.js",
471+
"check": "flow check",
387472
"build": "babel src --out-dir dist/",
388473
+ "serve": "babel src --ignore __tests__ --out-dir dist/"
389474
}

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"start": "nodemon src/index.js --exec babel-node",
88
"build": "babel src --ignore __tests__ --out-dir dist/",
99
"serve": "node dist/index.js",
10-
"test": "mocha --require resources/MochaSetup src/**/__tests__/**/*.js || exit 0",
11-
"check": "flow check || exit 0"
10+
"test": "mocha --require resources/mochaSetup src/**/__tests__/**/*.js",
11+
"check": "flow check",
12+
"travis": "npm run check && npm run test"
1213
},
1314
"engines": {
1415
"node": ">=5.3.0",
@@ -24,11 +25,8 @@
2425
"API",
2526
"node.js"
2627
],
27-
"contributors": [
28-
"Dirk-Jan Rutten <dirk-jan.rutten@hi-there.community>",
29-
"Florentijn Hogerwerf <florentijn.hogerwerf@hi-there.community>"
30-
],
31-
"license": "ISC",
28+
"author": "Dirk-Jan Rutten <dirkjanrutten@gmail.com>",
29+
"license": "MIT",
3230
"bugs": {
3331
"url": "https://github.com/HiThereCommunity/example-graphql-server/issues"
3432
},

resources/mochaSetup.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/**
2-
* Created by dirk-janrutten on 18/06/16.
2+
* Copyright (c) 2016, Dirk-Jan Rutten
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
38
*/
49

510
/** Required for transpiling Mocha test code using babel **/

src/graphQL/__tests__/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/**
2-
* Created by Dirk-Jan Rutten on 18/06/16.
2+
* Copyright (c) 2016, Dirk-Jan Rutten
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
38
*/
49

510
import {

src/graphQL/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// @flow
2-
32
/**
4-
* Created by Dirk-Jan Rutten on 18/06/16.
3+
* Copyright (c) 2016, Dirk-Jan Rutten
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*
59
*/
610

711
import {
@@ -18,7 +22,7 @@ export default new GraphQLSchema({
1822
hello: {
1923
type: GraphQLString,
2024
description: "Hello, world",
21-
resolve: () => "hello, world"
25+
resolve: (): ?string => "hello, world"
2226
}
2327
})
2428
})

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// @flow
2-
"use strict";
2+
/**
3+
* Copyright (c) 2016, Dirk-Jan Rutten
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*
9+
*/
310

411
import "babel-polyfill";
512

0 commit comments

Comments
 (0)