ferry icon indicating copy to clipboard operation
ferry copied to clipboard

Ferry cannot decompose fragment on TypeCondition

Open Masadow opened this issue 2 months ago • 1 comments

Assume the following schema :

  interface GenericValue {
    name: String
  }

  type TextValue implements GenericValue {
    name: String
    text: String
  }

  type NumberValue implements GenericValue {
    name: String
    number: Int
  }

  type Test {
    value: GenericValue
  }

  type CrashTest {
    a: Test
    b: Test
  }

  type Query {
    fixme: CrashTest
  }

And now, in your app, you query it like

fragment Text on TextValue {
  name
  text
}

fragment Number on NumberValue {
  name
  number
}

fragment CrashTestFragment on Test {
  value {
    ...Text
    ...Number
  }
}

query Test {
  fixme {
    a {
      ...CrashTestFragment
    }
    b {
      ...CrashTestFragment
    }
  }
}

Building the following will result in

[SEVERE] ferry_generator:graphql_builder on lib/src/views/questionnaires/questionnaires.graphql:

Bad state: No element
dart:collection                                          ListBase.firstWhere
package:gql_code_builder/src/operation/data.dart 457:8   _getFieldTypeNode
package:gql_code_builder/src/operation/data.dart 153:24  buildSelectionSetDataClasses.<fn>
dart:core                                                Iterable.toList
package:gql_code_builder/src/operation/data.dart 169:5   buildSelectionSetDataClasses
package:gql_code_builder/src/operation/data.dart 235:22  buildSelectionSetDataClasses.<fn>
dart:core                                                List.addAll
package:gql_code_builder/src/operation/data.dart 234:10  buildSelectionSetDataClasses
package:gql_code_builder/src/operation/data.dart 235:22  buildSelectionSetDataClasses.<fn>
dart:core                                                List.addAll
package:gql_code_builder/src/operation/data.dart 234:10  buildSelectionSetDataClasses
package:gql_code_builder/src/operation/data.dart 235:22  buildSelectionSetDataClasses.<fn>
dart:core                                                List.addAll
package:gql_code_builder/src/operation/data.dart 234:10  buildSelectionSetDataClasses
package:gql_code_builder/src/operation/data.dart 24:10   buildOperationDataClasses
package:gql_code_builder/data.dart 36:17                 buildDataLibrary.<fn>
dart:core                                                Iterable.toList
package:gql_code_builder/data.dart 46:8                  buildDataLibrary
package:ferry_generator/graphql_builder.dart 104:22      GraphqlBuilder.build

Until this is resolved, a workaround is to wrap your fragment with the type condition :

fragment CrashTestFragment on Test {
  value {
    ... on TextValue { ...Text }
    ... on NumberValue { ...Number }
  }
}

In case it's useful, source of graphql manifest : https://spec.graphql.org/October2021/#sec-Type-Conditions

Masadow avatar Apr 10 '24 09:04 Masadow