GraphCool中的枚举(Enum)如何使用?官方文档缺失
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 }
Recommended Conventions
- Use all uppercase for enum values (this follows standard GraphQL best practices)
- Name enums with an
Enumsuffix (likePublishersEnum) to make their purpose immediately clear - Keep enum values concise and descriptive — avoid overly long or ambiguous labels
内容的提问来源于stack exchange,提问作者Kevin Danikowski

