| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | no
| BC Break report? | no
| RFC? | no
| Version/Branch | 0.11.0
I am trying to update from the 0.10 to 0.11 way of doing things but the documentation for ResolverMap is really confusing for me. If somebody could answer these questions, I would greatly appreciate it!
How do I use the Service Container / DI? Previously, I had an expression @=resolver in my config and all dependencies would be properly loaded. In my 0.11 setup, I am passing my resolvers to the ResolverMap constructor. Although it works, it ends up instantiating all of the resolvers instead of lazy-loading them if the query asks for them. Am I doing this correctly or is there a better way?
With the new ResolverMap, does this require that I change the method signature of my resolvers? Previously it was simply ($args) but now it seems to be ($value, $args, $context, $info).
# config/graphql/types/Query.yaml
Query:
type: object
config:
fields:
fruits:
type: "[String]!"
args:
color:
type: String
shape:
type: String
resolve: '@=resolver("App\\GraphQL\\Resolver\\Fruits", [args])'
```php
// src/GraphQL/Resolver/Fruits.php
use Overblog\GraphQLBundleDefinition\Argument;
use Overblog\GraphQLBundleDefinition\Resolver\ResolverInterface;
class Fruits implements ResolverInterface
{
protected $repo;
public function __construct(FruitRepository $repo)
{
$this->repo = $repo;
}
public function __invoke(Argument $args)
{
return $this->repo->findBy($args);
}
}
### 0.11 setup:
```graphql
# config/graphql/types/Query.graphql
type Query {
fruits(
color: String
shape: String
): [String]!
}
```php
// src/GraphQL/ResolverMap.php
namespace App\GraphQL;
use App\GraphQL\Resolver\Fruits;
use Overblog\GraphQLBundle\Resolver\ResolverMap as BaseResolverMap;
class ResolverMap extends BaseResolverMap
{
private $fruits;
public function __construct(Fruits $fruits)
{
$this->fruits = $fruits;
}
protected function map()
{
return [
"Query" => [
"fruits" => $this->fruits,
],
];
}
}
```php
// src/GraphQL/Resolver/Fruits.php
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
class Fruits implements ResolverInterface
{
private $repo;
public function __construct(FruitRepository $repo)
{
$this->repo = $repo;
}
public function __invoke($value, Argument $args)
{
return $this->repo->findBy($args);
}
}
Hi if you want to migrate from expression language resolver to ResolverMap, you should use the resolver service to keep lazy loading.
Here how this can be done:
namespace App\GraphQL;
use App\GraphQL\Resolver\Fruits;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Resolver\ResolverMap as BaseResolverMap;
use Overblog\GraphQLBundle\Resolver\ResolverResolver;
class ResolverMap extends BaseResolverMap
{
private $resolver;
public function __construct(ResolverResolver $resolver)
{
$this->resolver = $resolver;
}
protected function map()
{
return [
"Query" => [
"fruits" => function ($value, Argument $args, \ArrayObject $context, ResolveInfo $info) {
return $this->resolver->resolve(Fruits::class, [$args]);
},
],
];
}
}
The signature of resolver (with expression language) was custom because you could inject variables in the order you wanted but with ResolverMap you must follow the given signature. Please see this page for more details. Note you can also inject use Overblog\GraphQLBundle\Resolver\MutationResolver when dealing with mutations .
Thank you, @mcg-web! ResolverResolver was the missing piece of the puzzle. I did have to wrap the arguments in an array to make it work however: ...->resolve([Fruits::class, [$args]]);.
I have read the Resolver Map docs many times (and all other pages of the docs) but it does not mention ResolverResolver anywhere. I make notes of where I have difficulty understanding documentation and try to submit PRs to make them more clear if I am able to and once I have a better understanding how things work.
Yes I agree documentation need some improvements. PR is always welcome. I'm closing this right now feel free to reopen if needed :+1:
Most helpful comment
Hi if you want to migrate from expression language resolver to ResolverMap, you should use the resolver service to keep lazy loading.
Here how this can be done:
The signature of resolver (with expression language) was custom because you could inject variables in the order you wanted but with ResolverMap you must follow the given signature. Please see this page for more details. Note you can also inject use
Overblog\GraphQLBundle\Resolver\MutationResolverwhen dealing with mutations .