Hey guys so I'm having trouble getting the update from nexus framework to @nexus/schema with nexus-plugin-prisma working in my next.js app.
I saw a similar issue here already, but the user ended up solving it himself with an upgrade which unfortunately didnt help me.
My initial project was based off prisma's nextjs example (https://github.com/prisma/prisma-examples/blob/latest/typescript/graphql-nextjs) and that ran fine while it was still based on nexus framework.
Anyway, my current folder structure looks like this:
โโโ README.md
โโโ package.json
โโโ prisma
โ โโโ schema.prisma
โ โโโ migrations
โ โโโ docs
โโโ src
โโโ graphql
โโโ context.ts
โโโ schema.ts
โโโ pages
โโโ api
โโโ index.ts
โโโ nexus-typegen.ts
โโโ schema.graphql
And my src/pages/api/index.ts page is as follows:
import { makeSchema } from '@nexus/schema'
import { nexusPrisma } from 'nexus-plugin-prisma'
import { ApolloServer } from 'apollo-server-micro'
import { PrismaClient } from '@prisma/client'
interface Context {
prisma: PrismaClient
}
import { Item, Company, Warehouse, Image, Location, Status, Shipment, Mutations, Crud } from '../../graphql/schema'
import path from 'path'
const schema = makeSchema({
typegenAutoConfig: {
sources: [
// {
// source: '../../../node_modules/.prisma/client/index.d.ts',
// alias: 'prisma',
// },
{
source: require.resolve('../../graphql/context.ts'),
alias: 'ContextModule'
},
{
source: '@prisma/client',
alias: 'prisma',
},
],
contextType: 'ContextModule.Context'
},
types: [Item, Company, Warehouse, Image, Location, Status, Shipment, Mutations, Crud],
plugins: [
nexusPrisma({
prismaClient: (ctx: Context) => ctx.prisma,
experimentalCRUD: true,
}),
],
nonNullDefaults: {
input: false,
output: false,
},
outputs: {
typegen: path.join(process.cwd(), 'src', 'pages', 'api', 'nexus-typegen.ts'),
// typegen: path.join( __dirname, '../node_modules/@types/nexus-typegen/index.d.ts'),
schema: path.join(process.cwd(), 'src', 'pages', 'api', 'schema.graphql')
},
})
export const config = {
api: {
bodyParser: false,
},
}
export default new ApolloServer({ schema }).createHandler({
path: '/api',
});
My src/graphql/schema.ts file is the standard:
import {
asNexusMethod,
objectType,
queryType,
mutationType,
} from '@nexus/schema'
import { GraphQLDate } from 'graphql-iso-date'
export const GQLDate = asNexusMethod(GraphQLDate, 'date')
export const Item = objectType({
name: 'Item',
definition(t) {
t.model.id()
t.model.title()
t.model.qty()
t.model.type()
t.model.description()
t.model.serialNo()
t.model.purchase_price()
t.model.currency()
...
Anyway, I can get to the graphql playground at localhost:3000/api but there and in my frontend, on any graphql query I just get the following error back: "Could not find Prisma Client JS in context (ctx.prisma)"
Any idea what I'm missing?
Okay so now I got the prisma client loading - in the playground queries resolve correctly and everythings working. However, in an app using apollo client I'm seeing errors on the frontend.
The returned object has two keys data which contains all the data I want, correct and everything,but also errors which contains a bunch of message: "Cannot real property 'item' of undefined' - where item is a property of the object I'm querying.
Fixed it - all issues stemmed from not being able to find prisma on the ctx (Just like the error said.. haha).
Anyway, fix was:
const prisma = new PrismaClient()
...
const schema = makeSchema({
...
plugins: [
nexusPrisma({
prismaClient: ctx => ctx.prisma = prisma,
experimentalCRUD: true,
}),
],
...
})
I have the same issue, it is because i did not pass createContext here

None of the previous answers worked for me, I had to manually attach the prisma client in the ApolloServer constructor.
import { PrismaClient } from "@prisma/client"
import * as types from "./types"
export const schema = makeSchema({
plugins: [
nexusPrisma({
experimentalCRUD: true,
prismaClient: (ctx) => ctx.db,
}),
],
types,
typegenAutoConfig: {
sources: [
{
source: require.resolve(".prisma/client/index.d.ts"),
alias: "prisma",
},
{
source: require.resolve("./context"),
alias: "ContextModule",
},
],
contextType: "ContextModule.Context",
},
outputs: {
schema: path.join(__dirname, "generated", "schema.graphql"),
typegen: path.join(__dirname, "generated", "typegen.d.ts"),
},
})
const prisma = new PrismaClient()
const server = new ApolloServer({
schema: applyMiddleware(schema, permissions),
context: () => ({
db: prisma,
}),
})
Most helpful comment
Fixed it - all issues stemmed from not being able to find prisma on the ctx (Just like the error said.. haha).
Anyway, fix was: