对象类型
对象类型定义
对象类型是典型的 GraphQL 应用程序中使用最频繁的基元。
概念上对象类型是字段的集合。反过来,每个领域都有其自己的类型,允许构建复杂的层次结构。
在 graphql-php 中对象类型中是 GraphQL\Type\Definition\ObjectType
(或其中一个子类)的实例,它接受构造函数中的配置数组:
<?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Examples\Blog\Data\DataSource;
use GraphQL\Examples\Blog\Data\Story;
$userType = new ObjectType([
'name' => 'User',
'description' => 'Our blog visitor',
'fields' => [
'firstName' => [
'type' => Type::string(),
'description' => 'User first name'
],
'email' => Type::string()
]
]);
$blogStory = new ObjectType([
'name' => 'Story',
'fields' => [
'body' => Type::string(),
'author' => [
'type' => $userType,
'description' => 'Story author',
'resolve' => function(Story $blogStory) {
return DataSource::findUser($blogStory->authorId);
}
],
'likes' => [
'type' => Type::listOf($userType),
'description' => 'List of users who liked the story',
'args' => [
'limit' => [
'type' => Type::int(),
'description' => 'Limit the number of recent likes returned',
'defaultValue' => 10
]
],
'resolve' => function(Story $blogStory, $args) {
return DataSource::findLikes($blogStory->id, $args['limit']);
}
]
]
]);
本示例使用内联样式来定义对象类型,你也可以使用 继承或类型语言。
配置选项
对象类型构造函数的数组配置选项。 以下是可用选项的完整列表:
Option | Type | Notes |
---|---|---|
name | string |
必须。 Schema 中此对象的唯一名称 |
fields | array or callable |
必须。 描述对象字段或可调用返回此类数组的数组。见 Fields 下面的部分是每个数组被期望的入口结构。何时使用此选项可调用的说明,另见章节 Circular types。 |
description | string |
呈现于客户端的参数文本说明(例如:用于 GraphiQL 自动生成文档 ) |
interfaces | array or callable |
此类型实现的接口列表或返回此类列表的可调用接口。详情见 Interface Types。何时使用此选项可调用的说明,另见章节 Circular types。 |
isTypeOf | callable |
function($value, $context, ResolveInfo $info) 如果 $value 符合这个类型,期望返回 true。 (解释见 Abstract Type Resolution) |
resolveField | callable |
function($value, $args, $context, ResolveInfo $info) 此类型将得到值 $value,它应该返回定义的字段的值 $info->fieldName。 定义一个特定类型的字段解析的好方法。详情另见 Data Fetching。 |
字段配置说明
下面是字段配置的完全说明:
Option | Type | Notes |
---|---|---|
name | string |
必须。 字段名称。为空时,间接使用 fields 数组键 (参阅下面 简约字段定义 ) |
type | Type |
必须。 内部或自定义类型实例。 注意 : 类型必须在 schema 中用单一实例呈现 (详见 类型注册) |
args | array |
任意类型的参数数组。 每条都应是使用 name, type, description, defaultValue 这些键值的数组。 查看下面小节 字段参数 。 |
resolve | callable |
function($value, $args, $context, ResolveInfo $info) 函数中的 $value 应当返回当前字段的实际值。详情参阅 数据获取 |
complexity | callable |
function($childrenComplexity, $args) 用来限制查询的复杂性。 默认不使用,参阅 安全性 以获取更多信息。 |
description | string |
呈现于客户端的字段文本说明 (例如:GraphiQL 用于自动生成的文档) |
deprecationReason | string |
文本描述不推荐使用此字段的原因。不为空时,自检查询不会返回字段 (除非强制) |
字段参数
GraphQL 对象类型上的所有字段都有 0 个或多个参数,使用在 args 的字段定义上。每个参数数组参考以下说明:
Option | Type | Notes |
---|---|---|
name | string |
必须。 参数名称。 为空时,使用 args 数组键值 |
type | Type |
必须。 输入类型 (scalar, enum, InputObjectType + 其中任意组合及 nonNull 和 listOf 修改器) 的实例 |
description | string |
呈现于客户端的参数文本说明 (例如:用于 GraphiQL 自动生成文档) |
defaultValue | scalar |
当前参数默认值 |
简约字段定义
字段定义也可以使用 shorthand 的方式进行表示 (只允许 name 和 type ):
'fields' => [
'id' => Type::id(),
'fieldName' => $fieldType
]
相当于:
'fields' => [
'id' => ['type' => Type::id()],
'fieldName' => ['type' => $fieldName]
]
相应的,也如同完整定义:
'fields' => [
['name' => 'id', 'type' => Type::id()],
['name' => 'fieldName', 'type' => $fieldName]
]
同样的简约表达方式也可用于字段参数。
循环和循环类型
几乎所有的实际开发的应用程序都包含循环或循环类型。例如,考虑一下用户的朋友信息或嵌套的评论信息。
graphql-php 允许这样的类型,但你必须在字段(和 /或 接口中)使用 callable
。
例如:
<?php
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
$userType = null;
$userType = new ObjectType([
'name' => 'User',
'fields' => function() use (&$userType) {
return [
'email' => [
'type' => Type::string()
],
'friends' => [
'type' => Type::listOf($userType)
]
];
}
]);
使用 TypeRegistry 的 类型定义的继承样式 也是同样的例子。
<?php
namespace MyApp;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
class UserType extends ObjectType
{
public function __construct()
{
$config = [
'fields' => function() {
return [
'email' => MyTypes::string(),
'friends' => MyTypes::listOf(MyTypes::user())
];
}
];
parent::__construct($config);
}
}
class MyTypes
{
private static $user;
public static function user()
{
return self::$user ?: (self::$user = new UserType());
}
public static function string()
{
return Type::string();
}
public static function listOf($type)
{
return Type::listOf($type);
}
}
字段解析
字段解析是 graphql-php 中返回对应字段真实数据的核心机制。
它被类型定义中的 resolveField 或字段定义中的 resolve(推荐使用)所继承。
参阅 数据获取 获取本节完整描述。
自定义元数据
在 graphql-php 中,所有类型都会接收一个配置数组,在一些例子中,你可能更倾向于使用自定义元数据来定义类型或字段。
graphql-php 包含原始配置数组,存放在每个类型或字段实例的 $config 公共属性中。使用它可以实现应用级的映射和定义。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。