This repository was archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathGraphQLField.php
More file actions
84 lines (72 loc) · 1.79 KB
/
GraphQLField.php
File metadata and controls
84 lines (72 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace yii\graphql\base;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ResolveInfo;
use yii\base\Model;
use yii\graphql\GraphQL;
use yii\web\Application;
/**
* GraphQLField类对应于graphql描述文档中的类型系统中每一个节点.如
*
* ```json
* type Person {
* name: String
* age: Int
* picture: Url
* relationship: Person
* }
* ```
* 每一个节点包括了name,type,args,description等
*
* @package yii\graphql\base
*/
class GraphQLField extends GraphQLModel
{
public function type()
{
return null;
}
public function args()
{
return [];
}
protected function getResolver()
{
if (!method_exists($this, 'resolve')) {
return null;
}
$resolver = array($this, 'resolve');
return function () use ($resolver) {
$args = func_get_args();
$this->trigger(GraphQLModel::EVENT_BEFORE_RESOLVE);
return $resolver(...$args);
};
}
/**
* Get the graphql office's description format,that will be used for create GraphQL Object Type.
*
* @param $name
* @param $except
* @return array
*/
public function getAttributes($name = null, $except = null)
{
$attributes = $this->attributes;
$args = $this->args();
$attributes = array_merge([
'args' => $args
], $attributes);
$type = $this->type();
if (isset($type)) {
if(!is_object($type)){
$type = GraphQL::type($type);
}
$attributes['type'] = $type;
}
$resolver = $this->getResolver();
if (isset($resolver)) {
$attributes['resolve'] = $resolver;
}
return $attributes;
}
}