php - 如何在 graphql-laravel 中使用 UnionType?
问题描述
我的问题是 select *
for table,这会禁用 with 或 select 函数,并n+1
导致查询决定我是否与另一个表有关系的问题。它还禁用了减少查询的 GraphQL 属性
class ProductsQuery extends Query
{
protected $attributes = [
'name' => 'products',
];
public function type(): Type
{
// return Type::nonNull(GraphQL::paginate('Product'));
return Type::nonNull(GraphQL::type('ProductPaginateOrList'));
}
public function args(): array
{
return [
// start paginate
'paginate' => [
'name' => 'paginate',
'type' => Type::boolean(),
'rules' => ['boolean'],
'defaultValue' => true,
],
'paginateLimit' => [
'name' => 'paginateLimit',
'type' => Type::int(),
'rules' => ['required_if:paginate,true'],
'defaultValue' => 15,
],
'page' => [
'name' => 'page',
'type' => Type::int(),
'rules' => ['required_if:paginate,true'],
],
// end paginate
];
}
public function resolve($root, array $args, $context, ResolveInfo $info, SelectFields $fields)
{
$select = $fields->getSelect();
$with = $fields->getRelations();
return Product::query()
->select($select)
->with($with)
->when($args['paginate'], function ($q) use ($args) {
return $q->paginate($args['paginateLimit'], ['*'], 'page', $args['page']);
}, function ($q) {
return $q->get();
});
}
}
class ProductPaginateOrList extends UnionType
{
protected $attributes = [
'name' => 'ProductPaginateOrList',
'description' => 'Products with paginator or collection of products',
];
public function types(): array
{
return [
GraphQL::paginate('Product'),
GraphQL::wrapType(
'Product',
'ProductData',
DataType::class
)
];
}
public function resolveType($root): Type
{
if ($root instanceof LengthAwarePaginator) {
return GraphQL::paginate('Product');
} elseif ($root instanceof Collection) {
return GraphQL::wrapType(
'Product',
'ProductData',
DataType::class
);
}
return null;
}
}
class DataType extends ObjectType
{
public function __construct(string $typeName, string $customName = null)
{
$name = $customName ?: $typeName . 'Data';
$config = [
'name' => $name,
'fields' => $this->getDataFields($typeName),
];
$underlyingType = GraphQL::type($typeName);
if (isset($underlyingType->config['model'])) {
$config['model'] = $underlyingType->config['model'];
}
parent::__construct($config);
}
protected function getDataFields(string $typeName): array
{
return [
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'description' => 'List of items',
'resolve' => function ($data) {
return $data;
},
'selectable' => true,
],
];
}
}
版本:
graphql-laravel 版本:7.2
Laravel 版本:8.61
PHP版本:8
解决方案
推荐阅读
- cucumber - 在黄瓜测试中捕获 HTTP 响应以用于不同的步骤定义
- algorithm - O(log n) 与 O(1)
- mysql - MySQL 事件比预定时间晚两个小时执行
- amazon-web-services - AWS OpsWorks“start_failed”状态并且没有日志?
- r - 从两个数据框构建绘图时的两个图例
- node.js - 如果在后台进程中,AWS 上的 Websocket 连接失败
- c++ - 我的 oat ++ 项目的构建导致不参考我的方法的问题
- php - 突出显示或识别 php / css 中的某些字符,其中数据来自 CSV?
- anonymous - OpenSeadragon 本地 html 错误:crossOriginPolicy: 'Anonymous' 使演示图像在本地 html 中不可见
- sockets - TCP 与 SOCK_STREAM 以及 UDP 与 SOCK_DGRAM 的关系