-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathStarWarsRelaySchema.php
More file actions
108 lines (97 loc) · 4.25 KB
/
StarWarsRelaySchema.php
File metadata and controls
108 lines (97 loc) · 4.25 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Examples\StarWars;
use Youshido\GraphQL\Config\Schema\SchemaConfig;
use Youshido\GraphQL\Exception\ConfigurationException;
use Youshido\GraphQL\Field\InputField;
use Youshido\GraphQl\Relay\Connection\ArrayConnection;
use Youshido\GraphQL\Relay\Connection\Connection;
use Youshido\GraphQL\Relay\Fetcher\CallableFetcher;
use Youshido\GraphQL\Relay\Field\NodeField;
use Youshido\GraphQL\Relay\RelayMutation;
use Youshido\GraphQL\Schema\AbstractSchema;
use Youshido\GraphQL\Type\ListType\ListType;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Scalar\StringType;
class StarWarsRelaySchema extends AbstractSchema
{
/**
* @throws ConfigurationException
*/
public function build(SchemaConfig $config): void
{
$fetcher = new CallableFetcher(
function ($type, $id) {
return match ($type) {
FactionType::TYPE_KEY => TestDataProvider::getFaction($id),
ShipType::TYPE_KEY => TestDataProvider::getShip($id),
default => null,
};
},
function ($object) {
return $object && array_key_exists('ships', $object) ? new FactionType() : new ShipType();
}
);
$config->getQuery()
->addField(new NodeField($fetcher))
->addField('rebels', [
'type' => new FactionType(),
'resolve' => function () {
return TestDataProvider::getFaction('rebels');
}
])
->addField('empire', [
'type' => new FactionType(),
'resolve' => function () {
return TestDataProvider::getFaction('empire');
}
])
->addField('factions', [
'type' => new ListType(new FactionType()),
'args' => [
'names' => [
'type' => new ListType(new StringType())
]
],
'resolve' => function ($args, $info, $value = null) {
return TestDataProvider::getByNames($args['names']);
}
]);
$config->getMutation()
->addField(
RelayMutation::buildMutation(
'introduceShip',
[
new InputField(['name' => 'shipName', 'type' => new NonNullType(new StringType())]),
new InputField(['name' => 'factionId', 'type' => new NonNullType(new StringType())])
],
[
'newShipEdge' => [
'type' => Connection::edgeDefinition(new ShipType(), 'newShip'),
'resolve' => function ($value) {
$allShips = TestDataProvider::getShips();
$newShip = TestDataProvider::getShip($value['shipId']);
return [
'cursor' => ArrayConnection::cursorForObjectInConnection($allShips, $newShip),
'node' => $newShip
];
}
],
'faction' => [
'type' => new FactionType(),
'resolve' => function ($value) {
return TestDataProvider::getFaction($value['factionId']);
}
]
],
function ($value, $args, $info) {
$newShip = TestDataProvider::createShip($args['shipName'], $args['factionId']);
return [
'shipId' => $newShip['id'],
'factionId' => $args['factionId']
];
}
)
);
/** https://github.com/graphql/graphql-relay-js/blob/master/src/__tests__/starWarsSchema.js */
}
}