Best practices for implementing graphQL based on a joke API example in node.js.
Install the dependencies by running the following command from the terminal in the directory of the project.
$ yarn
Run the server in development on linux
using the following command
$ npm run start
Note: Do not forget to set the following environmental variables of your PostgresQL db before starting the server:
DB_PASSWORD
: The password of the databaseDB_DATABASE
: The name of the database
When running the node server in development Nodemon will be turned on. Nodemon will watch for any file changes in the project and automatically restart the server.
To compile the files run the command:
$ npm run build
Next, start the production server using the command:
$ npm run serve
Note: Do not forget to set the following environmental variables of your PostgresQL db before starting the server:
DB_HOST
: The host address of the databaseDB_USER
: The user of the databaseDB_PASSWORD
: The password of the databaseDB_DATABASE
: The name of the database
The examples queries below assume that the server is running in development on port 3000.
The graphQL API can be accessed by under the path /
. The query
is contained in the query-string
of the request.
http://localhost:3000/?query={query}
In addition, you can also use GraphiQL for
writing queries in an IDE environment. GraphiQL can be accessed using the URL path /graphiql
http://localhost:3000/graphiql