public function build(SchemaConfig $config) { $fetcher = new CallableFetcher(function ($type, $id) { switch ($type) { case FactionType::TYPE_KEY: return TestDataProvider::getFaction($id); case ShipType::TYPE_KEY: return TestDataProvider::getShip($id); } return 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 ($value = null, $args, $info) { 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 */ }
public function build($config) { $config->addField(new GlobalIdField(self::TYPE_KEY))->addField('factionId', ['type' => new IntType(), 'resolve' => function ($value) { return $value['id']; }])->addField('name', ['type' => TypeMap::TYPE_STRING, 'description' => 'The name of the faction.'])->addField('ships', ['type' => Connection::connectionDefinition(new ShipType()), 'description' => 'The ships used by the faction', 'args' => Connection::connectionArgs(), 'resolve' => function ($value = null, $args = [], $type = null) { return ArrayConnection::connectionFromArray(array_map(function ($id) { return TestDataProvider::getShip($id); }, $value['ships']), $args); }]); }
public function testConnectionDefinition() { $data = ['a', 'b', 'c', 'd', 'e']; $edges = []; foreach ($data as $key => $item) { $edges[] = ArrayConnection::edgeForObjectWithIndex($item, $key); } $this->assertEquals(['edges' => $edges, 'pageInfo' => ['startCursor' => $edges[0]['cursor'], 'endCursor' => $edges[count($edges) - 1]['cursor'], 'hasPreviousPage' => false, 'hasNextPage' => false]], ArrayConnection::connectionFromArray($data)); $this->assertEquals(['edges' => array_slice($edges, 0, 2), 'pageInfo' => ['startCursor' => $edges[0]['cursor'], 'endCursor' => $edges[1]['cursor'], 'hasPreviousPage' => false, 'hasNextPage' => true]], ArrayConnection::connectionFromArray($data, ['first' => 2, 'last' => 4])); }