提交包含嵌套数组的对象游戏负载的 graphql 突变,"aws-amplify"



我试图使用aws放大提交以下有效载荷(嵌套的对象数组(:

{
"data": [
{ "name": "alpha", "description": "lorem" },
{ "name": "bravo", "description": "ipsum" },
]
}

此解决方案(源代码为TypeScript(:

import AWSAmplifyAPI, { graphqlOperation } from "@aws-amplify/api";

const rawGraphQLRequest: string = `
mutation ($data: [registerMultipleItemsInput]!) {
registerMultipleItems(data: [$data])
}`;
type Item = {
name: string;
description: string;
}
async function registerMultipleItems(items: Array<Item>): Promise<void> {
await AWSAmplifyAPI.graphql(graphqlOperation(rawGraphQLRequest, { data: items }));
}

导致不清楚的错误:

Validation error of type VariableTypeMismatch: Variable type '[registerMultipleItemsInput]!' doesn't match expected 
type 'registerMultipleItemsInput' @ 'registerMultipleItems'";

是后端,方案定义为:

registerMultipleItems(data: [registerMultipleItemsInput]!): [String]!
@aws_cognito_user_pools
input registerMultipleItemsInput{
name: String!
description: String!
}

我尝试将registerMultipleItemsInput定义添加到rawGraphQLRequest:

const rawGraphQLRequest: string = `
input registerMultipleItemsInput {
name: String!
description: String!
}
mutation ($data: [registerMultipleItemsInput]!) {
registerMultipleItems(data: [$data])
}`;

以上错误仍然存在,并且发生了其他错误:

Validation error of type NonExecutableDefinition: The registerMultipleItemsInput definition is not executable.

{ "data": <Array>}有效载荷的正确语法是什么?

第页。S.我希望我不需要在rawGraphQLRequest中定义registerMultipleItemsInput,因为registerMultipleItemsInput已经在后端定义了,在前端再次定义它是某种硬编码。

我认为您的第一个解决方案是正确的,除了:

mutation ($data: [registerMultipleItemsInput]!) {
registerMultipleItems(data: [$data]) // <-------- incorrect
}`;

$data已经包含一个registerMultipleItemsInput的数组,但您将其封装在另一个数组中,这是不正确的,它必须是:

mutation ($data: [registerMultipleItemsInput]!) {
registerMultipleItems(data: $data) // <-------- correct
}`;

相关内容

  • 没有找到相关文章