I followed the docs on creating a query, but I cannot get it working. I keep getting the following error:
{
"errors": [
{
"message": "Cannot query field \"users\" on type \"Query\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}
The query:
query FetchUser{
users{
id
email
}
}
When i googled this issue it came up multiple times, but most of the time the poster didn't comment anymore or they simply said it was a configuration issue. Does anyone know how to fix this?
'schemas' => [
'default' => [
'query' => [
'users' => App\GraphQL\Queries\UsersQuery::Class
],
'mutation' => [
// 'example_mutation' => ExampleMutation::class,
],
'middleware' => [],
'method' => ['get', 'post'],
],
],
'types' => [
'user' => App\GraphQL\Types\UserType::class
],
<?php
namespace App\GraphQL\Types;
use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;
class UserType extends GraphQLType
{
protected $attributes = [
'name' => 'User',
'description' => 'A user',
'model' => User::class,
];
public function fields() : array
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The id of the user'
],
'email' => [
'type' => Type::string(),
'description' => 'The email of the user',
],
];
}
}
<?php
namespace App\GraphQL\Queries;
use Closure;
use App\User;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;
class UsersQuery extends Query
{
protected $attributes = [
'name' => 'Users query'
];
public function type() : Type
{
return Type::listOf(GraphQL::type('user'));
}
public function args() : array
{
return [
// No aruments
];
}
public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectedFields)
{
return Users::all();
}
}
protected $attributes = [ 'name' => 'Users query' ];
_That_ $name is supposed to be users and likely should not contain a space. Try that please and see if it works.
"Cannot query field \"users\" on type \"Query\"."
I think this gives it away: Query is the GraphQL root object type for queries and it says it can't find users, ergo the query, although registered, has the wrong name.
Nope, sadly that doesn't fix it...
Everything I see here is supposed to work (unless I missed some mini detail, but by and large it's correct).
Please post a example repo with the full app which reproduces the issue; preferable with master/v2 (see released) and not v1.
I found the issue. It was because of an caching problem. I needed to run php artisan config:cache to clear the config cache, so it would be updated correctly.
Perhaps the README could point out the necessity of running php artisan config:cache?
I'm not overly fond of this idea, because the Laravel docs cover this pretty well:
=> https://laravel.com/docs/6.x/deployment
I don't want to sound, uhm鈥ocky?, but I consider people understanding how Laravel itself works the bare minimum. Also if that procedure on Laravels behalf might change in the future, we would have to think about this etc.
All in all, not something I consider should be part of a "Laravel library", especially the library itself isn't doing anything out of the ordinary in that regard.
Alright, fair enough. I simply landed in a Laravel project and went straight to setting up GraphQL without bothering much with Laravel first. so my bad I guess.
Thanks for your understanding 馃槃
Most helpful comment
I found the issue. It was because of an caching problem. I needed to run
php artisan config:cacheto clear the config cache, so it would be updated correctly.