I've got a repro here:
Basically, the modifyFoo payload is supposed to only accept FooFailure as a type, however, it accepts BarFailure because the signature is the same. Even the error part of it, the signature is the same.
This makes for runtime errors that are pretty difficult to see in review.
Obviously we can add a random property (like fooFailure = true) to FooFailure, and then it works as desired because the signature is different, but requiring a unique structure will also be hard to catch in review.
Anyone know of a way to work around this?
import { Field, Mutation, ObjectType, Resolver, createUnionType } from '@nestjs/graphql';
@ObjectType()
class FirstError { title = "foo"; message = "bar"; }
@ObjectType()
class OtherError { title = "bar"; message = "foo"; }
const FooError = createUnionType({
name: 'FooError',
types: () => [FirstError],
});
const BarError = createUnionType({
name: 'BarError',
types: () => [OtherError],
});
@ObjectType()
class FooFailure {
@Field(() => FooError)
error: typeof FooError;
constructor(error: typeof FooError) {
this.error = error;
}
}
@ObjectType()
class BarFailure {
@Field(() => BarError)
error: typeof BarError;
constructor(error: typeof BarError) {
this.error = error;
}
}
export const FooPayload = createUnionType({
name: 'FooPayload',
types: () => [FooFailure],
})
export const BarPayload = createUnionType({
name: 'BarPayload',
types: () => [BarFailure],
});
@Resolver()
export class FooBarResolver {
@Mutation(() => FooPayload)
modifyFoo(): typeof FooPayload {
return new BarFailure(new OtherError())
}
}