Completed
Pull Request — master (#204)
by Ryan
11:34
created

StarWarsRelaySchema   B

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 16
dl 0
loc 92
rs 8.4614
c 0
b 0
f 0
1
<?php
2
3
namespace Examples\StarWars;
4
5
use Youshido\GraphQL\Config\Schema\SchemaConfig;
6
use Youshido\GraphQL\Field\InputField;
7
use Youshido\GraphQl\Relay\Connection\ArrayConnection;
8
use Youshido\GraphQL\Relay\Connection\Connection;
9
use Youshido\GraphQL\Relay\Fetcher\CallableFetcher;
10
use Youshido\GraphQL\Relay\Field\NodeField;
11
use Youshido\GraphQL\Relay\RelayMutation;
12
use Youshido\GraphQL\Schema\AbstractSchema;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Scalar\StringType;
16
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