簡體   English   中英

使用 GraphQL Nexus 刪除突變在 Prisma 中不起作用

[英]Delete Mutations not working in Prisma with GraphQL Nexus

我正在嘗試編寫刪除突變,但出現了一些奇怪的行為。 如果我 go 到應用程序的前端並單擊兩次刪除按鈕然后刷新頁面,記錄將從數據庫中刪除。 但是如果我單擊它一次,然后刷新,它仍然存在於數據庫中。

這是打印到控制台的錯誤:

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.

所以在前端我必須點擊我的刪除按鈕兩次才能工作,但是同樣的錯誤會打印出來,並且 function 不會重新獲取查詢。 如果我 go 進入 Apollo 沙箱,我可以通過 ID 刪除一條記錄,只運行一次突變,但它仍然打印出相同的錯誤,表明它沒有工作,但如果我 go 再次獲取用戶,用戶已被刪除並且突變起作用了。

/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,
          },
        });
      },
    });
  },
});

我在 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.

有誰知道如何解決這個問題?

從 `t.nonNull.list.field("deleteUser"{/***/}) 中刪除list可以解決問題。

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,
          },
        });
      },
    });
  },
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM