Union response type not working #850
-
I have a mutation
In the documentation, I only see The GraphQL request: mutation resetPassword {
ResetPassword (
input: {
token: "PasswordResetRequest-011eaf33-a93a-4f72-9509-e2b739741535"
newPassword: "MyPassword01!+qw121124"
repeatNewPassword: "MyPassword01!+qw12112"
}
) {
... on ResetPasswordSuccessfulResponse {
token
newPassword
}
... on ResetPasswordFailedResponse {
errors
}
}
} The mutation: ResetPassword:
type: ResetPasswordResponse
resolve: '@=mutation("reset_password", args["input"]["token"], args["input"]["newPassword"], args["input"]["repeatNewPassword"])'
args:
input:
type: ResetPasswordInput! The resolver: 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 static function getAliases(): array
{
return [
'resetPassword' => 'reset_password',
];
}
} YAML types config: ResetPasswordResponse:
type: union
config:
types: [ResetPasswordSuccessfulResponse, ResetPasswordFailedResponse]
description: 'Reset password succeeded or failed.'
ResetPasswordSuccessfulResponse:
type: object
config:
fields:
token:
type: 'String!'
newPassword:
type: 'String!' ResetPasswordFailedResponse:
type: object
config:
fields:
errors:
type: '[String]'
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@WouterCypers you should use In your case you should define for your abstract type ( ResetPasswordResponse:
type: union
config:
types: [ResetPasswordSuccessfulResponse, ResetPasswordFailedResponse]
description: 'Reset password succeeded or failed.'
resolveType: "@=mutation('map_response_type', value, typeResolver)" And modify your 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'
];
}
} |
Beta Was this translation helpful? Give feedback.
@WouterCypers you should use
union
types 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
):And modify your
ResetPasswordMutation
class: