You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GraphCool中的枚举(Enum)如何使用?官方文档缺失

Working with Enums in GraphCool

Since GraphCool's official docs don't cover enums in much detail, I wanted to share some key details and practical examples for anyone working with them:

What are Enums?

Enums are field parameters that can only take one of a predefined set of values. They’re ideal for scenarios where you need to restrict input to a specific list of options (like publisher names, status types, etc.).

Critical GraphCool Requirement

GraphCool requires every enum field to have a default value set — this is a easy-to-miss detail given the sparse official documentation.

Example Implementations

Defining an Enum

In the GraphCool console’s Enums tab, define your enum like this:

enum PublishersEnum {
  PEARSON
  WILEY
  MCGRAWHILL
}

Using the Enum in a Type

When building a type that uses your enum, be sure to add the @defaultValue directive to set a default option:

type Book {
  title: String!
  author: String!
  publisher: PublishersEnum @defaultValue(value: PEARSON)
  # Add other relevant fields here
}
  • Use all uppercase for enum values (this follows standard GraphQL best practices)
  • Name enums with an Enum suffix (like PublishersEnum) to make their purpose immediately clear
  • Keep enum values concise and descriptive — avoid overly long or ambiguous labels

内容的提问来源于stack exchange,提问作者Kevin Danikowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:04:14