graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
829 stars 118 forks source link

"Cannot convert undefined or null to object" when running an upsert or delete mutation #678

Open AssisrMatheus opened 4 years ago

AssisrMatheus commented 4 years ago

My t.crud.upsertOneXXXX and t.crud.deleteOneXXXX operations are throwing this error:

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at addGloballyComputedInputs (...\node_modules\nexus-prisma\src\dmmf\transformer.ts:146:17)
    at Object.addComputedInputs (...\node_modules\nexus-prisma\src\dmmf\transformer.ts:177:10)
    at resolve (...\node_modules\nexus-prisma\src\builder.ts:301:28)
    at field.resolve (...\node_modules\graphql-extensions\src\index.ts:155:61)
    at resolveFieldValueOrError (...\node_modules\graphql\execution\execute.js:467:18)
    at resolveField (...\node_modules\graphql\execution\execute.js:434:16)
    at ...\node_modules\graphql\execution\execute.js:244:18
    at ...\node_modules\graphql\jsutils\promiseReduce.js:23:10
    at Array.reduce (<anonymous>)

It seems that at \node_modules\nexus-prisma\src\dmmf\transformer.ts line 181 it does params.args.data. data doesn't exist when we are deleting an entity nor when we're upserting. Upserting uses create and update args but not a data one, and delete has none.

This also happens with deleteMany. Update and create works fine.

AssisrMatheus commented 4 years ago

I noted that this only happen if "computedInputs" is added to nexusPrismaPlugin()

traderdome commented 3 years ago

Any update on this, I just encountered this issue as well

gopidon commented 3 years ago

Yup! this is ugly. Can't use 'computedInputs' option on nexusPrismaPlugin().

cmidgley commented 3 years ago

I spent a brief amount of time looking at this. The problem appears to be that src/builder.ts in the buildCRUD method checks for Mutation and then if there are any local or global computed inputs, it calls the dmmf/transformer.ts function addComputedInputs to add them. The problem is that addComputedInputs returns them as a data argument which is not supported for some mutation operations. Of note, even if you comment out the adding of the global inputs, it still returns an empty data: { } and that causes a failure as well (though that wouldn't be a valid solution).

If you comment out the call to addComputedInputs in buildCRUD the problem goes away, but of course that isn't the right answer as it stops computed inputs from working at all for any mutation. I added a quick condition on the mutation operation (mappedField.operation) to see if it was running an operation that doesn't support data: { } and that made it all work for me, but it feels like a hack since it doesn't cover the root computedInput field (such as your object ID on delete) and also seems like the wrong way to determine which operations support data arguments (hard coded operations rather than something more abstract). Therefore I have not done a PR for this...

Hope this helps find the right solution.