@@ -36,6 +36,10 @@ This library provides set of strictly typed builders that help you build your sc
3636✔️ Standard way with ` webonyx/graphql-php `
3737
3838``` php
39+ <?php
40+
41+ use GraphQL\Type\Definition\ResolveInfo;
42+
3943$userType = new ObjectType([
4044 'name' => 'User',
4145 'description' => 'Our blog visitor',
@@ -45,16 +49,27 @@ $userType = new ObjectType([
4549 'description' => 'User first name'
4650 ],
4751 'email' => Type::string()
48- ]
52+ ],
53+ 'resolveField' => static function(User $user, $args, $context, ResolveInfo $info) {
54+ switch ($info->fieldName) {
55+ case 'name':
56+ return $user->getName();
57+ case 'email':
58+ return $user->getEmail();
59+ default:
60+ return null;
61+ }
62+ }
4963]);
5064```
5165
5266✨ The same can be produced in objective way
5367
5468``` php
55- use SimPod\GraphQLUtils\Builder\ObjectBuilder;
69+ <?php
5670
57- ...
71+ use GraphQL\Type\Definition\ResolveInfo;
72+ use SimPod\GraphQLUtils\Builder\ObjectBuilder;
5873
5974$userType = ObjectBuilder::create('User')
6075 ->setDescription('Our blog visitor')
@@ -65,6 +80,18 @@ $userType = ObjectBuilder::create('User')
6580 FieldBuilder::create('email', Type::string())
6681 ->build(),
6782 ])
83+ ->setFieldResolver(
84+ static function(User $user, $args, $context, ResolveInfo $info) {
85+ switch ($info->fieldName) {
86+ case 'name':
87+ return $user->getName();
88+ case 'email':
89+ return $user->getEmail();
90+ default:
91+ return null;
92+ }
93+ }
94+ )
6895 ->build();
6996
7097```
@@ -74,9 +101,7 @@ $userType = ObjectBuilder::create('User')
74101✔️ Standard way with ` webonyx/graphql-php `
75102
76103``` php
77- use SimPod\GraphQLUtils\Builder\EnumBuilder;
78-
79- ...
104+ <?php
80105
81106$episodeEnum = new EnumType([
82107 'name' => 'Episode',
@@ -101,6 +126,10 @@ $episodeEnum = new EnumType([
101126✨ The same can be produced in objective way
102127
103128``` php
129+ <?php
130+
131+ use SimPod\GraphQLUtils\Builder\EnumBuilder;
132+
104133$episodeEnum = EnumBuilder::create('Episode')
105134 ->setDescription('One of the films in the Star Wars Trilogy')
106135 ->addValue(4, 'NEWHOPE', 'Released in 1977.')
@@ -115,6 +144,7 @@ $episodeEnum = EnumBuilder::create('Episode')
115144
116145``` php
117146<?php
147+
118148use GraphQL\Type\Definition\InterfaceType;
119149use GraphQL\Type\Definition\Type;
120150
@@ -131,12 +161,12 @@ $character = new InterfaceType([
131161 'description' => 'The name of the character.'
132162 ]
133163 ],
134- 'resolveType' => function ($value) {
164+ 'resolveType' => static function ($value) : object {
135165 if ($value->type === 'human') {
136166 return MyTypes::human();
137- } else {
138- return MyTypes::droid();
139167 }
168+
169+ return MyTypes::droid();
140170 }
141171]);
142172```
@@ -149,7 +179,6 @@ use SimPod\GraphQLUtils\Builder\InterfaceBuilder;
149179use SimPod\GraphQLUtils\Builder\FieldBuilder;
150180use GraphQL\Type\Definition\Type;
151181
152-
153182$character = InterfaceBuilder::create('Character')
154183 ->setDescription('A character in the Star Wars Trilogy')
155184 ->setFields([
@@ -161,10 +190,11 @@ $character = InterfaceBuilder::create('Character')
161190 ->build()
162191 ])
163192 ->setResolveType(
164- function ($value) {
193+ static function ($value) : object {
165194 if ($value->type === 'human') {
166195 return MyTypes::human();
167196 }
197+
168198 return MyTypes::droid();
169199 }
170200 )
@@ -186,12 +216,13 @@ Extending your exception with `SimPod\GraphQLUtils\Error\Error` forces you to im
186216Example Error class
187217
188218``` php
219+ <?php
220+
189221use SimPod\GraphQLUtils\Error\Error;
190- use function sprintf;
191222
192223final class InvalidCustomerIdProvided extends Error
193224{
194- public const TYPE = 'INVALID_CUSTOMER_ID_PROVIDED';
225+ private const TYPE = 'INVALID_CUSTOMER_ID_PROVIDED';
195226
196227 public static function noneGiven() : self
197228 {
@@ -213,9 +244,12 @@ final class InvalidCustomerIdProvided extends Error
213244Create your formatter
214245
215246``` php
247+ <?php
248+
249+ use GraphQL\Error\Error;
216250use SimPod\GraphQLUtils\Error\FormattedError;
217251
218- $formatError = function formatError (Error $error) : array
252+ $formatError = static function (Error $error) : array
219253{
220254 if (!$error->isClientSafe()) {
221255 // eg. log error
@@ -229,13 +263,12 @@ $errorFormatterCallback = static function (Error $error) use ($formatError) : ar
229263};
230264
231265$config = GraphQL::executeQuery(/* $args */)
232- ...
233266 ->setErrorFormatter($errorFormatterCallback)
234267 ->setErrorsHandler(
235268 static function (array $errors, callable $formatter) : array {
236269 return array_map($formatter, $errors);
237270 }
238- )
271+ );
239272```
240273
241274Error types will then be provided in your response so client can easier identify the error type
0 commit comments