Hello Nexus Framework Prisma User!

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

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

Dependencies

You will need to install the Prisma dependencies yourself now.

1npm add -D @prisma/cli
2npm add @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/schema directly.

-import { use } from 'nexus'
-import { prisma } from 'nexus-plugin-prisma'
+import { makeSchema } from '@nexus/schema'
+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/schema'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4makeSchema({
5 plugins: [nexusPrisma({
+ experimentalCRUD: true
7 })]
8})

Configuring Backing types

To let @nexus/schema 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/schema'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4export const schema = makeSchema({
+ typegenAutoConfig: {
+ sources: [
+ {
+ source: 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 know 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/schema'
2import { nexusPrisma } from 'nexus-plugin-prisma'
3
4export const schema = makeSchema({
5 typegenAutoConfig: {
6 sources: [
7 {
8 source: require.resolve('.prisma/client/index.d.ts'),
9 alias: "prisma",
10 },
+ {
+ source: require.resolve('./context'),
+ alias: 'ContextModule'
+ }
15 ],
+ contextType: 'ContextModule.Context'
17 }
18 plugins: [
19 nexusPrisma({
20 experimentalCRUD: true
21 })
22 ]
23})

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/schema, you will need to install and do the mapping yourself.

  1. Start by installing the following dependency

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

    1import { makeSchema } from '@nexus/schema'
    2import { nexusPrisma } from 'nexus-plugin-prisma'
    +import { DateTimeResolver, JSONObjectResolver } from 'graphql-scalars'
    4
    5export const schema = makeSchema({
    6 typegenAutoConfig: {
    7 sources: [
    8 {
    9 source: require.resolve('.prisma/client/index.d.ts'),
    10 alias: "prisma",
    11 },
    12 {
    13 source: require.resolve('./context'),
    14 alias: 'ContextModule'
    15 }
    16 ],
    17 contextType: 'ContextModule.Context'
    18 }
    19 plugins: [
    20 nexusPrisma({
    21 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).',
    + })
    + }
    30 })
    31 ]
    32})

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, run:

1npx prisma migrate save --experimental

To apply the created migration file, run:

1npx prisma migrate up --experimental
Edit this page on Github