Reputation: 399
I'm trying to write a delete mutation and I'm getting some weird behavior. If i go to the frontend of the app and click on the delete button twice then refresh the page, the record gets deleted from the database. But If I click It once, then refresh, It's still there in the database.
This is the error that prints to the console:
Uncaught (in promise) Error:
Invalid `context.db.user.delete()` invocation in
/home/eric/DEV/Junk/nexus-prisma-app/api/graphql/User.ts:84:44`
An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
So in the front end I have to click my delete button twice for it to work, however the same error prints out, and the function doesn't re-fetch the query. If I go into the Apollo sandbox I can delete a record by ID only running the mutation once, but it still prints out the same error indicating it didn't work, but if I go to fetch the users again, the user has been deleted and the mutation worked.
/api/graphql/User.ts:
import { extendType, nonNull, objectType, stringArg } from "nexus";
export const User = objectType({
name: "User",
definition(t) {
t.string("id");
t.string("name");
t.string("username");
t.string("email");
},
});
export const GetUsersQuery = extendType({
type: "Query",
definition(t) {
t.nonNull.list.field("users", {
type: "User",
resolve(_root, _args, context) {
return context.db.user.findMany();
},
});
},
});
export const UserMutations = extendType({
type: "Mutation",
definition(t) {
t.field("createUser", {
type: "User",
args: {
name: nonNull(stringArg()),
username: nonNull(stringArg()),
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
const newUser = {
name: args.name,
username: args.username,
email: args.email,
};
return context.db.user.create({ data: newUser });
},
});
t.nonNull.list.field("findUser", {
type: "User",
args: {
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
console.log(args);
return context.db.user.findMany({
where: {
email: args.email,
},
});
},
});
},
});
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.list.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
async resolve(_root, args, context) {
console.log(args.id);
return await context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});
I get this problem in VS Code:
Type '(_root: {}, args: { id: string; }, context: Context) => Promise<User>' is not assignable to type 'FieldResolver<"Mutation", "deleteUser">'.
Type 'Promise<User>' is not assignable to type 'MaybePromise<({ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null)[]> | MaybePromise<...>'.
Type 'Promise<User>' is not assignable to type 'PromiseLike<MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]>'.
Types of property 'then' are incompatible.
Type '<TResult1 = User, TResult2 = never>(onfulfilled?: ((value: User) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[], TResult2 = never>(onfulfilled?: ((value: MaybePromise<...>[]) => TResult1 | PromiseLike<...>) | ... 1 more ... | undefined, onrejected?: (...'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'User' is missing the following properties from type 'MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]': length, pop, push, concat, and 29 more.
Does anyone know how to solve this problem?
Upvotes: 1
Views: 1436
Reputation: 399
removing list
from `t.nonNull.list.field("deleteUser"{/***/}) fixes the problem.
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
resolve(_root, args, context) {
return context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});
Upvotes: 1