I am getting a bit confused with how to query the following GraphQL schema. I am trying to query pokemons to pull search results, but the fact that PokemonQueryInput! is called in the argument and PokemonConnection! is the result is confusing me. How do I properly add filter as an argument? And how do I properly utilize PokemonConnection?
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
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/ . Types can be declared as arguments by nesting them within { }.
{
pokemons(query: {filter: {type: "Water"}}) {
edges {
name
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.