/nexus-auth

A sample auth module with Prisma 2 and Nexus

Primary LanguageTypeScript

GraphQL Server with Authentication Prisma 2 and Nexus Schema

Build Status

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.

How to use

1. Clone this repo & install dependencies

Install Node dependencies:

yarn (recommended) or npm install

2. Set up the database

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.

3. Generate Prisma Client (type-safe database client)

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

4. Start the GraphQL server

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.

5. Using the GraphQL API

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.

Retrieve all published posts and their authors

query {
  feed {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}
See more API operations

Register a new user

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: "alice@prisma.io", password: "graphql") {
    token
  }
}

Log in an existing user

This mutation will log in an existing user by requesting a new authentication token for them:

mutation {
  login(email: "alice@prisma.io", password: "graphql") {
    token
  }
}

Check whether a user is currently logged in with the me query

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
  }
}

6. Changing the GraphQL schema

To make changes to the GraphQL schema, you need to manipulate the files in the resolvers folder.

Next steps

Testing

Run yarn test or npm run test to run tests via Jest in the tests folder.