【问题标题】:GraphQL-java add variables to queryGraphQL-java 添加变量到查询
【发布时间】:2019-12-28 11:30:35
【问题描述】:

我是 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) 来传递查询字符串。这里如何传递变量值?

【问题讨论】:

    标签: graphql graphql-java


    【解决方案1】:

    终于找到了过滤结果的方法。 更新架构:

    type Continent {
      id  : ID
      name: String
      countries(populationMoreThan: Float = 0): [Country]
    }
    

    并查询:

    {
        ContinentInfo(id: "continent01") {
            name
            countries(populationMoreThan: 1.0) {
                name
                population
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 2019-12-17
      • 2017-08-24
      • 2018-03-02
      • 2019-10-02
      • 2019-05-17
      • 2023-03-12
      • 2020-09-10
      • 2018-04-08
      相关资源
      最近更新 更多