I want to use this pattern for returning validation failures from the GraphQL Ruby: https://medium.com/@sachee/200-ok-error-handling-in-graphql-7ec869aec9bc
From my mutation I'd like to be able to return a payload that is a union like this:
class RegistrationPayloadType < Base::Union
possible_types UserType, ValidationFailureType
def self.resolve_type(object, context)
if context.current_user.present?
UserType
else
ValidationFailureType
end
end
end
And my resolve method in the mutation is something like this;
def resolve(input:)
@input = input.to_h
if registration.save
candidate
else
registration.errors
end
end
The client can then call the mutation thus;
mutation UserRegistrationMutation($input: UserRegistrationInput!) {
userRegistration(input: $input) {
__typename
... on User {
id
}
... on ValidationFailure {
path
message
}
}
}
How in GraphQL-ruby can I return a Union as a payload?