假设您有GraphQL类型,它包含许多字段.如何在不写下包含所有字段名称的长查询的情况下查询所有字段?
例如,如果我有这些字段:
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user'
],
'username' => [
'type' => Type::string(),
'description' => 'The email of user'
],
'count' => [
'type' => Type::int(),
'description' => 'login count for the user'
]
];
}
Run Code Online (Sandbox Code Playgroud)
要查询所有字段,查询通常是这样的:
FetchUsers{users(id:"2"){id,username,count}}
Run Code Online (Sandbox Code Playgroud)
但我想要一种方法来获得相同的结果,而无需编写所有字段,如下所示:
FetchUsers{users(id:"2"){*}}
//or
FetchUsers{users(id:"2")}
Run Code Online (Sandbox Code Playgroud)
有没有办法在GraphQL中执行此操作?
我正在使用Folkloreatelier/laravel-graphql库.
我试图弄清楚如何使用 GraphQL 在父子关系中实现自下而上的过滤。
考虑以下对停车场和可用停车位进行建模的基本类型层次结构:
type ParkingLot {
id: Int!
spots: [ParkingSpot]
}
type ParkingSpot {
id: Int!,
occupied: Boolean!
}
Run Code Online (Sandbox Code Playgroud)
我想以一种使我能够检索所有至少有一个免费停车位的停车场的方式实现我的解析器。
query AllParkingLotsWithStatus($status : Boolean = false) {
ParkingLot {
id
spots(occupied: $status) {
id
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用的 GraphQL 的具体实现是基于 PHP 的,可在此处获得,但任何基于 JS 的实现都非常有助于为我指明正确的方向。我知道有一些库支持使用“where:”参数进行过滤,但我必须自己实现这种机制,因为我使用的库没有提供开箱即用的功能。
我探索的一个想法是在 ParkingLot 的解析器中简单地编写一个 JOIN 语句,但我觉得这不符合 GraphQL 的精神。
我想到的另一个想法是重写查询,使其首先检索 ParkingSlots,并且仅在其状态可用时才获取其父级。然而,这可能是计算密集型的,因为停车位比停车位少,因此导航从父级到子级的关系的计算密集度可能较低。
你会建议什么方法不归结为改变类型层次结构(我没有能力做到这一点)。是否有一种方法可以在子节点的解析器中发生某些条件时立即拒绝父节点,或者这是否会被视为超出 GraphQL 实现的范围,而更像是正在使用的实际库的功能?我想因为有些库支持使用“where:”子句进行过滤,这一定是可能的,而且不应该很复杂。
我想测试 GraphQL 端点和 RESTful 端点的响应时间,因为我以前从未使用过 GraphQL,我将在我的下一个 Laravel 项目中使用它。
所以我使用Lighthouse PHP 包从我的 Laravel 应用程序中为 GraphQL 端点提供服务,并且我还创建了一个 RESTful 端点。
两个端点(GraphQL 和 RESTful)都旨在从我的本地数据库中获取所有用户(250 个用户)。
因此,基于我在这里注意到的测试,当我在 上测试这两个端点时Postman,RESTful 端点响应比 GraphQL 端点快。
我可以知道为什么 GraphQL 端点的响应比 RESTful 花费更多时间,而两个端点都获得相同的数据吗?
GET 请求的 GraphQL 端点结果(响应时间:88 毫秒)

目前我有这样的 Types.php:
namespace Application\GraphQL;
use Application\GraphQL\Type\NodeType;
use Application\GraphQL\Type\QueryType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use Application\GraphQL\Type\PersonType;
/**
* Class Types
*
* Acts as a registry and factory for your types.
*
* As simplistic as possible for the sake of clarity of this example.
* Your own may be more dynamic (or even code-generated).
*
* @package GraphQL\Examples\Blog
*/
class Types
{
private static $query;
private static $person;
private static $node;
public static function person()
{
return self::$person ?: (self::$person = …Run Code Online (Sandbox Code Playgroud) 我对 GraphQL 完全陌生,想尝试一下 graphql-php 来构建一个简单的 API 来开始。我目前正在阅读文档并尝试示例,但我一开始就陷入困境。
我希望将我的架构存储在schema.graphql文件中,而不是手动构建它,因此我按照文档了解如何执行此操作,并且它确实有效:
<?php
// graph-ql is installed via composer
require('../vendor/autoload.php');
use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\AST;
use GraphQL\GraphQL;
try {
$cacheFilename = 'cached_schema.php';
// caching, as recommended in the docs, is disabled for testing
// if (!file_exists($cacheFilename)) {
$document = Parser::parse(file_get_contents('./schema.graphql'));
file_put_contents($cacheFilename, "<?php\nreturn " . var_export(AST::toArray($document), true) . ';');
/*} else {
$document = AST::fromArray(require $cacheFilename); // fromArray() is a lazy operation as well
}*/
$typeConfigDecorator = function($typeConfig, $typeDefinitionNode) {
// In …Run Code Online (Sandbox Code Playgroud) 我一直在研究GraphQL作为我的一些REST API的替代品,虽然我认为我已经完成了基本的工作,并且像我目前看到的大部分内容一样,但是有一个重要特性似乎缺失了.
假设我有一系列这样的项目:
{
"id": "aaa",
"name": "Item 1",
...
}
Run Code Online (Sandbox Code Playgroud)
应用程序需要所有这些对象的映射,由ID索引,如下所示:
{
"allItems": {
"aaa": {
"name": "Item 1",
...
},
"aab": {
"name": "Item 2",
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我写过的每个API都能够以这样的格式返回结果,但我很难找到一种方法来使用GraphQL.我一直在问题101中运行,但这更多地涉及未知模式.就我而言,我确切地知道所有领域是什么; 这纯粹是关于输出格式.我知道我可以简单地返回数组中的所有项目并在客户端重新格式化,但这看起来有点过分,因为它过去从未被需要,并且会让GraphQL感觉像是倒退了一步.我不确定我想要做的事情是不可能的,或者我只是使用了所有错误的术语.我应该继续挖掘,还是GraphQL不适合我的需求?如果这是可能的,查询可能会像这样检索数据?
我目前正在服务器上使用graphql-php,但我对更高级别的概念响应持开放态度.
我已经从to更新了Laravel,现在我的应用程序无法运行。v5.7v5.8
我因为这个问题更新了它:composer require rebing/graphql-laravel failed
我解决了包不兼容问题,但现在 Laravel 崩溃了:
$ php artisan serve
In Router.php line 366:
Argument 1 passed to Illuminate\Routing\Router::group() must be of the type array, string given, called in /var/www/masvino/Server-bak/vendor/laravel/framework/src/Illuminate/Support/Facades
/Facade.php on line 239
Run Code Online (Sandbox Code Playgroud)
我怀疑 Laravel 的团队改变了这种方法的坚定性:
照亮\路由\Router.php
/**
* Create a route group with shared attributes.
*
* @param array $attributes
* @param \Closure|string $routes
* @return void
*/
public function group(array $attributes, $routes) // line 366
{
$this->updateGroupStack($attributes);
// Once we …Run Code Online (Sandbox Code Playgroud) 我试图通过那里的例子来获取人对象:https : //github.com/webonyx/graphql-php/tree/master/examples/01-blog
我应该如何调试呢?
IndexController.php
public function indexAction()
{
if (!empty($_GET['debug'])) {
// Enable additional validation of type configs
// (disabled by default because it is costly)
Config::enableValidation();
// Catch custom errors (to report them in query results if debugging is enabled)
$phpErrors = [];
set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
$phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
});
}
try {
/* // Initialize our fake data source
DataSource::init();*/
// Prepare context that will be available …Run Code Online (Sandbox Code Playgroud)