Lighthouse: Custom resolver args just takes the first value

Created on 2 Sep 2019  路  4Comments  路  Source: nuwave/lighthouse

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

question

Most helpful comment

<?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?

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

souljacker picture souljacker  路  3Comments

wimski picture wimski  路  3Comments

m1guelpf picture m1guelpf  路  3Comments

a-ssassi-n picture a-ssassi-n  路  3Comments

alexwhb picture alexwhb  路  4Comments