Skip to content

Union response type not working #849

@WouterCypers

Description

@WouterCypers
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Version/Branch 1.0.0

I have a mutation ResetPassword which should return some date OR an error response. The response is defined as a Union. I get an error when executing the mutation:

Abstract type ResetPasswordResponse must resolve to an Object type at runtime for field publicMutation.ResetPassword with value "instance of App\GraphQL\PublicApi\Mutation\ResetPassword\ResetPasswordMutationFailedResponse", received "null". Either the ResetPasswordResponse type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.

In the documentation, I only see resolveType or isTypeOf being used for interfaces. I'm not sure how to make this work.

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]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions