Я использую библиотеку laravel-graphql от Folklore. Я следовал всем инструкциям к T, но когда я тестирую его в Altair Client для 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();
}
}
}
CONFIG / 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']
];
-
-
UPDATE
Поэтому я установил библиотеку 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,
Что-то подсказывает мне, что я не должен видеть такие пути. Если это не так, как мне исправить?