简体   繁体   English

当参数为输入类型时,如何查询GraphQL模式?

[英]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.

相关问题 在 c# 中使用 GraphQL.Net 时,如何访问 GraphQL 字段中的参数值? - How do I access the argument values in GraphQL fields when using GraphQL.Net with c#? 在 GraphQL 架构文档注释中,如何链接到类型? - In a GraphQL schema documentation comment, how do you link to a type? 如何防止 graphql 错误并在模式中按标量类型进行突变? - How to prevent graphql error and do mutation by scalar type in the schema? 如何设置 GraphQL 查询,以便需要一个或另一个参数,但不能同时使用 - How do I set up GraphQL query so one or another argument is required, but not both 使用 Apollo Server 时如何生成 schema.graphql 文件? - How do I generate the schema.graphql file when using Apollo Server? GraphQL 查询仅在输入类型为原始文本时有效 - GraphQL Query Only Works When Input Type is Raw Text 我想如何在neo4j graphql js中使用自定义查询返回关系类型 - How do I suppose to do to return a relation type with custom query in neo4j graphql js 如何为 DynamoDB 创建 GraphQL 查询 - How do I create a GraphQL query for DynamoDB 无法确定名为 GraphQL 的参数的输入类型 - Cannot determine GraphQL input type for argument named 如何在“GraphQL模式语言”中向字段添加描述 - How do I add a description to a field in “GraphQL schema language”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM