英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论