MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
https://typegraphql.com
MIT License
7.99k stars 674 forks source link

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

Closed z0d14c closed 8 months ago

z0d14c commented 8 months ago

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):

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

z0d14c commented 8 months ago

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?

MichalLytek commented 8 months ago

Yes, the problem is with String type.

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