laravel-graphql icon indicating copy to clipboard operation
laravel-graphql copied to clipboard

How to do my custom Resolver of a field?

Open yosoyjp opened this issue 6 years ago • 1 comments

I have a list of products associated to each category, I have defined categories as a Type other than Product. The resolver that believes if it runs but does not send me the data.

captura de pantalla de 2018-08-28 07-54-59 captura de pantalla de 2018-08-28 07-58-18

the function resolveCategoryField not is send my data. HelpMe Please.

yosoyjp avatar Aug 28 '18 12:08 yosoyjp

You should rename 'category' field to 'categories' :

...
'categories'=>[
     'type' => Type::listOf(GraphQL::type('Category'))
],

Or write this code in the resolver function:

protected function resolveCategoriesField($root,$args)
{
   // you may have some code here...

    return $root->categories; // this relationship must be defined in the product model
}

mohammad-fouladgar avatar Aug 29 '18 05:08 mohammad-fouladgar