在 GraphQL 对象查询中使用变量
Using variables in GraphQL object query
我正在研究 GraphQL JS tutorial 并试图了解变量如何与查询一起工作。
在 Object Types section 我可以正常工作:
我的 server.js
文件:
const express = require('express')
const graphqlHTTP = require('express-graphql')
const { buildSchema } = require('graphql')
const app = express()
const schema = buildSchema(`
type RandomDie {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}
type Query {
getDie(numSides: Int): RandomDie
}
`)
class RandomDie {
constructor(numSides) {
this.numSides = numSides;
}
rollOnce() {
return 1 + Math.floor(Math.random() * this.numSides);
}
roll({numRolls}) {
var output = [];
for (var i = 0; i < numRolls; i++) {
output.push(this.rollOnce());
}
return output;
}}
const root = {
getDie: ({numSides}) => {
return new RandomDie(numSides || 6);
},
}
module.exports = root
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}))
app.listen(4000)
console.log('Running a GraphQL API server at localhost:4000/graphql')
我的 random.json
文件:
{
"query": "query RollDice($sides: Int) { getDie(numSides: $sides) { rollOnce roll(numRolls: 3) }}",
"variables": {
"sides": 6
}
}
如果我在此处 运行 此命令:
http http://localhost:4000/graphql < ./random.json
我得到这个输出:
{
"data": {
"getDie": {
"roll": [
1,
6,
2
],
"rollOnce": 5
}
}
}
我的问题是:
如何将 numRolls
的 3
设置为 random.json
文件中的变量?
我试过这个:
{
"query": "query RollDice($sides: Int, $rolls: Int) { getDie(numSides: $sides) { rollOnce roll(numRolls: $rolls) }}",
"variables": {
"sides": 6,
"rolls": 3
}
}
但是出现这个错误:
"message": "Variable \"$rolls\" of type \"Int\" used in position expecting type \"Int!\"."
定义变量时,变量类型必须完全匹配它们要替换的输入类型。虽然您的 $rolls
变量和 numRolls
输入类型都是整数,但您已将 rolls 定义为可为空的整数 (Int),而在您的模式中,您已将输入定义为 "Non-Null" 整数 (Int!)
type RandomDie {
roll(numRolls: Int!): [Int]
}
type Query {
getDie(numSides: Int): RandomDie
}
请注意,numSides
只是一个 Int
,而 numRolls
被定义为 Int!
,这就是为什么 !
不需要 $sides
(事实上使 $sides
成为 Int!
也会抛出错误!)
Non-null 是一个包装器,它告诉 GraphQL 输入不能为 null(对于输入类型)或者返回的字段不能为 null(对于数据类型)。要记住的是,从 GraphQL 的角度来看,非空包装器会将其包装的类型转换为不同的类型,因此 Int
!== Int!
。
我正在研究 GraphQL JS tutorial 并试图了解变量如何与查询一起工作。
在 Object Types section 我可以正常工作:
我的 server.js
文件:
const express = require('express')
const graphqlHTTP = require('express-graphql')
const { buildSchema } = require('graphql')
const app = express()
const schema = buildSchema(`
type RandomDie {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}
type Query {
getDie(numSides: Int): RandomDie
}
`)
class RandomDie {
constructor(numSides) {
this.numSides = numSides;
}
rollOnce() {
return 1 + Math.floor(Math.random() * this.numSides);
}
roll({numRolls}) {
var output = [];
for (var i = 0; i < numRolls; i++) {
output.push(this.rollOnce());
}
return output;
}}
const root = {
getDie: ({numSides}) => {
return new RandomDie(numSides || 6);
},
}
module.exports = root
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}))
app.listen(4000)
console.log('Running a GraphQL API server at localhost:4000/graphql')
我的 random.json
文件:
{
"query": "query RollDice($sides: Int) { getDie(numSides: $sides) { rollOnce roll(numRolls: 3) }}",
"variables": {
"sides": 6
}
}
如果我在此处 运行 此命令:
http http://localhost:4000/graphql < ./random.json
我得到这个输出:
{
"data": {
"getDie": {
"roll": [
1,
6,
2
],
"rollOnce": 5
}
}
}
我的问题是:
如何将 numRolls
的 3
设置为 random.json
文件中的变量?
我试过这个:
{
"query": "query RollDice($sides: Int, $rolls: Int) { getDie(numSides: $sides) { rollOnce roll(numRolls: $rolls) }}",
"variables": {
"sides": 6,
"rolls": 3
}
}
但是出现这个错误:
"message": "Variable \"$rolls\" of type \"Int\" used in position expecting type \"Int!\"."
定义变量时,变量类型必须完全匹配它们要替换的输入类型。虽然您的 $rolls
变量和 numRolls
输入类型都是整数,但您已将 rolls 定义为可为空的整数 (Int),而在您的模式中,您已将输入定义为 "Non-Null" 整数 (Int!)
type RandomDie {
roll(numRolls: Int!): [Int]
}
type Query {
getDie(numSides: Int): RandomDie
}
请注意,numSides
只是一个 Int
,而 numRolls
被定义为 Int!
,这就是为什么 !
不需要 $sides
(事实上使 $sides
成为 Int!
也会抛出错误!)
Non-null 是一个包装器,它告诉 GraphQL 输入不能为 null(对于输入类型)或者返回的字段不能为 null(对于数据类型)。要记住的是,从 GraphQL 的角度来看,非空包装器会将其包装的类型转换为不同的类型,因此 Int
!== Int!
。