heftapp / graphql_codegen

MIT License
143 stars 53 forks source link

" Failed to generate type for <Union>" when an input has a union field #355

Closed rrousselGit closed 1 month ago

rrousselGit commented 1 month ago

Hello!

Consider:

type Foo {
  a: String!
}

type Bar {
  b: String!
}

union Union = Foo | Bar

input Input {
  input: Union!
}

extend type Mutation {
  example(input: Input!): String!
}

This fails with:

[SEVERE] graphql_codegen on lib/src/graphql/deployment.graphql:

Bad state: Failed to generate type for Union.
package:graphql_codegen/src/printer/base/property.dart 119:5  _printNamedTypeNode
package:graphql_codegen/src/printer/base/property.dart 52:12  _printTypeNode
package:graphql_codegen/src/printer/base/property.dart 39:10  printClassPropertyType
package:graphql_codegen/src/printer/base/input.dart 36:29     _printInputClasses.<fn>.<fn>
package:code_builder/src/specs/method.g.dart 649:33           _$ParameterBuilder.update
package:code_builder/src/specs/method.g.dart 398:32           new _$Parameter
package:graphql_codegen/src/printer/base/input.dart 34:21     _printInputClasses.<fn>
dart:core                                                     new List.from
package:built_collection/src/list/list_builder.dart 44:20     ListBuilder.replace
package:built_collection/src/list/list_builder.dart 20:45     new ListBuilder
package:graphql_codegen/src/printer/base/input.dart 32:29     _printInputClasses
package:graphql_codegen/src/printer/base/input.dart 15:5      printInputClasses
package:graphql_codegen/src/printer/printer.dart 23:20        printRootContext.<fn>
dart:core                                                     new List.of
package:graphql_codegen/src/printer/printer.dart 21:40        printRootContext
package:graphql_codegen/graphql_codegen.dart 27:10            _generateDocument
package:graphql_codegen/graphql_codegen.dart 118:10           generate
package:graphql_codegen/builder.dart 104:20                   GraphQLBuilder.build

It appears that _printNamedTypeNode does not handle Union types and throws because of it.

github-actions[bot] commented 1 month ago

👋 @rrousselGit Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible. Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

rrousselGit commented 1 month ago

Looks like the schema was invalid. Closing :)