react-nativegraphqlapolloresolvergraphql-mutation

Many-to-many mutation resolver for Apollo GraphQL


I have 2 entities: Peas and Pods. They have an implicit many-to-many relationship. At the moment, I'm trying to add already created Peas to a Pod on creation.

Here is my current resolver:

addPod: async (parent, args, context) => {
      return context.prisma.pod.create({
        data: {
          name: args.name,
          description: args.description,
          image: args.image,
          peas: args.peas.map(email => ({
            connect: {
              email: email
            }
          }))
        }
      })
    },

I am trying to pass an array of emails that correspond to individual Peas so as to associate a group of Peas with this new Pod.

In Apollo Explorer, when passing an array of emails, the error reads

Invalid `prisma.pod.create()` invocation:
{ data: { name: 'The Newest Pod', description: 'Can\'t get newer than this!', image: 'https://i.ibb.co/9vg5jjG/IMG-20220501-WA0022.jpg', peas: [ { connect: { email: 'rach@test.com' } } ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } } 
Argument peas: Got invalid value  { connect: { email: 'rach@test.com' } }, { connect: { email: 'benji@test.com' } } ]  on prisma.createOnePod. 
Provided List<Json>, expected PeaCreateNestedManyWithoutPodsInput: type PeaCreateNestedManyWithoutPodsInput { create?: PeaCreateWithoutPodsInput | List<PeaCreateWithoutPodsInput> | PeaUncheckedCreateWithoutPodsInput | List<PeaUncheckedCreateWithoutPodsInput> connectOrCreate?: PeaCreateOrConnectWithoutPodsInput | List<PeaCreateOrConnectWithoutPodsInput> connect?: PeaWhereUniqueInput | List<PeaWhereUniqueInput> }

This is my mutation:

mutation AddPod($name: String!, $description: String, $image: String, $peas: [String]) {
  addPod(name: $name, description: $description, image: $image, peas: $peas) {
    name
    description
    image
    peas {
      fullName
    }
  }
}

Based on the type error, I assume my issue is how I've constructed the 'Peas' variable but I'm not sure how else to structure it other than in an array? Maybe the problem is elsewhere.

And these are the variables I'm passing in Apollo Explorer:

{
  "name": "The Newest Pod",
  "description": "Can't get newer than this!",
  "image": "https://i.ibb.co/9vg5jjG/IMG-20220501-WA0022.jpg",
  "peas": ["rach@test.com", "benji@test.com"]
}

This is my definition in schema.graphql

type Mutation {
  addPod(name: String!, description: String, image: String, peas: [String]): Pod
}

Would love some help! Surprisingly hard to find on the web as most fleshed out examples just have one-to-one or one-to-many relationships like blogs or to-do lists.


Solution

  • I found the solution, which was surprisingly difficult to find in the docs.

    The resolver and the email format were the issues, the emails have to be formatted as a list of objects. If you define the emails as an input type in graphql.schema, it'll work out fine.

    Here's the new resolver:

    addPod: async (parent, args, context) => {
          return context.prisma.pod.create({
            data: {
              name: args.name,
              description: args.description,
              image: args.image,
              peas: {
                // connect: [{ email: "rach@test.com" }, { email: "benji@test.com"}],
                connect: args.peas
              }
            },
            include: {
              peas: true, // Include all peas in the returned object
            },
          })
        },