1. Go to this page and download the library: Download kuandd/graphql-relay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kuandd / graphql-relay example snippets
use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Traits\IsNodeQuery;
use GraphQLResolve\AbstractQuery;
class OrderQuery extends AbstractQuery
{
use IsNodeQuery;
/**
* 查询名
*
* @return string
*/
public function name(): string
{
return 'order';
}
/**
* 返回类型
*
* @return \GraphQL\Type\Definition\ObjectType
*/
public function type()
{
return OrderType::getObject();
}
/**
* 解析
*
* @return Closure
*/
public function resolve(): Closure
{
return function ($root, $args, $context, ResolveInfo $resolveInfo) {
return [
'id' => '1',
'sn' => 'abc',
];
};
}
}
use GraphQL\Type\Definition\Type;
use GraphQLResolve\AbstractObjectType;
use GraphQLResolve\Contracts\HasInterface;
use GraphQLRelay\Types\NodeInterface;
/**
* 模拟订单类型
*
* Class Order
* @package GraphQLRelay\Tests\Sim
*/
class OrderType extends AbstractObjectType
implements HasInterface
{
/**
* 类型名
*
* @return string
*/
public function name(): string
{
return 'Order';
}
/**
* 获取字段
*
* @return \Closure|mixed
*/
public function fields()
{
return function () {
return NodeInterface::mergeFields([
[
'name' => 'sn',
'type' => Type::string(),
],
]);
};
}
/**
* 实现接口
*
* @return array
*/
public function implements(): array
{
return [
NodeInterface::getObject(),
];
}
}
use GraphQLRelay\Queries\Node;
use GraphQLResolve\AbstractObjectType;
/**
* 根查询
*
* Class Query
* @package GraphQLRelay\Tests\Sim
*/
class Query extends AbstractObjectType
{
/**
* 字段
*
* @return \Closure|mixed
*/
public function fields()
{
return function () {
return [
OrderQuery::fetchOptions(),
Node::fetchOptions(),
];
};
}
}