Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma Client Extensions: args is undefined #16551

Closed
chenkie opened this issue Nov 30, 2022 · 7 comments · Fixed by #16563
Closed

Prisma Client Extensions: args is undefined #16551

chenkie opened this issue Nov 30, 2022 · 7 comments · Fixed by #16563
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: clientExtensions
Milestone

Comments

@chenkie
Copy link
Contributor

chenkie commented Nov 30, 2022

Bug description

Hello Prisma!

I've just tried out the new preview feature for Prisma Client Extensions and I'm getting an issue with args being undefined. I've been referencing the sample here.

For my experimentation, I'm trying to create a Prisma Client instance that scopes down queries to records belonging to a specific user or subset of users.

In this case, I'm hard-coding a customerId to work with.

const customerId = "cl9ztaj7a0000pko377xgkq0u";

const nonAdminPrisma = new PrismaClient().$extends({
  query: {
    invoice: {
      async findMany({ model, operation, args, query }) {
        console.log("args", args); // undefined

        args.where = { customer: { id: customerId }, ...args.where };

        const invoices = await query(args);
        return query(invoices);
      },
    },
  },
});

I then try to call this client as follows:

const invoices = await nonAdminPrisma.invoice.findMany();

This throws with the following:

args undefined
(node:12311) UnhandledPromiseRejectionWarning: PrismaClientExtensionError: Error caused by an extension: Cannot read property 'where' of undefined
    at /Users/ryanchenkie/Desktop/code/prisma-examples/node_modules/@prisma/client/runtime/index.js:27989:63
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at PrismaClient._request (/Users/ryanchenkie/Desktop/code/prisma-examples/node_modules/@prisma/client/runtime/index.js:35900:16)

I've tried using both a specific model (invoice) and also $allModels and I get the same behavior.

Thanks!

How to reproduce

  1. Use the clientExtensions preview feature.
  2. Set up a new client using $extends and use the query block.
  3. Try to access something like where on args
  4. Find args is undefined

Expected behavior

No response

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["clientExtensions"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Customer {
  id             String    @id @default(cuid())
  name           String
  email          String    @unique
  districtNumber Int       @default(0)
  invoices       Invoice[]
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt
  address        String?
  active         Boolean   @default(true)
}

model Invoice {
  id         String   @id
  amount     Int
  dueDate    DateTime
  customer   Customer @relation(fields: [customerId], references: [id])
  customerId String
  createdAt  DateTime @default(now())
  updatedAt  DateTime
}
const customerId = "cl9ztaj7a0000pko377xgkq0u";

const nonAdminPrisma = new PrismaClient().$extends({
  query: {
    invoice: {
      async findMany({ model, operation, args, query }) {
        console.log("args", args); // undefined

        args.where = { customer: { id: customerId }, ...args.where };

        const invoices = await query(args);
        return query(invoices);
      },
    },
  },
});

async function main() {
  const invoices = await nonAdminPrisma.invoice.findMany();

  console.log("the invoices", invoices);
}

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: 14.9.3

Prisma Version

4.7.0
@chenkie chenkie added the kind/bug A reported bug. label Nov 30, 2022
@millsp
Copy link
Member

millsp commented Nov 30, 2022

Thanks Ryan for the feedback! We have missed assigning a default empty arg for your inputs. For now passing an empty object to findMany({}) should do the trick.

@chenkie
Copy link
Contributor Author

chenkie commented Nov 30, 2022

Thanks @millsp!

I tried passing an empty object but am now getting the following error:

(node:36614) UnhandledPromiseRejectionWarning: PrismaClientExtensionError: Error caused by an extension: 
Invalid `nonAdminPrisma.invoice.findMany()` invocation in
/Users/ryanchenkie/Desktop/code/prisma-examples/server.ts:37:49

  34 });
  35 
  36 async function main() {
→ 37   const invoices = await nonAdminPrisma.invoice.findMany([
         {
           id: 'cl9ztaj7a0000pko377xgkq0r',
           amount: 1000,
           dueDate: new Date('2022-12-31T00:00:00.000Z'),
           customerId: 'cl9ztaj7a0000pko377xgkq0u',
           createdAt: new Date('2022-11-30T14:44:04.833Z'),
           updatedAt: new Date('2022-11-30T00:00:00.000Z')
         }
       ])

Unknown arg `0` in 0 for type Invoice. Available args:

type findManyInvoice {
  where?: InvoiceWhereInput
  orderBy?: List<InvoiceOrderByWithRelationInput> | InvoiceOrderByWithRelationInput
  cursor?: InvoiceWhereUniqueInput
  take?: Int
  skip?: Int
  distinct?: List<InvoiceScalarFieldEnum>
}


    at /Users/ryanchenkie/Desktop/code/prisma-examples/node_modules/@prisma/client/runtime/index.js:27989:63
    at PrismaClient._request (/Users/ryanchenkie/Desktop/code/prisma-examples/node_modules/@prisma/client/runtime/index.js:35900:16)

The record on line 37 looks like the one belonging to that customer. Not sure about the unknown arg bit though.

Thanks!

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. team/client Issue for team Client. topic: clientExtensions labels Nov 30, 2022
@millsp
Copy link
Member

millsp commented Dec 1, 2022

Hey, we'll have a fix for this in the upcoming patch release. As for the last error, I think you did a small mistake in your inputs and it's our fault because it wasn't typesafe, but it will be after the patch release. Basically, when you do return query(invoices); it will try to issue the query with the invoice data and you're basically triggering the query twice. I realized that this is might be our docs that mislead you, correct?

@millsp millsp linked a pull request Dec 1, 2022 that will close this issue
@Jolg42 Jolg42 added this to the 4.8.0 milestone Dec 1, 2022
@chenkie
Copy link
Contributor Author

chenkie commented Dec 1, 2022

Thanks @millsp! I was indeed following the example here closely so docs might need to be adjusted. I'll follow up with the patch release 👍

@SevInf
Copy link
Contributor

SevInf commented Dec 2, 2022

Freshly released 4.7.1 contains two fixes for issues mentioned here. First, args will never be undefined. Second, query will have correct TS errors when invalid arguments are passed.

@chenkie
Copy link
Contributor Author

chenkie commented Dec 2, 2022

awesome!! many thanks

@andrew-walford-prisma
Copy link

andrew-walford-prisma commented Dec 5, 2022

We're working on a docs fix now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: clientExtensions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants