【问题标题】:Passing array of objects into AWS Amplify GraphQL API mutation将对象数组传递到 AWS Amplify GraphQL API 突变
【发布时间】:2022-01-21 03:30:23
【问题描述】:

我在我的 React Native 应用程序上使用 AWS Amplify 的 GraphQL API。我的应用中有一组对象,如下所示:

const [data, setData] = useState([
   {
       id: someid,
       thing: thing,
       otherThing: otherThing
   },
   {
       id: someid,
       thing: thing,
       otherThing: otherThing
   }
]);

这需要在我的schema.graphql 中看起来像什么?我目前的定义是这样的:

type someThing @model {
  UserID: String!
  thingName: String
  thingID: String! @primaryKey(sortKeyFields: ["UserID"])
  data: [AWSJSON]
}

在调用 createSomeThing 突变后,我目前在我的应用程序中收到此错误,其中我将 data: data 作为输入传递:

Variable 'data' has an invalid value. Unable to parse {id=8f3aa794-1881-4eaa-ba4e-0ac979b5b0a6, thing=pasta, otherThing=one} as valid JSON.

这里有什么问题?我是否在schema.graphql 中错误地定义了这个?或者我需要在传递到我的突变之前转换数据吗?

【问题讨论】:

  • 检查提交的data是否是有效的JSON。
  • 我实际上必须将对象数组作为字符串传递

标签: react-native graphql aws-amplify


【解决方案1】:

通过将数据作为字符串传入来解决,只需JSON.Stringify(data)

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 2021-05-30
    • 2020-11-23
    • 2020-09-22
    • 2020-06-16
    • 2020-08-16
    • 2021-04-24
    • 2017-09-03
    • 2021-09-23
    相关资源
    最近更新 更多