This example shows how to implement a GraphQL server with an email-password-based auth, based on Prisma, apollo-server, graphql-shield & Nexus Schema via the Nexus Prisma plugin.
Install Node dependencies:
yarn
(recommended) or npm install
This uses a simple SQLite database.
Note: You can delete the migrations folder to create your own new migrations
To set up your database, run:
yarn db:save
yarn db:migrate
You can now use the SQLite Browser to view and edit your data in the ./prisma/dev.db
file that was created when you ran yarn db:migrate
.
Run the following command to generate Prisma Client:
yarn generate:prisma
Now you can seed your database using the seed
script from package.json
:
yarn seed
Launch your GraphQL server with this command:
yarn dev
Navigate to http://localhost:4002 in your browser to explore the API of your GraphQL server in a GraphQL Playground.
The schema that specifies the API operations of your GraphQL server is defined in ./src/generated/schema.graphql
. Below are a number of operations that you can send to the API using the GraphQL Playground.
Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.
query {
feed {
id
title
content
published
author {
id
name
email
}
}
}
See more API operations
You can send the following mutation in the Playground to sign up a new user and retrieve an authentication token for them:
mutation {
signup(name: "Alice", email: "[email protected]", password: "graphql") {
token
}
}
This mutation will log in an existing user by requesting a new authentication token for them:
mutation {
login(email: "[email protected]", password: "graphql") {
token
}
}
For this query, you need to make sure a valid authentication token is sent along with the Bearer
-prefix in the Authorization
header of the request:
{
"Authorization": "Bearer __YOUR_TOKEN__"
}
With a real token, this looks similar to this:
{
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjanAydHJyczFmczE1MGEwM3kxaWl6c285IiwiaWF0IjoxNTQzNTA5NjY1fQ.Vx6ad6DuXA0FSQVyaIngOHYVzjKwbwq45flQslnqX04"
}
Inside the Playground, you can set HTTP headers in the bottom-left corner:
Once you've set the header, you can send the following query to check whether the token is valid:
{
me {
id
name
email
}
}
To make changes to the GraphQL schema, you need to manipulate the files in the resolvers folder.
Run yarn test
or npm run test
to run tests via Jest in the tests folder.
- Check out the Prisma docs