Skip to content

Commit d983337

Browse files
committed
Document UnionBuilder
1 parent 047ea91 commit d983337

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,57 @@ $character = new InterfaceType(
212212
);
213213
```
214214

215+
#### UnionBuilder
216+
217+
✔️ Standard way with `webonyx/graphql-php`
218+
219+
```php
220+
<?php
221+
222+
use GraphQL\Type\Definition\UnionType;
223+
224+
$searchResultType = new UnionType([
225+
'name' => 'SearchResult',
226+
'types' => [
227+
MyTypes::story(),
228+
MyTypes::user()
229+
],
230+
'resolveType' => static function($value) {
231+
if ($value->type === 'story') {
232+
return MyTypes::story();
233+
}
234+
235+
return MyTypes::user();
236+
}
237+
]);
238+
```
239+
240+
✨ The same can be produced in objective way
241+
242+
```php
243+
<?php
244+
245+
use SimPod\GraphQLUtils\Builder\UnionBuilder;
246+
247+
$character = new UnionType(
248+
UnionBuilder::create('SearchResult')
249+
->setTypes([
250+
MyTypes::story(),
251+
MyTypes::user()
252+
])
253+
->setResolveType(
254+
static function($value) {
255+
if ($value->type === 'story') {
256+
return MyTypes::story();
257+
}
258+
259+
return MyTypes::user();
260+
}
261+
)
262+
->build()
263+
);
264+
```
265+
215266
### Types
216267

217268
#### 🕰️ DateTime

0 commit comments

Comments
 (0)