MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Home Page:https://typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested/2D arrays don't work with union types

z0d14c opened this issue · comments

Describe the Bug
If you use a union type within a nested array, you'll see an error like this:

<REPO_ROOT>/node_modules/type-graphql/build/cjs/schema/schema-generator.js:69
return unionObjectTypesInfo.map(it => it.type);
TypeError: Cannot read properties of undefined (reading 'type')

To Reproduce

  1. Create enum type
  2. Reference it inside of a 2-dimensional (nested) array inside of an ObjectType
export const SomeUnionType = createUnionType({
  name: 'SomeUnionType',
  types: () => [String, SomeType, SomeOtherType] as const,
  resolveType: (value) => {
    // type resolution logic here
  },
})

@ObjectType()
export class SomeOutput {
  // Project runs/works if I comment this out:
  @Field((type) => [[SomeUnionType]])
  rows: SomeUnionType[][]
}

Expected Behavior
I expect this to work/not crash the app.

Logs
If applicable, add some console logs to help explain your problem.
You can paste the errors with stack trace that were printed when the error occurred.

Environment (please complete the following information):

  • OS: MacOS
  • Node 16.20.2
  • Type-graphql 2.0.0-beta.3
  • TypeScript version 4.x

Additional Context
This makes making typed grid/table content difficult.

Oops, I think I messed up here and the problem is the use of String in a union type, and not the union type otherwise. Maybe have a better error message here?

Yes, the problem is with String type.

Can you add your suggestion about better error message in #12? 🙏