提交带有“ aws-amplify”

时间:2020-03-04 09:09:05

标签: javascript graphql aws-amplify

我尝试使用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'";

是后端,方案定义为:

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.我希望我不需要在registerMultipleItemsInput中定义rawGraphQLRequest,因为registerMultipleItemsInput已经在后端定义了,而在前端再次定义是一种硬编码。

1 个答案:

答案 0 :(得分:1)

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

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

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

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