根据graphQl文档:
在代码中传递参数时,通常最好避免 自己构造整个查询字符串。相反,您可以使用$ 在查询中定义变量的语法,并将变量作为 单独的地图。
https://graphql.org/graphql-js/passing-arguments/
在这种情况下,我正在尝试查询以下架构:
const typeDefs = "
input OrderInputData {
date: String!
time: String!
frequency: String!
extras: [Int!]
mailAlarm: String
phoneAlarm: String
rating: Float
comments: String
}
type Mutation {
updateIntent(
paymentIntentId: String,
setupIntentId: String,
pairIntentId: String,
orderInput: OrderInputData): Boolean
}"
我很难根据graphQl的更好实践来构建查询,因为由于存在嵌套文档(OrderInputData),因此这种情况比文档中描述的情况更为复杂。这是我要知道的:
const dummyData = {
date: "11/11/2020",
frequency: "One time",
time: "6:17 PM",
mailAlarm: "teste",
phoneAlarm: "teste",
extras: [1, 3, 7],
};
const graphqlQuery = {
query: `
mutation updateIntent (
$paymentIntentId: String,
$pairIntentId: String,
$orderInput: {
date: String!,
time: String!,
frequency: String!,
extras: [Int!],
mailAlarm: String,
phoneAlarm: String
}) {
updateIntent (
paymentIntentId: $paymentIntentId,
pairIntentId: $pairIntentId,
orderInput: {
date: $date,
time: $time,
frequency: $frequency,
extras: $extras,
mailAlarm: $mailAlarm,
phoneAlarm: $phoneAlarm
})
}`,
variables: {
paymentIntentId: paymentIntentId,
pairIntentId: setupIntentId,
"orderInput.date": dummyData.date,
"orderInput.time": dummyData.time,
"orderInput.frequency": dummyData.frequency,
"orderInput.extras": dummyData.extras,
"orderInput.mailAlarm": dummyData.mailAlarm,
"orderInput.phoneAlarm": dummyData.phoneAlarm
}
我怎么了?
答案 0 :(得分:1)
通常,为[API,BE]突变规范中定义的任何复杂输入类型准备[并作为变量传递]对象。
variables: {
paymentIntentId: paymentIntentId,
pairIntentId: setupIntentId,
orderInput: { date: orderInput.date,
...
...或在这种情况下(先前定义为orderInput
)
orderInput: orderInput
...或者当然(如果名称匹配):
variables: {
paymentIntentId: paymentIntentId,
pairIntentId: setupIntentId,
orderInput,
...
只需查询/变异:
$orderInput: OrderInputData
之后
orderInput: $orderInput`