I'm using a custom resolve to query a many to many relationship, But everytime the array pass to the custom resolver this just returns the first result of the first value.
schema
complexFeeds(id: [Int] ): [Feeds] @field(resolver: "ComplexFeeds@resolve")
this is my query
query{
complexFeeds(id:[10,1,3]){
id
name
image
}
}
custom resolver
<?php
namespace App\GraphQL\Queries;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Feeds;
use App\Plazas;
class ComplexFeeds
{
public function resolve($rootValue,array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
$promo = $feeds->whereHas('plazas',function ($query) use ($args) {
$query->whereIn('plaza_id', $args);
}) ;
return $promo->get();
}
}
When i run the resolver's function in a controller everything works as expected and the results are correct, but with custom resolver it just takes the first argument of the array of ids.
I'm using laravel-lighthouse 4.1
Your code can't actually work. You are using $args in resolve method as is. $args is an associative array with values passed as input. For your schema
complexFeeds(id: [Int] ): [Feeds] @field(resolver: "ComplexFeeds@resolve")
$args will contain your ids array in $args['id'].
yes, but how an i achieve this?. I also read about it for mutations but not for queries.
<?php
namespace App\GraphQL\Queries;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Feeds;
use App\Plazas;
class ComplexFeeds
{
public function resolve($rootValue,array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
// Where is $feeds defined? May be use App\Feeds instead?
$promo = App\Feeds::whereHas('plazas',function ($query) use ($args) {
// simply use $args['id']
$query->whereIn('plaza_id', $args['id']);
});
return $promo->get();
}
}
Or did I missunderstand you?
<?php namespace App\GraphQL\Queries; use GraphQL\Type\Definition\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use App\Feeds; use App\Plazas; class ComplexFeeds { public function resolve($rootValue,array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { // Where is $feeds defined? May be use App\Feeds instead? $promo = App\Feeds::whereHas('plazas',function ($query) use ($args) { // simply use $args['id'] $query->whereIn('plaza_id', $args['id']); }); return $promo->get(); } }Or did I missunderstand you?
Yes you're right. I tried this before but now it's working. I'll close the issue. Thank you
Most helpful comment
Or did I missunderstand you?