我怎样提供一个变量给查询,该变量可用于指定排序返回数组的字段?

huangapple go评论73阅读模式
英文:

how can I supply a a variable to a query that can be used to specify a field to sort a returned array by?

问题

我已经创建了这个查询,用来从Pokeapi的GraphQL服务器上请求数据,链接在这里 https://beta.pokeapi.co/graphql/console/

query getOrderedPokemon (
  $sortOrder: order_by,
)  {
  pokemon_v2_pokemon(
    limit: 20, 
    order_by: {
      name : $sortOrder
    }
  ) {
    id
    name
    height 
    weight
  }
}

通过这个请求,我可以通过修改变量$sortOrder为'asc'或'desc'来改变返回项目的排序顺序。如果我想按不同的属性进行排序,比如身高或体重,我需要创建一个新的查询吗?

如果服务器不接受一个变量来指定按哪个字段排序,是否不可能创建一个可以按可变属性排序的灵活查询?

根据我所了解的情况,似乎这个服务器确实没有任何可以用来指定按哪个字段排序的变量。

感谢您的提问。

英文:

I have built this query to request data from the Pokeapi graphQL server here https://beta.pokeapi.co/graphql/console/

query getOrderedPokemon (
  $sortOrder: order_by,
)  {
  pokemon_v2_pokemon(
    limit: 20, 
    order_by: {
      name : $sortOrder
    }
  ) {
    id
    name
    height 
    weight
  }
}

With this request, I can modify the order of returned items by changing the variable $sortOrder to either 'asc' or 'desc'. What can I do if I want to sort the returned items by a different property, like height or weight? Do I have to create a new query?

If the server does not accept a variable for specifying which field to sort by, is it impossible to create a flexible query that can be sorted by variable properties?

From what I can tell, it does look like this server doesn't have any variables I can use to specify which field to sort by.

我怎样提供一个变量给查询,该变量可用于指定排序返回数组的字段?

Thank you

答案1

得分: 1

The pokeapi gives you quite a few options which you can see from the GraphiQL interface

To select by height in descending order for example:

query getOrderedPokemon {
  pokemon_v2_pokemon(limit: 20, order_by: {height: desc}) {
    id
    name
    height
    weight
  }
}

To make this more flexible, do:

query getOrderedPokemon($order_by: [pokemon_v2_pokemon_order_by!]) {
  pokemon_v2_pokemon(limit: 20, order_by: $order_by) {
    id
    name
    height
    weight
  }
}

Use {variables: {order_by: [{"height": "asc"}]}} to order by height or {variables: {order_by: [{weight: "asc"}]}} to order by weight.

英文:

The pokeapi gives you quite a few options which you can see from the GraphiQL interface

To select by height in descending order for example:

query getOrderedPokemon {
  pokemon_v2_pokemon(limit: 20, order_by: {height: desc}) {
    id
    name
    height
    weight
  }
}

To make this more flexible, do:

query getOrderedPokemon($order_by: [pokemon_v2_pokemon_order_by!]) {
  pokemon_v2_pokemon(limit: 20, order_by: $order_by) {
    id
    name
    height
    weight
  }
}

Use {variables: {order_by: [{"height": "asc"}]}} to order by height or {variables: {order_by: [{weight: "asc"}]}} to order by weight.

huangapple
  • 本文由 发表于 2023年2月18日 13:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定