由于数组参数,Prisma 无法创建用户类型?

Prisma cannot create a user type because of array argument?

这是我在 schema.graphql 中的输入类型:

input RegisterInput {
  birthday: String!
  email: String!
  firstName: String!
  gender: String!
  interests: [String!]!
  lastName: String!
  password: String!
}

这是我的突变:

const RegisterInput = inputObjectType({
  name: 'RegisterInput',
  definition(t) {
    t.string('birthday', { nullable: false });
    t.string('email', { nullable: false });
    t.string('firstName', { nullable: false });
    t.string('lastName', { nullable: false });
    t.string('gender', { nullable: false });
    t.string('password', { nullable: false });
    t.list.field('interests', {
      type: 'String',
      nullable: false,
    });
  },
});

const Mutation = objectType({
  name: 'Mutation',
  definition(t) {
    t.field('register', {
      type: User,
      args: {
        data: arg({ type: RegisterInput }),
      },
      resolve: async (
        _root,
        { data: { password, interests, ...userData } },
        { prisma }
      ) => {
        const hashedPassword = await bcrypt.hash(password, 10);
        const user = await prisma.user.create({
          data: {
            ...userData,
            interests: [...interests],
            password: hashedPassword,
          },
        });
        return user;
      },
    });

我的兴趣只是一个字符串数组,例如:['abcd'、'def']

但我收到了这个错误:

Unknown arg `0` in data.interests.0 for type UserCreateInterestInput. Available args:

type UserCreateInterestsInput { 
 set?: List<String>
}

该错误会重复出现,具体取决于数组中有多少项,例如:Unknown arg '1' 等等,相同的错误消息,我该如何解决?

您必须为 set 参数提供字符串列表,例如:

type UserCreateInterestsInput {
  set?: List<String>
}

有关详细信息,请参阅此 issue

const Mutation = objectType({
  name: 'Mutation',
  definition(t) {
    t.field('register', {
      type: User,
      args: {
        data: arg({ type: RegisterInput }),
      },
      resolve: async (
        _root,
        { data: { password, interests, ...userData } },
        { prisma }
      ) => {
        const hashedPassword = await bcrypt.hash(password, 10);
        const user = await prisma.user.create({
          data: {
            ...userData,
            interests: {set: interests},
            password: hashedPassword,
          },
        });
        return user;
      },
    });

希望对您有所帮助

之前遇到过,后来发现是查询错误

mutation {
  createFruit(data:{
    name: "Banana",
    images: {
      set: ["image_1.img", "image_2.img"]
    }
  }) {
    name
    images
  }
}

注意这不是 images: ["image_1.img", "image_2.img"]

fyi 使用 prisma 你可以在定义 objectType

时做 t.model.interest()