graphql - 如何在前端代码中使用 GraphQL 枚举(例如在 <select> 中)?

标签 graphql apollo appsync-apollo-client

我有一个简单的 GraphQL 枚举定义如下:

enum PassType {
    DailyFit
    StarterFit
    MonthlyFit
    QuarterlyFit
    DoubleFit
    MultiFit10
    MultiFit20
}

我想在 <select> 中重用这些值标签。我使用 Apollo 和 AWS Appync 作为 GraphQL 基础设施。有没有办法从 Apollo 客户端获取这些而无需在前端手动复制它们?

最佳答案

您可以使用 introspection查询以获取有关架构中任何特定类型的信息,包括枚举。使用 Apollo 的 Query 组件,这看起来像:

const PASS_TYPE_QUERY = gql`
  query GetEnum {
    __type(name: "PassType") {
      enumValues {
        name
      }
    }
  }
`

<Query query={PASS_TYPE_QUERY}>
  {({ data }) => {
    // Handle loading/errors as usual
    if (!data.__type) {
      return null
    }
    return (
      <select>
        {data.__type.enumValues.map(enumValue => (
          <option value={enumValue.name}>{enumValue.name}</option>
        ))}   
      </select>
    )
  }}
</Query>

关于graphql - 如何在前端代码中使用 GraphQL 枚举(例如在 <select> 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54007916/

相关文章:

graphql - 何时使用 GraphQLID 而不是 GraphQLInt?

Angular Apollo 在突变中使用来自重新获取查询的响应

jms - ActiveMQ、Apollo、Kafka

graphql - AWS AppSync : pass arguments from parent resolver to children

amazon-web-services - AWS AppSync 中订阅的自定义筛选

sql - 基于 Graphql 嵌套游标的分页、解析器和 SQL 查询

typescript - Apollo GraphQL 服务器 + TypeScript

rest - Github GraphQL API : How can I gather specific user's repositories?

graphql - 查询覆盖 Apollo 缓存中缺失的字段

amazon-dynamodb - 针对 Cognito 用户的 AWS AppSync 事件订阅过滤