Enumeration Types
定义枚举类型
枚举类型是一种特殊的标量,它被限制在一组特定的允许值中。
在 graphql-php 中,枚举类型是一个 GraphQL\Type\Definition\EnumType
在构造函数中接受配置数组的实例:
<?php
use GraphQL\Type\Definition\EnumType;
$episodeEnum = new EnumType([
'name' => 'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => [
'NEWHOPE' => [
'value' => 4,
'description' => 'Released in 1977.'
],
'EMPIRE' => [
'value' => 5,
'description' => 'Released in 1980.'
],
'JEDI' => [
'value' => 6,
'description' => 'Released in 1983.'
],
]
]);
本示例对 Enum 类型定义使用内联样式,但你也可以使用 继承或类型语言。
配置说明
枚举类型构造器接收一个数组参数,以下是参数说明:
Option | Type | Notes |
---|---|---|
name | string |
必须。 类型名, 如未提供,默认使用数组键 (参考下文 简约定义 ) |
description | string |
类型的普通文本描述 (如:使用 GraphiQL 自动生成文档) |
values | array |
枚举值列表,参考以下结构 |
每条 values 数组相应地作以下说明:
Option | Type | Notes |
---|---|---|
name | string |
必须。 条目名称,如未提供,默认使用数组键 (参考下文 简约定义 ) |
value | mixed |
应用内部表示的枚举条目值 (可为任一值,包括复杂对象或回调函数) |
description | string |
枚举值的普通文本描述 (如:使用 GraphiQL 自动生成文档) |
deprecationReason | string |
文本描述条目值被拦截原因, 如不为空,条目将不被内部验证返回 (除非强制使用) |
简略定义
如果枚举条目的内部表示与条目名称相同,那么你可以使用下面简略方式来进行定义:
<?php
use GraphQL\Type\Definition\EnumType;
$episodeEnum = new EnumType([
'name' => 'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => ['NEWHOPE', 'EMPIRE', 'JEDI']
]);
这相当于:
<?php
use GraphQL\Type\Definition\EnumType;
$episodeEnum = new EnumType([
'name' => 'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => [
'NEWHOPE' => ['value' => 'NEWHOPE'],
'EMPIRE' => ['value' => 'EMPIRE'],
'JEDI' => ['value' => 'JEDI']
]
]);
这相应的也等同于完整的形式:
<?php
use GraphQL\Type\Definition\EnumType;
$episodeEnum = new EnumType([
'name' => 'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => [
['name' => 'NEWHOPE', 'value' => 'NEWHOPE'],
['name' => 'EMPIRE', 'value' => 'EMPIRE'],
['name' => 'JEDI', 'value' => 'JEDI']
]
]);
字段解决方案
当对象字段为 Enum 条目时,字段解析器应当返回 Enum 条目(配置中的 值)的内部表示。graphql-php 将 值 序列化为 名称 以包含在响应中:
<?php
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\ObjectType;
$episodeEnum = new EnumType([
'name' => 'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => [
'NEWHOPE' => [
'value' => 4,
'description' => 'Released in 1977.'
],
'EMPIRE' => [
'value' => 5,
'description' => 'Released in 1980.'
],
'JEDI' => [
'value' => 6,
'description' => 'Released in 1983.'
],
]
]);
$heroType = new ObjectType([
'name' => 'Hero',
'fields' => [
'appearsIn' => [
'type' => $episodeEnum,
'resolve' => function() {
return 5; // 实际输入的响应为: 'appearsIn' => 'EMPIRE'
}
]
]
]);
当枚举作为输入类型(例如字段参数)时, Reverse (反过来)为 true 。
在传递给应用程序前, GraphQL 将把枚举输入看作 名称 并将其转化为 值 。
例如,给定如下一个对象类型定义:
<?php
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
$heroType = new ObjectType([
'name' => 'Hero',
'fields' => [
'appearsIn' => [
'type' => Type::boolean(),
'args' => [
'episode' => Type::nonNull($enumType)
],
'resolve' => function($_value, $args) {
return $args['episode'] === 5 ? true : false;
}
]
]
]);
然后执行如下查询:
fragment on Hero {
appearsInNewHope: appearsIn(NEWHOPE)
appearsInEmpire: appearsIn(EMPIRE)
}
将会返回:
[
'appearsInNewHope' => false,
'appearsInEmpire' => true
]
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。