【发布时间】:2020-05-05 13:23:14
【问题描述】:
我正在使用 GraphQL 构建 React Native 移动应用程序。现在我一直坚持在 GraphQL 突变中传递数组。我一直在使用 redux-thunk 作为中间件将数据传递给 GraphQL 突变。
我的 GraphQL 变异信息:
createVirtualChallenge(
name: String!
target: String!
image: String
description: String
from: String!
to: String!
selectedUsers: [String]
): VirtualChallengeResponse!
我一直将选定的用户作为一个数组传递,如下所示:
["5e2148d4b76df200314f4848", "5e213e6ab76df200314f47c4"]
我的redux thunk fetch函数是这样的
fetch(URL.BASE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body: JSON.stringify({
query: `mutation{ createVirtualChallenge( name:"${name}", target:"${target}", image:"${image_name}", description:"${description}", from:"${from}", to:"${to}", selectedUsers: "${selectedUsers}" , ){ success } }`,
}),
})
.then(res => res.json())
所有的值都通过使用 redux 的 props 传递。
如果数组长度为 1,则此突变有效。但如果数组长度大于 1,则 GraphQL 会抛出错误。
来自 URL 的响应 - {"data": null, "errors": [{"extensions": [对象],“位置”:[数组],“消息”:“虚拟挑战 验证失败:selectedUsers:Cast to Array 值 \"[ '5e213e6ab76df200314f47c4,5e214493b76df200314f47fa' ]\" 在路径 \"selectedUsers\"", "path": [Array]}]}
【问题讨论】:
标签: javascript arrays react-native graphql mutation