简体   繁体   English

如何通过 graphQL 和 AWS AppSync 发送 JSON 数组以将数据添加到 Dynamo 表?

[英]How to send JSON array through graphQL and AWS AppSync to add data to Dynamo Table?

What I'm trying to do is to send data from a React Native app through AppSync so that I can update a user's data on a dynamo table.我想要做的是通过 AppSync 从 React Native 应用程序发送数据,以便我可以更新 dynamo 表上的用户数据。 I can perform all of my CRUD operations, just not a list of data.我可以执行我所有的 CRUD 操作,只是不能执行数据列表。 When I try, I get an error that what I'm sending isn't a valid JSON.当我尝试时,我收到一条错误消息,提示我发送的不是有效的 JSON。 However, my input passes as valid JSON when I put it through a JSON linter.但是,当我将输入通过 JSON linter 时,我的输入作为有效的 JSON 传递。

To try and solve this problem, I've changed my input flow to the simplest possible process.为尝试解决此问题,我已将输入流程更改为最简单的流程。 Originally I was pulling my JSON from a parsed JSON string that was locally cached.最初我是从本地缓存的已解析 JSON 字符串中提取我的 JSON。 I thought that perhaps there was an issue with the JSON.parse(obj) function.我认为 JSON.parse(obj) 函数可能存在问题。 That didn't hold up to reason.这不符合道理。 So I moved all of my input out of a JS object and into a JSON that isn't stored in a variable beforehand but is directly passed to the API I'm using ( https://aws-amplify.github.io/docs/js/api ).所以我将我所有的输入从一个 JS 对象中移出并转移到一个 JSON 中,这个 JSON 事先没有存储在一个变量中,而是直接传递给我正在使用的 API ( https://aws-amplify.github.io/docs /js/api )。

After this didn't work, I thought that maybe my GraphQL Schema was wrong for the object type I was sending, but didn't find anything amiss.在这不起作用之后,我想也许我的 GraphQL 模式对于我发送的对象类型是错误的,但没有发现任何问题。

My schema in AppSync using GraphQL is defined as follow:我在 AppSync 中使用 GraphQL 的架构定义如下:

input CreateUserInput {
  email: String!
  data: AWSJSON
}

The request I'm sending is as follows:我发送的请求如下:

const dbReturn = await API.graphql(graphqlOperation(
  mutations.createUser, {
    input: {
      "email": "fake.email@atdot.com",
      "data": [{
        "num": 34,
      }]
    }
  }
));

The error message says:错误消息说:

"Variable 'data' has an invalid value. Unable to parse [{num=34}] as valid JSON." “变量‘数据’的值无效。无法将 [{num=34}] 解析为有效的 JSON。”

What confuses me is that "[{num=34}]" isn't the input I'm giving it.令我困惑的是"[{num=34}]"不是我给它的输入。 I'm sending it "[{"num": 34}]" which is valid JSON.我发送的是"[{"num": 34}]" ,这是有效的 JSON。 I would expect it to be able to parse my input and send the data to my dynamo table seeing as the JSON input is valid.我希望它能够解析我的输入并将数据发送到我的 dynamo 表,因为 JSON 输入是有效的。

Anybody have any ideas what I can do to get past this?有人知道我能做些什么来克服这个问题吗? Thank you for the help!感谢您的帮助!

Figured it out.弄清楚了。 The answer lies in the GraphQL schema.答案在于 GraphQL 模式。

"createUser": {
    "email": "Hello, world!",
    "data": "{\"key\":\"value\"}",
}

The input for a "complex data type" is just a string. “复杂数据类型”的输入只是一个字符串。 So when making the request, you just have to pass in the data as something something like JSON.stringify(variable.data) .因此,在发出请求时,您只需将数据作为JSON.stringify(variable.data)类的内容JSON.stringify(variable.data) I hope this helps someone along the way.我希望这对一路上的人有所帮助。

I had the opposite issue.我有相反的问题。 I am not using code to create the query but using gql directly.我没有使用代码来创建查询,而是直接使用 gql。 If I use String in input then I get a stringified JSON in my db which is not what I want.如果我在输入中使用字符串,那么我会在我的数据库中得到一个字符串化的 JSON,这不是我想要的。 Instead I had to ensure there were no quotes around - in your case, num .相反,我必须确保周围没有引号 - 在您的情况下, num

Schema架构

Item {
 data: AWSJSON
}
ItemInput {
  data: AWSJSON
}

Query询问

mutation {
 createUser {
   data: { num: 34 } // Not { "num": 34 }
 }
}

This ensures my data is not stored as a string and that on query I get parseable JSON back instead of a quote escaped string.这确保我的数据不会存储为字符串,并且在查询时我得到可解析的 JSON 而不是引号转义字符串。

My VTL templates do do anything unusual on mutations but on query I do return as:我的 VTL 模板对突变做了任何不寻常的事情,但在查询时我确实返回:

{
  "data" :  $util.toJson($ctx.result.get('data')) // OR $ctx.result.get('data').toJSON()
}

Try this.试试这个。

const dbReturn = await API.graphql(graphqlOperation(
  mutations.createUser, {
    input: {
      "email": "fake.email@atdot.com",
      "data":JSON.stringify([{
        "num": 34,
      }])
    }
  }
));

In this image, the sources are AWSJSON type which can hold object or array, if you want to use mutations since it's a JSON object you will need to do escape in order to create the item in the app sync.在此图像中,源是 AWSJSON 类型,它可以保存对象或数组,如果您想使用更改,因为它是一个 JSON 对象,您需要进行转义以在应用程序同步中创建项目。 在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM