1

这是我在 schema.graphql 中的 InputType:

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' 等等,同样的错误消息,我该如何解决这个问题?

4

2 回答 2

1

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

type UserCreateInterestsInput {
  set?: List<String>
}

有关详细信息,请参阅此问题

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

希望这可以帮助

于 2020-05-21T19:51:36.617 回答
0

早些时候发生在我身上,原来这是一个查询错误。

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

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

t.model.interest()在定义 objectType 时可以使用 prisma 仅供参考

于 2020-06-06T07:24:04.307 回答