Repository navigation
Union response type not working #849
Copy link
Copy link
Closed
Description
Activity
@WouterCypers you should use
uniontypes exactly like interfaces. You can check the documentation for interfaces for more details.In your case you should define for your abstract type (
ResetPasswordResponse) a type resolver (resolveType):ResetPasswordResponse: type: union config: types: [ResetPasswordSuccessfulResponse, ResetPasswordFailedResponse] description: 'Reset password succeeded or failed.' resolveType: "@=query('map_response_type', value, typeResolver)"
And modify your
ResetPasswordMutationclass:use GraphQL\Type\Definition\ObjectType; use Overblog\GraphQLBundle\Resolver\TypeResolver; use Overblog\GraphQLBundle\Resolver\UnresolvableException; class ResetPasswordMutation implements MutationInterface, AliasedInterface { public function __construct( private MessageBusInterface $messageBus, ) {} public function resetPassword( string $token, string $newPassword, string $repeatNewPassword ): ResetPasswordMutationResponse { try { $this->messageBus->dispatch( new ResetPassword( $token, $newPassword, $repeatNewPassword ) ); return new ResetPasswordMutationSuccessfulResponse($token, $newPassword); } catch (ValidationFailedException $exception) { $violations = []; /** @var ConstraintViolationInterface $violation */ foreach ($exception->getViolations() as $violation) { $violation->getMessage(); } return new ResetPasswordMutationFailedResponse($violations); } } public function mapResponseType($value, TypeResolver $typeResolver): ObjectType { if ($value instanceof ResetPasswordMutationSuccessfulResponse) { return $typeResolver->resolve('ResetPasswordSuccessfulResponse'); } if ($value instanceof ResetPasswordMutationFailedResponse) { return $typeResolver->resolve('ResetPasswordFailedResponse'); } throw new UnresolvableException("Couldn't resolve type for union 'ResetPasswordResponse'"); } public static function getAliases(): array { return [ 'resetPassword' => 'reset_password', 'mapResponseType' => 'map_response_type' ]; } }
- locked and limited conversation to collaborators
on May 14, 2021
Metadata
Metadata
Assignees
Labels
No labels
I have a mutation
ResetPasswordwhich should return some date OR an error response. The response is defined as a Union. I get an error when executing the mutation:In the documentation, I only see
resolveTypeorisTypeOfbeing used for interfaces. I'm not sure how to make this work.The GraphQL request:
The mutation:
The resolver:
YAML types config: