Prisma

Refer to the framework docs for API reference and concepts. What follows here are aspects that are particular to the schema plugin only.

Installation

$npm add nexus-plugin-prisma

Usage

  1. Import nexusSchemaPrisma from nexus-plugin-prisma
  2. Create and configure it if needed (usually not)
  3. Pass into Nexus.makeSchema plugins array

Note: If you're looking for CRUD capabilities, you must enable the experimentalCRUD option.

You can find runnable examples in the repo examples folder.

Example

1import { nexusSchemaPrisma } from 'nexus-plugin-prisma/schema'
2import { makeSchema } from '@nexus/schema'
3import * as types from './types'
4
5const schema = makeSchema({
6 types,
7 plugins: [nexusSchemaPrisma()],
8})

Configuration

Note that, In most cases, you should not need to configure anything.

1type Options = {
2 /**
3 * Enable experimental CRUD capabilities.
4 * Add a `t.crud` method in your definition block to generate CRUD resolvers in your `Query` and `Mutation` GraphQL Object Type.
5 *
6 * @default false
7 */
8
9 experimentalCRUD?: boolean
10 /**
11 * nexus-plugin-prisma will call this to get a reference to an instance of the Prisma Client.
12 * The function is passed the context object. Typically a Prisma Client instance will
13 * be available on the context to support your custom resolvers. Therefore the
14 * default getter returns `ctx.prisma`.
15 */
16 prismaClient?: PrismaClientFetcher
17 /**
18 * Same purpose as for that used in `NexusSchema.makeSchema`. Follows the same rules
19 * and permits the same environment variables.
20 */
21 shouldGenerateArtifacts?: boolean
22
23 inputs?: {
24 /**
25 * What is the path to the Prisma Client package? By default looks in
26 * `node_modules/@prisma/client`. This is needed in order to read your Prisma
27 * schema AST and Prisma Client CRUD info from the generated Prisma Client package.
28 */
29 prismaClient?: string
30 }
31 outputs?: {
32 /**
33 * Where should typegen be put on disk? By default emits into `node_modules/@types`.
34 */
35 typegen?: string
36 }
37 computedInputs?: GlobalComputedInputs
38}

Project Setup

These are tips to help you with a successful project workflow

Introduce a bundle step for serverless

The Prisma plugin bundles its Prisma dependencies. These are fairly large dependencies which will lead to over 100mb in your node_modules. The reason these dependencies are present is to support the framework version of the Prisma plugin.

This large amount of disk usage can pose a problem for serverless environments like AWS Lambda where there are lower deployment size limits than say on Heroku. If you encounter this issue consider introducing a bundling step into your build. The primary dependency you will want to shake out is @prisma/cli. Some tools you can try include: ncc, esbuild, spack. If you are using Serverless Framework then checkout the package.excludes config. Example of a developer using it to solve this issue here.

Keep your schema runnable in isolation

Keep app schema somewhere apart from server so that you can do ts-node --transpile-only path/to/schema/module to generate typegen. This will come in handy in certain deployment contexts.

Helpful NPM scripts

Consider using something like the following set of npm scripts. The postinstall step is helpful for guarding against pruning since the generated @types packages will be seen as extraneous. We have an idea to solve this with package facades. For yarn users though this would still be helpful since yarn rebuilds all packages whenever the dependency tree changes in any way (issue). The NODE_ENV=development is needed to ensure typegen is run even in a context where NODE_ENV is set to production (like a heroku deploy pipeline, see next point). Also, consider using cross-env for better compatibility with different environments, e.g. on Windows.

1{
2 "scripts": {
3 "generate:prisma": "prisma generate",
4 "generate:nexus": "cross-env NODE_ENV=development ts-node --transpile-only path/to/schema/module",
5 "generate": "npm -s run generate:prisma && npm -s run generate:nexus",
6 "postinstall": "npm -s run generate"
7 }
8}

Integrate generate into build step

In your deployment pipeline you may wish to run a build step. Heroku buildpacks for example call npm run build if that script is defined in your package.json. If this is your case and you are a TypeScript user consider a build setup as follows. Prior to tsc we run artifact generation so that TypeScript will have types for the all the resolver signatures etc. of your app.

1{
2 "scripts": {
3 "build": "npm -s run generate && tsc"
4 }
5}

Legacy Note

If you are still using nexus-prisma@0.3 / Prisma 1 you can find the old docs here. Please note that nexus-prisma@0.3 is only compatible with nexus up to 0.12.0-beta.14 (issue). We do not currently have plans to update nexus-prisma@0.3 to become compatible with versions of nexus newer than that.

Edit this page on Github