知识卡片
枚举限制字段取值
GraphQL 枚举用 enum 声明有限的合法值,可让字段只接受约定选项。例如计量单位限定为升、千克或件,避免同一字段混入随意拼写。枚举适合稳定且可列举的集合;若值需要频繁由业务数据扩展,应先评估维护成本。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 8 章《Designing GraphQL APIs》(源文件:_epub-src/OEBPS/Text/08.htm)
- 结论依据:原文定义枚举只能取预定值,并以 MeasureUnit 约束 IngredientRecipe 的单位字段。
- 原始内容:Enumerations are useful in properties that can accept a value only from a constrained list of choices. In GraphQL, we declare enumerations using the enum keyword followed by the enumeration’s name, and we list its allowed values within curly braces.