php - GraphQL 查询未从 Laravel 返回任何内容
问题描述
我正在使用来自 Folklore 的 laravel-graphql 库。我已按照 T 的所有说明进行操作,但是当我在 Altair Client for Windows 中对其进行测试时,我收到“未知服务器错误,代码 0”。我的 REST API 可以正常工作,因此要么是我在设置 GraphQL 层时出错,要么是 Folklore 没有更新他们的库,最终落后于我正在使用的 Laravel 版本。现在是 5.7。
POSTTYPE.PHP
<?php
namespace App\GraphQL\Type;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as GraphQLType;
/* Here we're giving the type a name and description */
class PostType extends GraphQLType {
protected $attributes = [
'name' => 'Post', //here we're defining the query by giving it a name of Post
'description' => 'One post'
];
public function fields(){
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'Primary key; incrementing id of post'
],
'header' => [
'type' => Type::string(),
'description' => 'The header of the post'
],
'body' => [
'type' => Type::string(),
'description' => 'The body of the blog post'
],
'created_at' => [
'type' => Type::string(),
'description' => 'When the post was created'
],
'updated_at' => [
'type' => Type::string(),
'description' => 'When the post was last updated'
],
'img' => [
'type' => Type::string(),
'description' => 'Where the main image of the blog post is stored'
],
];
}
// If you want to resolve the field yourself, you can declare a method
// with the following format resolve[FIELD_NAME]Field()
//protected function resolveEmailField($root, $args)
//{
// return strtolower($root->email);
//}
}
POSTQUERY.PHP
<?php
namespace App\GraphQL\Query;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;
use App\Post;
class PostsQuery extends Query {
//give the query a name of 'posts'
protected $attributes = [
'name' => 'posts'
];
//define the query type
public function type(){
return Type::listOf(GraphQL::type('Post'));
}
//define things to fetch and turn them into arguments
public function args(){
return [
'id' => ['name' => 'id', 'type' => Type::int()],
'header' => ['name' => 'header', 'type' => Type::string()],
'body' => ['name' => 'body', 'type' => Type::string()],
'img' => ['name' => 'img', 'type' => Type::string()],
];
}
//fetch all the posts
public function resolve($root, $args){
if (isset($args['id'])){
return Post::where('id', $args['id'])->get();
}
else if (isset($args['header'])){
return Post::where('header', $args['header'])->get();
}
else if (isset($args['body'])){
return Post::where('body', $args['body'])->get();
}
else if (isset($args['img'])){
return Post::where('img', $args['img'])->get();
}
else {
return Post::all();
}
}
}
配置/GRAPHQL.PHP
<?php
return [
// The prefix for routes
'prefix' => 'graphql',
// The routes to make GraphQL request. Either a string that will apply
// to both query and mutation or an array containing the key 'query' and/or
// 'mutation' with the according Route
//
// Example:
//
// Same route for both query and mutation
//
// 'routes' => 'path/to/query/{graphql_schema?}',
//
// or define each routes
//
// 'routes' => [
// 'query' => 'query/{graphql_schema?}',
// 'mutation' => 'mutation/{graphql_schema?}'
// ]
//
// you can also disable routes by setting routes to null
//
// 'routes' => null,
//
'routes' => '{graphql_schema?}',
// The controller to use in GraphQL request. Either a string that will apply
// to both query and mutation or an array containing the key 'query' and/or
// 'mutation' with the according Controller and method
//
// Example:
//
// 'controllers' => [
// 'query' => '\Folklore\GraphQL\GraphQLController@query',
// 'mutation' => '\Folklore\GraphQL\GraphQLController@mutation'
// ]
//
'controllers' => '\Folklore\GraphQL\GraphQLController@query',
// Any middleware for the graphql route group
'middleware' => [],
// The name of the default schema used when no argument is provided
// to GraphQL::schema() or when the route is used without the graphql_schema
// parameter.
'schema' => 'default',
// The schemas for query and/or mutation. It expects an array to provide
// both the 'query' fields and the 'mutation' fields. You can also
// provide directly an object GraphQL\Schema
//
// Example:
//
// 'schemas' => [
// 'default' => new Schema($config)
// ]
//
// or
//
// 'schemas' => [
// 'default' => [
// 'query' => [
// 'users' => 'App\GraphQL\Query\UsersQuery'
// ],
// 'mutation' => [
//
// ]
// ]
// ]
//
'schemas' => [
'default' => [
'query' => [
'posts' => 'App\GraphQL\Query\PostsQuery'
],
'mutation' => [
'newPost' => 'App\GraphQL\Mutation\NewPostMutation',
//'updatePostStatus' => App\GraphQL\Mutation\UpdatePostStatusMutation::class,
]
]
],
// The types available in the application. You can then access it from the
// facade like this: GraphQL::type('user')
//
// Example:
//
// 'types' => [
// 'user' => 'App\GraphQL\Type\UserType'
// ]
//
// or whitout specifying a key (it will use the ->name property of your type)
//
'types' => [
'App\GraphQL\Type\PostType',
],
//
//'types' => [
// 'Post' => 'App\GraphQL\Type\PostType',
//],
// This callable will receive every Error object for each error GraphQL catches.
// The method should return an array representing the error.
//
// Typically:
// [
// 'message' => '',
// 'locations' => []
// ]
//
'error_formatter' => ['\Folklore\GraphQL\GraphQL', 'formatError']
];
-
-
更新 所以我安装了 Rebing 库并卸载了 Folklore 库,然后让 GraphiQL 工作。我在其中运行了一个查询并得到了
"message": "Class App\\GraphQL\\Type\\UserType does not exist",
"exception": "ReflectionException",
"file": "C:\\wamp64\\www\\laravel-project\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
"line": 779,
有些东西告诉我,我不应该看到这样的路径。如果这是错误的,我该如何解决?
解决方案
在您的 config/graphql 而不是'posts' => 'App\GraphQL\Query\PostsQuery'
使用'posts' => App\GraphQL\Query\PostsQuery::class
.
它有时取决于 PHP 的版本。
推荐阅读
- sql - postgresql index not used on range queries
- gdb - 有没有办法在遇到断点时执行一组 GDB 命令?
- reporting-services - In SSRS is there a way to use newspaper columns and still have a new page per group?
- java - Webview app turns off after Splash Screen Android Studio
- gnuplot - gnuplot:如何从绘制的最后一个点添加一条水平线
- python - 创建图像表面窗口的精灵表
- c# - 将 JSON 作为对象的一部分传递给 .net 核心 API
- java - 避免基本身份验证的不必要的 Keycloak 会话
- python - ValueError:检查目标时出错:预期dense_24具有形状(1,)但得到形状为(77,)的数组
- html - 在不使用 flask-bootstrap 的情况下使用 wtfroms