public static connectionFromArray ( array $data, array $args = [] ) | ||
$data | array | |
$args | array |
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])); }