generator-node-graphql
v2.0.0
Published
Generates a node project with graphql, mongoose and JWT auth
Downloads
9
Maintainers
Readme
generator-node-graphql
generates a node project with graphql and JWT
Installation
First, install Yeoman and generator-node-graphql using npm (we assume you have pre-installed node.js).
npm install -g yo
npm install -g generator-node-graphql
Then generate your new project:
yo node-graphql
You can also pass in some arguments or options as well
yo node-graphql --default
# Will generate a project with all question reverting to their default values, this will not prompt for any questions
yo node-graphql dog
# Will take the given string an use it as a parent folder name to place all code into, in this case, 'dog'
Generated code
If all default options are entered, the code generated should include a few runnable tests, as well as a runnable server.
Tests
The generated tests are written using Jest and provide an in memory database using mongodb-memory-server, as well as means to test any graphql resolvers and schema validations using easygraphql-tester.
The tests can be run as soon as the code is generated with:
npm run test
And a coverage report by:
npm run coverage
Source Code
The source code uses apollo-server-express along with express for graphql and the server, with the jsonwebtoken package to handle JWTs. It also uses @graphql-tools/merge and graphql-import-node for the graphql implementation, as well as dotenv for its environment files.
The server can be run with nodemon by:
npm run dev
Database
By default the chosen database is noSQL, which uses the mongoose package (keep in mind noSQL is currently the only option you can use). While the tests can run fine without any setup here, to properly run the server an instance of mongoDB should be created for the system to connect to. Please check the .env
file and place your database credentials there.
What the code can do
Out of the box, if you have already set up a database, run the server and navigate to localhost:<PORT>/graphql
Out of the box, the system provides methods for creating a user, logging in and checking the logged in user (all methods using JWT for auth). If you navigate to the user.graphql
file, you can see the provided mutations. Feel free to extend this here for your own system. But for now, if we want to create a user, we can simply enter the following in graphql playground
mutation {
signup(username: "user", email: "[email protected]", password: "password")
}
Which will create a new user and return us a token.
From here we can either use that token or test the login, which we can do like this:
mutation {
login(email: "[email protected]", password: "password")
}
The above also returns us a token. So now, let's verify and check who is currently logged in with our token.
In the HTTP HEADERS
section in playground, enter the following
{
"Authorization": "<YOUR TOKEN THAT WAS RETURNED>"
}
With the following query:
query {
loggedInUser {
id
email
password
}
}
And when run, we should get information about the user we logged into / created earlier.
Directory Structure
The generated directory should look like this:
License
MIT © Hudson Cassidy