1 | <?php |
||
17 | class StarWarsRelaySchema extends AbstractSchema |
||
18 | { |
||
19 | public function build(SchemaConfig $config) |
||
20 | { |
||
21 | $fetcher = new CallableFetcher( |
||
22 | function ($type, $id) { |
||
23 | switch ($type) { |
||
24 | case FactionType::TYPE_KEY: |
||
25 | return TestDataProvider::getFaction($id); |
||
26 | |||
27 | case |
||
28 | ShipType::TYPE_KEY: |
||
29 | return TestDataProvider::getShip($id); |
||
30 | } |
||
31 | |||
32 | return null; |
||
33 | }, |
||
34 | function ($object) { |
||
35 | return $object && array_key_exists('ships', $object) ? new FactionType() : new ShipType(); |
||
36 | } |
||
37 | ); |
||
38 | |||
39 | $config->getQuery() |
||
40 | ->addField(new NodeField($fetcher)) |
||
41 | ->addField('rebels', [ |
||
42 | 'type' => new FactionType(), |
||
43 | 'resolve' => function () { |
||
44 | return TestDataProvider::getFaction('rebels'); |
||
45 | } |
||
46 | ]) |
||
47 | ->addField('empire', [ |
||
48 | 'type' => new FactionType(), |
||
49 | 'resolve' => function () { |
||
50 | return TestDataProvider::getFaction('empire'); |
||
51 | } |
||
52 | ]) |
||
53 | ->addField('factions', [ |
||
54 | 'type' => new ListType(new FactionType()), |
||
55 | 'args' => [ |
||
56 | 'names' => [ |
||
57 | 'type' => new ListType(new StringType()) |
||
58 | ] |
||
59 | ], |
||
60 | 'resolve' => function ($value = null, $args, $info) { |
||
61 | return TestDataProvider::getByNames($args['names']); |
||
62 | } |
||
63 | ]); |
||
64 | |||
65 | |||
66 | $config->getMutation() |
||
67 | ->addField( |
||
68 | RelayMutation::buildMutation( |
||
69 | 'introduceShip', |
||
70 | [ |
||
71 | new InputField(['name' => 'shipName', 'type' => new NonNullType(new StringType())]), |
||
72 | new InputField(['name' => 'factionId', 'type' => new NonNullType(new StringType())]) |
||
73 | ], |
||
74 | [ |
||
75 | 'newShipEdge' => [ |
||
76 | 'type' => Connection::edgeDefinition(new ShipType(), 'newShip'), |
||
77 | 'resolve' => function ($value) { |
||
78 | $allShips = TestDataProvider::getShips(); |
||
79 | $newShip = TestDataProvider::getShip($value['shipId']); |
||
80 | |||
81 | return [ |
||
82 | 'cursor' => ArrayConnection::cursorForObjectInConnection($allShips, $newShip), |
||
83 | 'node' => $newShip |
||
84 | ]; |
||
85 | } |
||
86 | ], |
||
87 | 'faction' => [ |
||
88 | 'type' => new FactionType(), |
||
89 | 'resolve' => function ($value) { |
||
90 | return TestDataProvider::getFaction($value['factionId']); |
||
91 | } |
||
92 | ] |
||
93 | ], |
||
94 | function ($value, $args, $info) { |
||
95 | $newShip = TestDataProvider::createShip($args['shipName'], $args['factionId']); |
||
96 | |||
97 | return [ |
||
98 | 'shipId' => $newShip['id'], |
||
99 | 'factionId' => $args['factionId'] |
||
100 | ]; |
||
101 | } |
||
102 | ) |
||
103 | ); |
||
104 | |||
105 | /** https://github.com/graphql/graphql-relay-js/blob/master/src/__tests__/starWarsSchema.js */ |
||
106 | } |
||
107 | |||
108 | } |
||
109 |