1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\GraphQL; |
6
|
|
|
use Digia\GraphQL\Language\Node\BooleanValueNode; |
7
|
|
|
use Digia\GraphQL\Language\Node\FloatValueNode; |
8
|
|
|
use Digia\GraphQL\Language\Node\IntValueNode; |
9
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
10
|
|
|
use Digia\GraphQL\Language\Node\StringValueNode; |
11
|
|
|
use Digia\GraphQL\Type\Definition\TypeNameEnum; |
12
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
13
|
|
|
use function Digia\GraphQL\Util\coerceBoolean; |
14
|
|
|
use function Digia\GraphQL\Util\coerceFloat; |
15
|
|
|
use function Digia\GraphQL\Util\coerceInt; |
16
|
|
|
use function Digia\GraphQL\Util\coerceString; |
17
|
|
|
|
18
|
|
|
class ScalarTypesProvider extends AbstractServiceProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $provides = [ |
24
|
|
|
GraphQL::BOOLEAN, |
25
|
|
|
GraphQL::FLOAT, |
26
|
|
|
GraphQL::INT, |
27
|
|
|
GraphQL::ID, |
28
|
|
|
GraphQL::STRING, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function register() |
35
|
|
|
{ |
36
|
|
|
$this->container->add(GraphQL::BOOLEAN, function () { |
37
|
|
|
return GraphQLScalarType([ |
38
|
|
|
'name' => TypeNameEnum::BOOLEAN, |
39
|
|
|
'description' => 'The `Boolean` scalar type represents `true` or `false`.', |
40
|
|
|
'serialize' => function ($value) { |
41
|
|
|
return coerceBoolean($value); |
42
|
|
|
}, |
43
|
|
|
'parseValue' => function ($value) { |
44
|
|
|
return coerceBoolean($value); |
45
|
|
|
}, |
46
|
|
|
|
47
|
|
|
'parseLiteral' => function (NodeInterface $node) { |
48
|
|
|
if ($node instanceof BooleanValueNode) { |
49
|
|
|
return $node->getValue(); |
50
|
|
|
} |
51
|
|
|
return null; |
52
|
|
|
}, |
53
|
|
|
]); |
54
|
|
|
}, true/* $shared */); |
55
|
|
|
|
56
|
|
|
$this->container->add(GraphQL::FLOAT, function () { |
57
|
|
|
return GraphQLScalarType([ |
58
|
|
|
'name' => TypeNameEnum::FLOAT, |
59
|
|
|
'description' => |
60
|
|
|
'The `Float` scalar type represents signed double-precision fractional ' . |
61
|
|
|
'values as specified by ' . |
62
|
|
|
'[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point).', |
63
|
|
|
'serialize' => function ($value) { |
64
|
|
|
return coerceFloat($value); |
65
|
|
|
}, |
66
|
|
|
'parseValue' => function ($value) { |
67
|
|
|
return coerceFloat($value); |
68
|
|
|
}, |
69
|
|
|
'parseLiteral' => function (NodeInterface $node) { |
70
|
|
|
if ($node instanceof FloatValueNode || $node instanceof IntValueNode) { |
71
|
|
|
return $node->getValue(); |
72
|
|
|
} |
73
|
|
|
return null; |
74
|
|
|
}, |
75
|
|
|
]); |
76
|
|
|
}, true/* $shared */); |
77
|
|
|
|
78
|
|
|
$this->container->add(GraphQL::INT, function () { |
79
|
|
|
return GraphQLScalarType([ |
80
|
|
|
'name' => TypeNameEnum::INT, |
81
|
|
|
'description' => |
82
|
|
|
'The `Int` scalar type represents non-fractional signed whole numeric ' . |
83
|
|
|
'values. Int can represent values between -(2^31) and 2^31 - 1.', |
84
|
|
|
'serialize' => function ($value) { |
85
|
|
|
return coerceInt($value); |
86
|
|
|
}, |
87
|
|
|
'parseValue' => function ($value) { |
88
|
|
|
return coerceInt($value); |
89
|
|
|
}, |
90
|
|
|
'parseLiteral' => function (NodeInterface $node) { |
91
|
|
|
if ($node instanceof IntValueNode) { |
92
|
|
|
$value = (int)$node->getValue(); |
93
|
|
|
if ($node->getValue() === (string)$value && $value <= PHP_INT_MAX && $value >= PHP_INT_MIN) { |
94
|
|
|
return $value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
}, |
99
|
|
|
]); |
100
|
|
|
}, true/* $shared */); |
101
|
|
|
|
102
|
|
|
$this->container->add(GraphQL::ID, function () { |
103
|
|
|
return GraphQLScalarType([ |
104
|
|
|
'name' => TypeNameEnum::ID, |
105
|
|
|
'description' => |
106
|
|
|
'The `ID` scalar type represents a unique identifier, often used to ' . |
107
|
|
|
'refetch an object or as key for a cache. The ID type appears in a JSON ' . |
108
|
|
|
'response as a String; however, it is not intended to be human-readable. ' . |
109
|
|
|
'When expected as an input type, any string (such as `"4"`) or integer ' . |
110
|
|
|
'(such as `4`) input value will be accepted as an ID.', |
111
|
|
|
'serialize' => function ($value) { |
112
|
|
|
return coerceString($value); |
113
|
|
|
}, |
114
|
|
|
'parseValue' => function ($value) { |
115
|
|
|
return coerceString($value); |
116
|
|
|
}, |
117
|
|
|
'parseLiteral' => function (NodeInterface $node) { |
118
|
|
|
if ($node instanceof StringValueNode || $node instanceof IntValueNode) { |
119
|
|
|
return $node->getValue(); |
120
|
|
|
} |
121
|
|
|
return null; |
122
|
|
|
}, |
123
|
|
|
]); |
124
|
|
|
}, true/* $shared */); |
125
|
|
|
|
126
|
|
|
$this->container->add(GraphQL::STRING, function () { |
127
|
|
|
return GraphQLScalarType([ |
128
|
|
|
'name' => TypeNameEnum::STRING, |
129
|
|
|
'description' => |
130
|
|
|
'The `String` scalar type represents textual data, represented as UTF-8 ' . |
131
|
|
|
'character sequences. The String type is most often used by GraphQL to ' . |
132
|
|
|
'represent free-form human-readable text.', |
133
|
|
|
'serialize' => function ($value) { |
134
|
|
|
return coerceString($value); |
135
|
|
|
}, |
136
|
|
|
'parseValue' => function ($value) { |
137
|
|
|
return coerceString($value); |
138
|
|
|
}, |
139
|
|
|
'parseLiteral' => function (NodeInterface $node) { |
140
|
|
|
if ($node instanceof StringValueNode) { |
141
|
|
|
return $node->getValue(); |
142
|
|
|
} |
143
|
|
|
return null; |
144
|
|
|
}, |
145
|
|
|
]); |
146
|
|
|
}, true/* $shared */); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|