[英]How do I query GraphQL schema when the argument is an input type?
I am getting a bit confused with how to query the following GraphQL schema. 我对如何查询以下GraphQL模式感到困惑。 I am trying to query pokemons to pull search results, but the fact that PokemonQueryInput! 我正在尝试查询pokemons以获取搜索结果,但事实是PokemonQueryInput! is called in the argument and PokemonConnection! 在参数和PokemonConnection中被调用! is the result is confusing me. 结果使我感到困惑。 How do I properly add filter as an argument? 如何正确添加过滤器作为参数? And how do I properly utilize PokemonConnection? 以及如何正确利用PokemonConnection?
schema.graphql schema.graphql
type Query {
pokemons(query: PokemonsQueryInput!): PokemonConnection!
pokemonByName(name: String!): Pokemon
pokemonById(id: ID!): Pokemon
pokemonTypes: [String!]!
}
input PokemonsQueryInput {
limit: Int = 10
offset: Int = 0
search: String
filter: PokemonFilterInput
}
input PokemonFilterInput {
type: String
isFavorite: Boolean
}
type PokemonConnection {
limit: Int!
offset: Int!
count: Int!
edges: [Pokemon!]!
}
type Pokemon {
id: ID!
number: Int!
name: String!
...
}
index.js index.js
pokemons: (__, args) => {
const { limit, offset, search, filter } = args.query;
let pokemons = pokemonsData;
if (search) {
const regex = new RegExp(search, 'i');
pokemons = _.filter(pokemons, p => p.name.match(regex));
}
if (filter) {
if (filter.type) {
const regex = new RegExp(filter.type, 'i');
pokemons = _.filter(pokemons,p => _.some(p.types, t => t.match(regex)));
}
if (filter.isFavorite) {
pokemons = _.filter(pokemons, p => !!favorites.get(p.id));
}
}
const count = pokemons.length;
const edges = pokemons.slice(offset, offset + limit);
return {
limit,
offset,
count,
edges
}
},
I located the solution for how to do this via the GraphQL documentation on Object types: https://graphql.org/graphql-js/object-types/ . 我通过有关对象类型的GraphQL文档找到了解决方案,方法是: https ://graphql.org/graphql-js/object-types/。 Types can be declared as arguments by nesting them within { }. 通过将类型嵌套在{}中,可以将类型声明为参数。
{
pokemons(query: {filter: {type: "Water"}}) {
edges {
name
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.