Skip to content

Issue Sending Array Of Objects in GraphQL Mutation (Amplify Gen 2)

0

I’m working with AWS Amplify Gen 2 and have defined a GraphQL mutation that accepts an array of objects. However, I’m encountering issues when sending the array from my client mutation.

Here’s my schema definition:

import { a } from "@aws-amplify/backend";
import { POAcknowledgementCreationFunction } from "../../../functions/POAcknowledgementCreation/resource";

export const POAcknowledgementCreationSchema = {

    // Define the Supplier POAcknowledgement Creation mutation
    CreatePOAcknowledgement: a
        .mutation()
        .arguments({
            input: a.ref("SupplierPOAcknowledgementInput").required(), // ✅ This is valid in custom types
        })
        .returns(a.ref("SupplierPOAcknowledgementCreationResponse"))
        .authorization((allow) => [allow.groups(["Suppliers"])])
        .handler(a.handler.function(POAcknowledgementCreationFunction)),

    SupplierPOAcknowledgementInput: a.customType({
        PurchaseOrderID: a.string().required(),
        PurchaseOrdeItem: a.ref('PurchaseOrderItemInput').array().required(),
    }),

    PurchaseOrderItemInput: a.customType({
        PurchaseOrderItemID: a.string().required(),
        ConfirmedItem: a.ref('ConfirmedItemInput').array().required(),
    }),

    ConfirmedItemInput: a.customType({
        DeliveryDate: a.string().required(),
        ConfirmedQuantity: a.integer().required(),
        ConfirmedQuantityUOM: a.string().required(),
    }),

    // Response Type for SupplierPOAcknowledgementInput
    SupplierPOAcknowledgementCreationResponse: a.customType({
        SupplierPOAcknowledgementStatus: a.string().required(),
    }),

};

Mutation I’m Trying:

mutation MyMutation {
  CreatePOAcknowledgement(
    input: 
    {
        PurchaseOrderID: "12345", 
        PurchaseOrdeItem: 
        [ 
            {
            PurchaseOrderItemID: "12345", 
            ConfirmedItem: 
            {
                DeliveryDate: "12365", 
                ConfirmedQuantity: 10, 
                ConfirmedQuantityUOM: "EA"
            }
        }
       ]
    }
  ) {
    SupplierPOAcknowledgementStatus
  }
}

When I run this mutation, I’m getting a validation or type mismatch error indicating that the array input isn’t being parsed correctly.

Is there a specific syntax or configuration needed in Amplify Gen 2 for accepting array inputs in custom mutations?

Any guidance or clarification on how to correctly define and send an array of objects in Amplify Gen 2 would be greatly appreciated.

1 Answer
0

I have tried to execute your schema and faced similar issues. On researching to understand how to pass the Array Input as argument, I could find the below documentation. The argument is defined as string using which we pass the array input and the return type is defined as per our custom type.

[+] https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/batch-ddb-operations/

AWS
SUPPORT ENGINEER

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.