public function setup() { $this->allUsers = [['name' => 'Dan', 'friends' => [1, 2, 3, 4]], ['name' => 'Nick', 'friends' => [0, 2, 3, 4]], ['name' => 'Lee', 'friends' => [0, 1, 3, 4]], ['name' => 'Joe', 'friends' => [0, 1, 2, 4]], ['name' => 'Tim', 'friends' => [0, 1, 2, 3]]]; $this->userType = new ObjectType(['name' => 'User', 'fields' => function () { return ['name' => ['type' => Type::string()], 'friends' => ['type' => $this->friendConnection, 'args' => Connection::connectionArgs(), 'resolve' => function ($user, $args) { return ArrayConnection::connectionFromArray($user['friends'], $args); }], 'friendsForward' => ['type' => $this->userConnection, 'args' => Connection::forwardConnectionArgs(), 'resolve' => function ($user, $args) { return ArrayConnection::connectionFromArray($user['friends'], $args); }], 'friendsBackward' => ['type' => $this->userConnection, 'args' => Connection::backwardConnectionArgs(), 'resolve' => function ($user, $args) { return ArrayConnection::connectionFromArray($user['friends'], $args); }]]; }]); $this->friendEdge = Connection::createEdgeType(['name' => 'Friend', 'nodeType' => $this->userType, 'resolveNode' => function ($edge) { return $this->allUsers[$edge['node']]; }, 'edgeFields' => function () { return ['friendshipTime' => ['type' => Type::string(), 'resolve' => function () { return 'Yesterday'; }]]; }]); $this->friendConnection = Connection::createConnectionType(['name' => 'Friend', 'nodeType' => $this->userType, 'edgeType' => $this->friendEdge, 'connectionFields' => function () { return ['totalCount' => ['type' => Type::int(), 'resolve' => function () { return count($this->allUsers) - 1; }]]; }]); $this->userEdge = Connection::createEdgeType(['nodeType' => $this->userType, 'resolveNode' => function ($edge) { return $this->allUsers[$edge['node']]; }]); $this->userConnection = Connection::createConnectionType(['nodeType' => $this->userType, 'edgeType' => $this->userEdge]); $this->queryType = new ObjectType(['name' => 'Query', 'fields' => function () { return ['user' => ['type' => $this->userType, 'resolve' => function () { return $this->allUsers[0]; }]]; }]); $this->schema = new Schema(['query' => $this->queryType]); }
/** * Return the cursor associated with an object in an array. * * @param array $data * @param $object * @return null|string */ public static function cursorForObjectInConnection(array $data, $object) { return ArrayConnection::cursorForObjectInConnection($data, $object); }
public function testWorksWithAnUndersizedArraySliceBothSides() { $connection = ArrayConnection::connectionFromArraySlice(array_slice($this->letters, 3, 1), ['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='], ['sliceStart' => 3, 'arrayLength' => 5]); $expected = array('edges' => array(0 => array('node' => 'D', 'cursor' => 'YXJyYXljb25uZWN0aW9uOjM=')), 'pageInfo' => array('startCursor' => 'YXJyYXljb25uZWN0aW9uOjM=', 'endCursor' => 'YXJyYXljb25uZWN0aW9uOjM=', 'hasPreviousPage' => false, 'hasNextPage' => true)); $this->assertEquals($expected, $connection); }