gpt4 book ai didi

javascript - 提交包含对象播放负载嵌套数组的 graphql 突变,值为 "aws-amplify"

转载 作者:行者123 更新时间:2023-12-02 21:34:58 25 4
gpt4 key购买 nike

我尝试使用 aws-amplify 提交以下有效负载(嵌套的对象数组) :

{
"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'";

是后端,scheme定义为:

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> 的正确语法是什么} 有效负载?

P。 S.我希望我不需要定义registerMultipleItemsInputrawGraphQLRequest ,因为registerMultipleItemsInput已经在后端定义并在前端再次定义它是某种硬编码。

最佳答案

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

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

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

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

关于javascript - 提交包含对象播放负载嵌套数组的 graphql 突变,值为 "aws-amplify",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60522485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com