繁体   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