gpbaculio
gpbaculio

Reputation: 5968

Prisma cannot create a user type because of array argument?

this is my InputType in schema.graphql:

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

and this is my mutation:

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

my interests is just an array of strings, .e.g: ['abcd', 'def']

but i got this error:

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

type UserCreateInterestsInput { 
 set?: List<String>
}

that error will repeat depending of how many items is in the array, e.g.: Unknown arg '1' and so on, same error message, how do i fix this?

Upvotes: 1

Views: 3483

Answers (2)

Nate
Nate

Reputation: 92

Happened to me earlier, turns out it was a query mistake.

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

Note it's not images: ["image_1.img", "image_2.img"]

fyi with prisma you can do t.model.interest() when defining objectType

Upvotes: 0

Rizialdi
Rizialdi

Reputation: 26

You must provide a list of strings to set argument, such as:

type UserCreateInterestsInput {
  set?: List<String>
}

Refer to this issue for more information.

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

Hope this helps

Upvotes: 1

Related Questions