|
1 | 1 | # GraphQL Authentication Prisma
|
2 | 2 |
|
3 |
| -TODO |
| 3 | +A Prisma adapter for [Graphql Authentication](https://github.com/Volst/graphql-authentication/blob/master/README.md). |
| 4 | + |
| 5 | +# Install |
| 6 | + |
| 7 | +Node v8+ should be used. Install with Yarn or npm: |
| 8 | + |
| 9 | +``` |
| 10 | +yarn add graphql-authentication graphql-authentication-prisma email-templates |
| 11 | +npm i graphql-authentication graphql-authentication-prisma email-templates |
| 12 | +``` |
| 13 | + |
| 14 | +# Usage with Prisma |
| 15 | + |
| 16 | +## Step 1 |
| 17 | + |
| 18 | +In your Prisma `datamodel.graphql` file, add this [User model](./example/datamodel.graphql). |
| 19 | + |
| 20 | +## Step 2 |
| 21 | + |
| 22 | +In your `schema.graphql` for your own server, add something like the following (you can also import specific endpoints only): |
| 23 | + |
| 24 | +```graphql |
| 25 | +# import Query.*, Mutation.* from "node_modules/graphql-authentication/schema.graphql" |
| 26 | +``` |
| 27 | + |
| 28 | +## Step 3 |
| 29 | + |
| 30 | +In your server we now need to map these types to resolvers and pass in some options. The following example uses [graphql-yoga](https://github.com/graphcool/graphql-yoga/), but it should also work with Apollo Server. |
| 31 | + |
| 32 | +```js |
| 33 | +import { authQueries, authMutations, graphqlAuthenticationConfig } from 'graphql-authentication'; |
| 34 | +import { GraphqlAuthenticationPrismaAdapter } from 'graphql-authentication-prisma'; |
| 35 | +import * as Email from 'email-templates'; |
| 36 | + |
| 37 | +const resolvers = { |
| 38 | + Query: { |
| 39 | + ...authQueries |
| 40 | + }, |
| 41 | + Mutation: { |
| 42 | + ...authMutations |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +const server = new GraphQLServer({ |
| 47 | + typeDefs: './schema.graphql', |
| 48 | + resolvers, |
| 49 | + context: req => ({ |
| 50 | + ...req, |
| 51 | + db: new Prisma({...}), |
| 52 | + graphqlAuthentication: graphqlAuthenticationConfig({ |
| 53 | + adapter: new GraphqlAuthenticationPrismaAdapter(), |
| 54 | + // Required, used for signing JWT tokens |
| 55 | + secret: 'wheredidthesodago', |
| 56 | + // Optional, for sending emails with email-templates (https://www.npmjs.com/package/email-templates) |
| 57 | + mailer: new Email(), |
| 58 | + // Optional, the URL to your frontend which is used in emails |
| 59 | + mailAppUrl: 'http://example.com', |
| 60 | + }) |
| 61 | + }) |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +## Step 4 |
| 66 | + |
| 67 | +Lastly, if you want to send emails, you should copy the email templates to your own project. Checkout [the example email templates](./example/emails). |
| 68 | + |
| 69 | +### [Full Documentation](https://github.com/Volst/graphql-authentication/blob/master/README.md#documentation) |
0 commit comments