【发布时间】:2019-08-19 10:04:04
【问题描述】:
我正在使用 axios 为 Grapql 编写一个简单的帖子示例。 完美的工作示例
let body = {
query: `
query {
game(id:"5c9beed4a34c1303f3371a38") {
_id
title
}
}
`,
variables: {}
}
axios.post("http://localhost:3344/graphql", body)
.then(res => {
console.log(res.data) // cool data from server
})
但我想使用我看到的这种变量,但我无法使其工作
let body = {
query: `
query {
game(id: $id) { // IT SUPPOSE THE ID VARIABLE SHOULD BE INJECTED HERE
_id
title
}
}
`,
variables: {id: "5c9beed4a34c1303f3371a38"} // DECLARED VARIABLE ID
}
axios.post("http://localhost:3344/graphql", body)
.then(res => {
console.log(res.data) // 400 (Bad Request)
})
GRaphQl 类型声明
type Query {
game(id: String!): Game!
}
根据 cmets 我也试过这个。但是还是不行
let body = {
query: `
query {
Game($id: String!) {
game(id: $id) {
_id
title
}
}
}
`,
variables: {id: "5c9beed4a34c1303f3371a38"}
}
【问题讨论】: