Adoption Guides

Nexus Framework Prisma Users

Hello Nexus Framework Prisma User!

If you're looking to migrate from the Nexus Framework over to Nexus and you're using the Prisma plugin, this guide is for you.

This guide assumes you've already followed the Framework Users adoption Guide.

Dependencies

You will need to install the Prisma dependencies yourself now.

1npm install -D @prisma/cli
2npm install @prisma/client

You must also make sure that you are using the version 0.20 or later of nexus-plugin-prisma.

Enabling the plugin

You will now need to work with the makeSchema API of nexus directly.

-import { use } from 'nexus'
-import { prisma } from 'nexus-plugin-prisma'
+import { makeSchema } from 'nexus'
+import { nexusPrisma } from 'nexus-plugin-prisma'
5
-use(prisma())
7
+export const schema = makeSchema({
+ plugins: [nexusPrisma()]
+})

Configuring the plugin

There are a number of defaults that the framework used to provide for you. Here's how you can configure the schema plugin to behave the same.

Enabling CRUD capabilities

If you were using t.crud with the framework, you must also explicitly enable it with the schema plugin

1import { makeSchema } from 'nexus'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4makeSchema({
5 plugins: [nexusPrisma({
+ experimentalCRUD: true
7 })]
8})

Configuring Backing types

To let nexus know of the Prisma Client types so that, for instance, your resolvers expect the right types, you must point makeSchema to the Prisma Client where all the types are exported.

1import { makeSchema } from 'nexus'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4export const schema = makeSchema({
+ sourceTypes: {
+ modules: [
+ {
+ module: require.resolve('.prisma/client/index.d.ts'),
+ alias: "prisma",
+ }
+ ]
+ }
13 plugins: [
14 nexusPrisma({
15 experimentalCRUD: true
16 })
17 ]
18})

Configuring Context Type

The framework used to automatically inject and type your context so that an instance of the client would be there for you. We now need to configure that manually. To do so, we'll first create a context.ts file where we'll export a type containing the Prisma Client.

1// context.ts
2import { PrismaClient } from '@prisma/client'
3
4export type Context = {
5 db: PrismaClient
6}

Then, we'll go back to the file where we're calling makeSchema, and add the following configuration

1import { makeSchema } from 'nexus'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4export const schema = makeSchema({
5 sourceTypes: {
6 modules: [
7 {
8 source: require.resolve('.prisma/client/index.d.ts'),
9 alias: "prisma",
10 },
11 ],
+ contextType: {
+ source: require.resolve('./context'),
+ alias: 'ContextModule'
+ }
16 }
17 plugins: [
18 nexusPrisma({
19 experimentalCRUD: true
20 })
21 ]
22})

Configuring Custom Scalars

If your Prisma Schema is using either the Json or DateTime type, the framework used to provide some GraphQL scalars to map these. With nexus, you will need to install and do the mapping yourself.

  1. Start by installing the following dependency

    1npm install graphql-scalars
    2```xf
    3
  2. Then, add the following configuration property to the Prisma plugin

    1import { makeSchema } from 'nexus'
    2import { nexusPrisma } from 'nexus-plugin-prisma'
    +import { DateTimeResolver, JSONObjectResolver } from 'graphql-scalars'
    4
    5export const schema = makeSchema({
    6 sourceTypes: {
    7 modules: [
    8 {
    9 module: require.resolve('.prisma/client/index.d.ts'),
    10 alias: "prisma",
    11 },
    12 ],
    13 contextType: {
    14 module: require.resolve('./context'),
    15 alias: 'ContextModule'
    16 }
    17 }
    18 plugins: [
    19 nexusPrisma({
    20 experimentalCRUD: true
    + scalars: {
    + DateTime: DateTimeResolver,
    + Json: new GraphQLScalarType({
    + ...JSONObjectResolver,
    + name: 'Json',
    + description: 'The `JSON` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).',
    + })
    + }
    29 })
    30 ]
    31})

Using the plugin

Once the plugin is enabled and properly configured, none of your actual code usage of nexus-plugin-prisma (like t.model or t.crud calls) should have to change.

The framework plugin however was giving you a couple of conveniences in terms of development workflow. Indeed, the framework plugin was:

  • Regenerating the Prisma Client for you everytime you'd run nexus dev
  • Regenerating the Prisma Client after you'd edit your schema.prisma file and migrate your database

This means you now have to manually regenerate the Prisma Client after migrating your database. To do this, use the Prisma CLI and run the following command:

1npx prisma generate

Although you already had to run the command manually with the framework plugin, here's an additional quick reminder on how to run database migrations with Prisma Migrate:

To generate a migrate file and apply the migration, run:

1npx prisma migrate dev --preview-feature
Edit this page on Github