reactjs - 如何使用 graphql 将 id 列表作为参数发送
问题描述
我正在使用 React、Graphql 和 Apollo。
我的功能如下:
updateSlots(updateSlots, data){
const {slotIds, refetch} = this.props;
const {disabled, printEntry, toggleSlotForm} = data;
updateSlots({variables: {disabled: disabled, printEntry: printEntry, ids: slotIds}})
.then(res => {
if (res.data.updateSlots.ok) {
refetch();
this.setState({hasError: false, errorMessage: "", saved: true, slot: initialSlot()}, () => {
setTimeout(() => toggleSlotForm(), 3000)
});
}
})
.catch(err => {
debugger;
let error_msg = err.message.replace("GraphQL error: ", '');
this.setState({hasError: true, saved: false, errorMessage: error_msg});
throw(error_msg);
})
}
mutation
如下:
const UPDATE_SLOTS = gql`
mutation updateSlots($disabled: Boolean!, $printEntry: Boolean!, $ids: String!) {
updateSlots(disabled: $disabled, printEntry: $printEntry, ids: $ids) {
ok
}
}
`;
在后端我使用石墨烯,突变如下:
class UpdateSlots(graphene.Mutation):
class Arguments:
disabled = graphene.Boolean(required=True)
printEntry = graphene.Boolean(required=True)
ids = graphene.String(required=True)
ok = graphene.Boolean()
@staticmethod
def mutate(root, info, disabled, printEntry, ids):
pdb.set_trace()
ok = False
try:
ok = True
for id in ids:
print(id)
except Exception as e:
ok = False
raise GraphQLError(str(e))
但我得到ids variable
如下字符串:
"['3692', '3695', '3704']"
代替 :
['3692', '3695', '3704']
如何发送一组 id 以及如何在后端获取它?
任何的想法?
解决方案
问题是因为ids
是 type String
。
在您schema
的参数类型中ids
,String
为了能够拥有类似的东西,['3692', '3695', '3704']
您必须将参数定义为 a List
of Strings
。
就像是:
class UpdateSlots(graphene.Mutation):
class Arguments:
disabled = graphene.Boolean(required=True)
printEntry = graphene.Boolean(required=True)
ids = graphene.List(graphene.String(required=True))
在突变中,您还必须将参数更新ids
为:
const UPDATE_SLOTS = gql`
mutation updateSlots($disabled: Boolean!, $printEntry: Boolean!, $ids: [String]!) {
updateSlots(disabled: $disabled, printEntry: $printEntry, ids: $ids) {
ok
}
}
`;
希望能帮助到你。
推荐阅读
- shell - shell中的连接字符串
- java - 如何在流中使用 replaceAll 方法
- pdf - 如何禁用图像和路径的 ghostscipt 光栅化?
- html - 如何使用地图在悬停时缩放图像
- java - 将 java.sql.Timestamp 转换为 Instant Time
- php - 试图让 PHP-FPM 工作 (Apache 2.4.33)
- angular - ngrx 商店选择总是给未定义
- oozie - 无法找出为什么 oozie 作业仍处于运行状态
- c# - C# - 如何在windowform中获取具有相同标签值的文本框列表?
- json - 如何从 json-lines 文件中读取到带有 immutable.HashMap 的数据集?