You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: docs/tutorial.md
+90-5Lines changed: 90 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,16 @@
2
2
3
3
So you want to implement a [graphQL](http://graphql.org/) api on node.js but are unsure where to begin? Then keep reading:)
4
4
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
+
5
7
In this tutorial I want to share what I have learned about setting up a node.js project that contains a graphQL API.
6
8
7
9
More specifically, I want to explain how to:
8
10
9
11
- Set up a simple `Hello, world` graphQL API in node.js
10
12
- Create a workflow for developing, testing and deploying your node.js codebase.
11
13
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...
13
15
14
16
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.
15
17
@@ -201,7 +203,7 @@ For more information on how to use `graphql-js` to build up a schema, I urge you
201
203
202
204
The final step is to link the schema (in `src/GraphQL/index.js`) to an endpoint.
203
205
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)
205
207
206
208
```js
207
209
importexpressfrom'express';
@@ -282,7 +284,7 @@ Like the previous sections we will again write a npm script to run all our tests
@@ -346,6 +348,87 @@ This should output a message indicating that the test has passed.
346
348
347
349
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.
348
350
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:
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
+
exportdefaultnewGraphQLSchema({
408
+
query:newGraphQLObjectType({
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
+
349
432
# Deploying to production
350
433
351
434
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:
0 commit comments