Hi, when i want to use eager loading in queries it returns Column Not Found error. I think this is because $with variable in resolve function is empty.
this is query resolve function:
public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
{
/** @var SelectFields $fields */
$fields = $getSelectFields();
$select = $fields->getSelect();
$with = $fields->getRelations();
$args['page'] = $args['page'] ?? 1;
$args['limit'] = $args['limit'] ?? 10;
$where = function ($query) use($args){
if(isset($args['id']))
$query = $query->where('id',$args['id']);
};
$articles = Article::with($with)
->select($select)
->where($where)
->paginate($args['limit'], ['*'], 'page', $args['page']);
return $articles;
}
this is article type fields:
public function fields(): array
{
return [
'id' => [
'type' => Type::int()
],
'title' => [
'type' => Type::string()
],
'body' => [
'type' => Type::string()
],
'user' => [
'type' => GraphQL::type('User')
],
'comments' => [
'type' => Type::listOf(GraphQL::type('Comment'))
]
];
}
and this is user type fields:
public function fields(): array
{
return [
'id' => [
'type' => Type::int()
],
'name' => [
'type' => Type::string()
],
'email' => [
'type' => Type::string()
],
'articles' => [
'type' => Type::listOf(GraphQL::type('Article'))
]
];
}
and my query and error is this:

Your error says that the column isn't in the database.
Maybe the field is named differently in the database and you want to use 'alias' as outlined in the first example at https://github.com/rebing/graphql-laravel#creating-a-query ?
in my users table there is name field. problem is that name field is in $select so it is selecting from articles table and $with variable is empty in reslove function.
$select = $fields->getSelect(); //['title','body','name']
$with = $fields->getRelations(); //[]
That's strange and should work.
Like, it seems you're using the resolver from the user to fetch the articles; that how this error looks to me.
Is public function resolve… you pasted on the GraphQL ArticleType?
yes. resolve method is for ArticleType
my ArticleQuery file:
<?php
declare(strict_types=1);
namespace App\GraphQL\Queries;
use App\Article;
use Closure;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\SelectFields;
use Rebing\GraphQL\Support\Query;
class ArticleQuery extends Query
{
protected $attributes = [
'name' => 'article',
'description' => 'A query'
];
public function type(): Type
{
return Type::listOf(GraphQL::type('Article'));
}
public function args(): array
{
return [
'id' => [
'type' => Type::int()
],
'page' => [
'type' => Type::int()
],
'limit' => [
'type' => Type::int()
]
];
}
public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
{
/** @var SelectFields $fields */
$fields = $getSelectFields();
$select = $fields->getSelect();
$with = $fields->getRelations();
$args['page'] = $args['page'] ?? 1;
$args['limit'] = $args['limit'] ?? 10;
$where = function ($query) use($args){
if(isset($args['id']))
$query = $query->where('id',$args['id']);
};
$articles = Article::with($with)
->select(['title','body','user_id'])
->where($where)
->paginate($args['limit'], ['*'], 'page', $args['page']);
return $articles;
}
}
Remove ->select(['title','body','user_id'])
And it should work or use
->select($fields)
->select(['title','body','user_id']) was for test the query results. original function is below as i sent first.
public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
{
/** @var SelectFields $fields */
$fields = $getSelectFields();
$select = $fields->getSelect();
$with = $fields->getRelations();
$args['page'] = $args['page'] ?? 1;
$args['limit'] = $args['limit'] ?? 10;
$where = function ($query) use($args){
if(isset($args['id']))
$query = $query->where('id',$args['id']);
};
$articles = Article::with($with)
->select($select)
->where($where)
->paginate($args['limit'], ['*'], 'page', $args['page']);
return $articles;
}
I re-read everything here but can't see what would cause this.
Nested relations supposedly are working, there are also tests for it, see e.g. \Rebing\GraphQL\Tests\Database\SelectFields\NestedRelationLoadingTests\NestedRelationLoadingTest
Please try to debug it further, maybe the insights for the tests work.
Otherwise the only suggestion I can give is:
@Nikoomarch Please try to look at my app. I use almost the same approach Hope it will be usefull
https://github.com/websitevirtuoso/graphql_demo/tree/feature/add_property_to_grapqhl/app/GraphQL
@mfn Hi, I removed the project and make a fresh laravel installation as you said. but unfortunately problem is steal remaining and $with variable is empty in resolve function. project is now on github: https://github.com/Nikoomarch/graphql-example/
@websitevirtuoso thanks! I saw it, but i think problem is something else. please check if $with variable is empty or not when you load relations in a query.
You didn't specify the 'model' attribute on the types. Without it, SelectFields can't know what to introspect to automagically generate $select and $with.
Here's the diff to your project to make it work:
diff --git a/app/GraphQL/Types/ArticleType.php b/app/GraphQL/Types/ArticleType.php
index c10d868..2a58d55 100644
--- a/app/GraphQL/Types/ArticleType.php
+++ b/app/GraphQL/Types/ArticleType.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\GraphQL\Types;
+use App\Article;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type as GraphQLType;
@@ -12,7 +13,8 @@ class ArticleType extends GraphQLType
{
protected $attributes = [
'name' => 'Article',
- 'description' => 'A type'
+ 'description' => 'A type',
+ 'model' => Article::class,
];
public function fields(): array
diff --git a/app/GraphQL/Types/CommentType.php b/app/GraphQL/Types/CommentType.php
index ff8e8e3..0e4e439 100644
--- a/app/GraphQL/Types/CommentType.php
+++ b/app/GraphQL/Types/CommentType.php
@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace App\GraphQL\Types;
+use App\Comment;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type as GraphQLType;
@@ -12,7 +14,8 @@ class CommentType extends GraphQLType
{
protected $attributes = [
'name' => 'Comment',
- 'description' => 'A type'
+ 'description' => 'A type',
+ 'model' => Comment::class,
];
public function fields(): array
diff --git a/app/GraphQL/Types/UserType.php b/app/GraphQL/Types/UserType.php
index fe7abdb..cfa9e76 100644
--- a/app/GraphQL/Types/UserType.php
+++ b/app/GraphQL/Types/UserType.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\GraphQL\Types;
+use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type as GraphQLType;
@@ -12,7 +13,8 @@ class UserType extends GraphQLType
{
protected $attributes = [
'name' => 'User',
- 'description' => 'A type'
+ 'description' => 'A type',
+ 'model' => User::class,
];
public function fields(): array
It's mentioned in the readme right next to creating a query: https://github.com/rebing/graphql-laravel#creating-a-query