Creating a resolver for a mutation with an array

prisma

#1

From the documentation here: https://www.prisma.io/docs/reference/prisma-api/mutations-ol0yuoz6go I can see the mutation for existing posts. Could you give me some guidance as to what the resolver would look like where you have multiple existing posts that you wish to push at the same time?

This is the mutation:

Create a user, create and connect new posts, and connect to existing posts

mutation {
  createUser(
    data: {
      email: "zeus@example.com"
      name: "Zeus"
      age: 42
      posts: {
        create: [{
          published: true
          title: "First blog post"
        }, {
          published: true
          title: "Second blog post"
        }]
        connect: [{
          id: "cjcdi63j80adw0146z7r59bn5"
        }, {
          id: "cjcdi63l80ady014658ud1u02"
        }]
      }
    }
  ) {
    id
    posts {
      id
    }
  }
}

#2
connect: [{
  id: "cjcdi63j80adw0146z7r59bn5"
}, {
  id: "cjcdi63l80ady014658ud1u02"
}]

This part of the mutation connects multiple existing posts to the new user.


#3

Thank you - I had understood that is the part of the mutation I need to produce. My question is how would I call the part of the mutation that adds the array of posts from the yoga server? I haven’t been able to find any examples that use arrays. Would I need to loop through the array and push each one separately, or is there a better way? Something that would start like this …

Mutation: {
  addExistingPostsToUser: (_, args, context, info) => {
    return context.prisma.mutation. ...

#4

I’d also like to know this. Any thoughts, @nilan? Thanks!


#5

I am looking for this too , please?

Edit

Solved:

The resolver:

createProduct(parent, args, context, info) {   

const PIC = args.pics.map((pic) => {
          return { url: pic };
        });

    return context.prisma.createProduct({
            pics: { create: PIC },
    })

}

The schema:

type Mutation {
createProduct(pics: [String!]!): Product

}

and the mutation:

mutation{
  createProduct(pics:["1","2"]){
  pics
}
}