digiaonline / graphql-relay
该软件包已弃用且不再维护。未建议替代包。
digiaonline/graphql的Relay支持
dev-master
2018-09-10 18:41 UTC
Requires
- php: >=7.1
Requires (Dev)
- digiaonline/graphql: dev-master
- phpstan/phpstan: ^0.10
- phpunit/phpunit: ^7.1
This package is auto-updated.
Last update: 2023-03-29 00:41:46 UTC
README
Relay支持我们的GraphQL实现。
要求
- PHP >= 7.1
用法
安装
运行以下命令通过Composer安装软件包
composer require digiaonline/graphql-relay:dev-master
示例
执行此脚本
use function Digia\GraphQL\buildSchema; use function Digia\GraphQL\graphql; $source = file_get_contents(__DIR__ . '/star-wars.graphqls'); $schema = buildSchema($source, [ 'Query' => [ 'rebels' => function () { return rebels(); }, 'empire' => function () { return empire(); } ], 'Faction' => [ 'ships' => function ($faction, $args) { $data = getShips($faction); $arguments = ConnectionArguments::fromArray($args); return ArrayConnectionBuilder::fromArray($data, $arguments); } ] ]); $result = graphql($schema, ' query RebelsShipsQuery { rebels { name, ships(first: 1) { edges { node { name } } } } }'); print_r($result);
生成以下输出
Array ( [rebels] => Array ( [name] => Alliance to Restore the Republic [ships] => Array ( [edges] => Array ( [0] => Array ( [node] => Array ( [name] => X-Wing ) ) ) ) ) )
使用的模式定义如下所示
"A connection to a list of items." interface Connection { "A list of edges." edges: [Edge] "Information to aid in pagination." pageInfo: PageInfo! } "An edge in a connection." interface Edge { "A cursor for use in pagination." cursor: String! "The item at the end of the edge." node: Node } "An object with an ID." interface Node { "ID of the object." id: ID! } "Information about pagination in a connection." type PageInfo { "When paginating forwards, are there more items?" hasPreviousPage: Boolean! "When paginating backwards, are there more items?" hasNextPage: Boolean! "When paginating backwards, the cursor to continue." endCursor: String "When paginating forwards, the cursor to continue." startCursor: String } type Faction implements Node { "The ID of an object." id: ID! "The name of the faction." name: String "The ships used by the faction." ships(after: String, before: String, first: Int, last: Int): ShipConnection } "A ship in the Star Wars saga" type Ship implements Node { "The ID of an object." id: ID! "The name of the ship." name: String } type ShipConnection implements Connection { edges: [ShipEdge] pageInfo: PageInfo! } type ShipEdge implements Edge { cursor: String! node: Ship } type Query { rebels: Faction empire: Faction node(id: ID!): Node } schema { query: Query }
节点根字段
为了实现节点根字段,提供了一个便利类
将类型和ID转换为全局ID
$nodeId = Node::toGlobalId('Ship', '1');
返回一个全局ID,可以传递给节点根
U2hpcDox
将全局ID转换回类型和ID
$node = Node::fromGlobalId('U2hpcDox');
返回一个可以查询的对象
$node->getType(); // Ship $node->getId(); // 1
节点根解析器
有关如何实现节点根解析器的示例,请参阅StarWarsConnectionTest.php
贡献
请阅读我们的指南。
许可
请参阅LICENSE。