This repository contains a starter project for creating a Node.js server that exposes a GraphQL API.
- GraphQL API already setup
- Simple development workflow
- Testing suite
- Simple deployment to production
Install the dependencies by running the following command from the terminal in the directory of the project.
npm installRun the server in development using the following command
npm run startThis will start the node server contained in the folder
/src.
When running the server in development a file watcher is turned on that performs 3 checks whenever a file contained in the /src folder is changed.
- Restart the server so that any changes are directly loaded
- Run the linter standard.
- Run flow static type checker. Add
// @flowat top of any file that you want flow to check.
The server runs on port 3000 in development
Start the server in production on port 80 using the command:
npm run serveA folder
/distwill appear. This contains the code on which the production server runs.Forever is used to run the node server in
/distcontinuously in the background.
In order to stop the server run:
npm run stopThe graphQL API can be accessed by under the path /. The query is contained in the query-string
of the request.
/?query={hello}
In addition, you can also use GraphiQL for
writing queries in an IDE environment. GraphiQL can be accessed using the URL path /graphiql
/graphiql
Any errors that occurs during the execution of a graphql query are contained in the response object under the key "errors".
The structure of the error object differs depending on the environment that the server is run in: production or development.
When running the server in development the error object has 3 properties:
"message": An explanation of the error that occurred"stack": The stacktrace of the error. This is very useful when debugging!"locations": An object that contains thelineand thecolumnassociated with the graphql query where the execution error occurred.
{
"errors": [{
"message": string,
"stack": [ string ],
"locations": [{
"line": number,
"column": number
}]
}]
}Example: graphql error in development
{
"errors": [
{
"message": "Some error occurred",
"stack": [
"Error: Unable to access database",
" at resolve (index.js:42:17)",
" at resolveOrError (/node_modules/graphql/execution/execute.js:447:12)",
" at resolveField (/node_modules/graphql/execution/execute.js:438:16)",
" at /node_modules/graphql/execution/execute.js:245:18",
" at Array.reduce (native)",
" at executeFields (/node_modules/graphql/execution/execute.js:242:42)"
],
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}When running the server in production the error object only contains the error "message".
When running the server in production any errors that occur during the execution of the graphql query will be shown in the response error object that is found under the key "errors" in the graphql response.
The array of object for the key "errors" contains all the errors that have occurred.
{
"errors": [{
"message": string
}]
}Example: graphql error in production
{
"errors":[
{
"message": "Some error occurred"
}
]
}All the tests are performed using mocha and assertions are made using chai. Tests are located in folders
under the name __tests__.
Note, mocha has been setup to import modules contained in the /resources directory.
Run the mocha tests using the command
npm run test##Support
This server runs on a linux environment, windows is not supported.