【问题标题】:GraphQL mutation with empty array with React and AWS Amplify带有 React 和 AWS Amplify 的空数组的 GraphQL 突变
【发布时间】:2021-05-30 11:57:59
【问题描述】:

我正在尝试创建一个包含“列表”数组的“用户”。当我尝试第一次创建用户时,我想要一个空的“列表”数组,以便以后添加。

这是我的架构:

type User @model {
  id: String!
  email: String!
  lists: [List] @connection(name: "UserLists")
}

type List @model {
  id: ID!
  name: String!
  owner: User! @connection(name: "UserLists")
}

这里是 createUser 变异函数:

export const createUser = /* GraphQL */ `
  mutation CreateUser(
    $input: CreateUserInput!
    $condition: ModelUserConditionInput
  ) {
    createUser(input: $input, condition: $condition) {
      id
      email
      lists {
        items {
          id
          name
          createdAt
          updatedAt
        }
        nextToken
      }
      createdAt
      updatedAt
    }
  }
`;

这是我要打的电话:

let input = {
    input: {
       email: 'test@test.com',
       id: 'ab953afe',
       lists: [],
     },
};

await API.graphql(graphqlOperation(createUser, input));

我收到了这个错误:

“变量输入包含一个字段名称'lists'……为输入对象类型'CreateUserInput'定义”

如果我为“列表”省略任何内容,我可以创建一个新用户。

任何想法为什么我会看到此错误以及我应该如何执行此操作?

【问题讨论】:

    标签: reactjs amazon-web-services graphql aws-amplify


    【解决方案1】:

    只需从输入中删除list: []

    const input = {
        input: {
           email: 'test@test.com',
           id: 'ab953afe',
         },
    };
    
    await API.graphql(graphqlOperation(createUser, input));
    

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 1970-01-01
      • 2019-06-28
      • 2021-04-24
      • 2021-09-23
      • 2023-03-05
      • 2020-09-20
      • 2020-07-01
      • 2020-12-21
      相关资源
      最近更新 更多