Create Product with upload Multiples Image Resolver

prisma

#1
 datamodel.graphql

 type File {
      id: ID! @unique
      createdAt: DateTime!
      updatedAt: DateTime!
      filename: String!
      mimetype: String!
      encoding: String!
      url: String! @unique
      product: Product
    }

    type Product {
      id: ID! @unique
      images: [File!]!
      name: String!
    }
return ctx.db.mutation.createSupplierProduct(
      {
        data: {
          name: args.name,
          description: args.description,
          detail: args.detail,
          tags: [...args.tags],
          inventory: args.inventory,
          retailPrice: args.retailPrice,
          wholesalePrice: args.wholesalePrice,
          category: {
            connect: { id: args.categoryId }
          },
          images: {
            connect:[ { id: <image_id_1> }, {id: <image_id_2>}, ...] // can solve this?
          }
        }
      },
      info
    );

inline

connect:[ { id: <image_id_1> }, {id: <image_id_2>}, ...] // can solve this?

<image_id_1> is dynamic

my code


#2

I’m currently keeping track of the file.url in the product.images array so it would be a [String] array instead of [File].

Your solution seems plausible as well. I’ll try it out and bring back my findings.

EDIT: @Kimiiz I think all you were missing was your relation between product images and file product. You should also make the file product an optional field if you’re gonna create the file first before your products. Then your connect should work fine.

EDIT 2: I found an even better solution. In your fileApi.js, return the s3 data, not the prisma saved file. Then, in your resolver you can create the file array along with your mutation like so:

async createPost(parent, { text, files }, ctx, info) {
    const userId = getUserId(ctx)
    var images = files ? await Promise.all(files.map(file => processUpload(file, ctx))) : null
    return ctx.db.mutation.createPost({
      data: {
        text,
        images: { create: images },
        author: { connect: { id: userId },
        },
      },
    }, info)
  },

#3

Thanks @manticarodrigo :grinning: