Hey there! How can one define Prisma middlewares w...
# orm-help
s
Hey there! How can one define Prisma middlewares when using the nexus-plugin-prisma?
r
Hey @Sascha 👋 You just need to define the middleware and it will be called when Nexus resolvers are invoked.
s
How / where would I define those? Would I need to instantiate the prisma client myself, add the middlewares and then pass those as the prisma instance as part of the plugin configuration?
Or is there a more straight forward way?
r
Yes. Something like:
Copy code
const prisma = new PrismaClient()
prisma.$use(async (params, next) => {
  console.log('middlware called')
  const result = next(params)
  return result
})

const nexusPrisma = nexusSchemaPrisma({
  experimentalCRUD: true,
  prismaClient: (ctx: Context) => ctx.prisma,
})

const server = new ApolloServer({
  schema,
  context: createContext, // pass the instance of Prisma Client here
  playground: true,
})
This will work when you call the middleware
s
My use-case is about using the ctx.token.sub from the nexus-plugin-auth0 to lookup a user from the database and attach it to the prisma context.
Ah, yeah, I'm using nexus, not just nexus-schema.
r
Then you would explicitly need to add the Prisma Client like this:
Copy code
import { use } from 'nexus'
import { prisma } from 'nexus-plugin-prisma'
import { PrismaClient } from 'nexus-plugin-prisma/client'

use(
  prisma({
    client: {
      instance: client,
    },
  })
)
s
Nice, I'll try that then. Cheers!
💯 1
I'll let you know how it goes. And thanks for the amazingly fast responses, not just on this one!
💯 1
@Ryan I had to update some of my dependencies and now get...
Copy code
✕ nexus reflection failed
  | error  Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
  |        In case this error is unexpected for you, please report it in <https://github.com/prisma/prisma-client-js/issues/390>.
✕ nexus reflection failed
  | error  Error: @prisma/client did not initirun for documentation about this command.
I made sure I only have the nexus dependencies installed.
Copy code
"dependencies": {
    "nexus": "^0.26.1",
    "nexus-plugin-auth0": "^0.1.1",
    "nexus-plugin-prisma": "^0.18.1",
    "nexus-plugin-shield": "^0.2.0",
    "typescript": "^3.7.5"
  },
To make sure I only use the bundled versions of prisma/client and prisma/cli
r
I have the same version of
nexus-plugin-prisma
. Could you try restarting the server and check?
Copy code
const client = new PrismaClient({
  log: isDev() ? ['query'] : [],
})

use(
  prisma({
    client: {
      instance: client,
    },
    features: {
      crud: true,
    },
  })
)

client.$use(async (params, next) => {
  console.log('called', params)
  const result = next(params)
  return result
})
The above code-snippet is working for me
s
Same error, it fails when instantiating the client via new PrismaClient() .. so doesn't even hit the other code.
r
Could you run
prisma generate
separately before running Nexus. If that still doesn’t work, then it’s best if you could provide a reproduction repo so that I can check.
s
Can you show me your
schema.prisma
by any chance?
I have the feeling something more fundamental is going wrong here 🙂
It seems like it's trying to use a client in
node_modeules/.prisma
which is never generated.
My
schema.prisma
looks like this tho:
Copy code
generator client {
  provider = "prisma-client-js"
  output   = "./generated/client"
}
Not sure how that worked before.
r
Ohh Mine is a simple schema
Copy code
generator prisma {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DB_URL")
}

model User {
  id   Int     @id @default(autoincrement())
  name String
}
Without a custom generated folder
I guess what you need to do is this:
Copy code
import { PrismaClient } from './generated/client'

const prisma = new PrismaClient()
prisma.$use(async (params, next) => {
  console.log('middlware called')
  const result = next(params)
  return result
})
const nexusPrisma = nexusSchemaPrisma({
  experimentalCRUD: true,
  prismaClient: (ctx: Context) => ctx.prisma,
})
const server = new ApolloServer({
  schema,
  context: createContext, // pass the instance of Prisma Client here
  playground: true,
})
s
Hah, that's what I thought, I simply had to remove the
output   = "./generated/client"
.. no it generates the client in the right directory.
💯 1
Not sure how that ever worked previously / with older versions.
🤔 1
But yeah, finally!
prisma green 1