3
votes

Trying to create a union type in type-graphql representing the String, Number and Boolean classes, however it failed. Any idea how to do it ?

export const NonObjectType = createUnionType({
    name: "NonObjectType",
    types: () => [String, Boolean, Number] as const,
    resolveType: (value) => {
        console.log(value, typeof value);
        switch (typeof value) {
            case "string":
                return String;
            case "number":
                return Number;
            case "boolean":
                return Boolean;
            default:
                return undefined;
        }
    },
});

@ArgsType()
@InputType("FindSomeArgsUnitInputType")
export class GqlArgsFindSomeArgsUnit<T extends object>
    implements IFindSomeArgsUnit<T> {
    @Field(() => String, { defaultValue: "id" })
    column: keyof T;

    @Field(() => [NonObjectType], { defaultValue: [""] })
    values: Array<string | boolean | number>;
}

Error : Error: Cannot determine GraphQL input type for 'values' of 'GqlArgsFindSomeArgsUnit' class

1

1 Answers

2
votes

Unfortunatelly, GraphQL spec does not support union on scalar types, only on object types:

https://github.com/graphql/graphql-spec/issues/215