-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathWrapperType.php
58 lines (51 loc) · 1.71 KB
/
WrapperType.php
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
<?php
declare(strict_types = 1);
namespace Rebing\GraphQL\Tests\Unit\WithTypeTests;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Support\Collection;
use Rebing\GraphQL\Support\Facades\GraphQL;
class WrapperType extends ObjectType
{
/**
* @param string $typeName The type name defined in graphql.php configuration file.
* @param string $customTypeName The new name for wrap type
*/
public function __construct(string $typeName, string $customTypeName)
{
$config = [
'name' => $customTypeName,
'fields' => $this->getMessagesFields($typeName),
];
$underlyingType = GraphQL::type($typeName);
if (isset($underlyingType->config['model'])) {
$config['model'] = $underlyingType->config['model'];
}
parent::__construct($config);
}
/**
* Resolve the wrap type.
*
* @param string $typeName The type name defined in graphql.php configuration file.
*/
protected function getMessagesFields(string $typeName): array
{
return [
'data' => [
'type' => GraphQL::type($typeName),
'resolve' => function ($data) {
return \array_key_exists('data', $data) ?
$data['data'] :
$data;
},
],
'messages' => [
'type' => GraphQLType::listOf(GraphQL::type('SimpleMessageType')),
'description' => 'List of messages',
'resolve' => function ($data): Collection {
return $data['messages'];
},
],
];
}
}