我是GraphQL的新手。我知道这是一个基本的问题,但希望有人能帮助我在查询中添加变量,因为我尝试了很多次,但都失败了:(
在我的查询中,使用了以下模式:
type Query {
ContinentInfo(id: ID): Continent
}
type Continent {
id : ID
name: String
countries: [Country]
}
type Country {
id : ID
name : String
population: Float
}以下查询执行成功:
{
ContinentInfo(id: "continent01") {
name
countries {
name
population
}
}
}然后,我想在查询中添加更多条件,例如添加一个变量"populationMoreThan“来过滤结果。因此,查询可能如下所示:
{
ContinentInfo(id: "continent01") {
name
countries(populationMoreThan: $populationMoreThan) {
name
population
}
}
}但是,当我尝试在模式和查询中添加此变量时,它总是失败。有人能给我提供一个在我的例子中添加变量的例子吗?另外,看起来我需要将参数值传递到查询中?现在我使用graphql.GraphQL.execute(queryString)来传递查询字符串。如何在这里传递变量值?
发布于 2019-08-23 17:33:26
终于找到了过滤结果的方法。使用以下命令更新架构:
type Continent {
id : ID
name: String
countries(populationMoreThan: Float = 0): [Country]
}并使用以下命令查询:
{
ContinentInfo(id: "continent01") {
name
countries(populationMoreThan: 1.0) {
name
population
}
}
}https://stackoverflow.com/questions/57621221
复制相似问题