micimize / graphql-to-dart

generate dart classes and respective JsonSerializable transcoders
42 stars 8 forks source link

Document using fragment causing issues when expanding fields that already exist in fragment #27

Open ryankauk opened 4 years ago

ryankauk commented 4 years ago

On the document that's using the fragment, the generated dart creates the get/set methods howeveer they throw an error. For example...

'SendMomentMessageCreateMomentMessage.readReceipts=' ('void Function(List)') isn't a valid override of 'MessageModel.readReceipts=' ('void Function(List)').

Maybe the selection set classes that are generated from fragments template should all be mixins that are implemented on the documents that use them. Example scenario: _fragment.gql

fragment MyFragment on Model {
    myField {
        id
    }
}

GetModel.gql

query GetModel {
    models {
        ...MyFragment
        myField {
            createdAt
        }
    }
}
micimize commented 4 years ago

hmm... fragments are merged/deduplicated in graphql, and it seems that graphql-code-generator doesn't handle that itself it seems.

fragment f on PlanetsConnection {
  planets {
    name
    orbitalPeriod
  }
}

{
  allPlanets {
    planets {
      name
      id
    }
    planets {
      name # Luckily an alias like `name: gravity` makes this query invalid
    }
    ...f
  }
}

yields

{
  "data": {
    "allPlanets": {
      "planets": [
        {
          "name": "Tatooine",
          "id": "cGxhbmV0czox",
          "orbitalPeriod": 304
        }, ...
micimize commented 4 years ago

fragment merging turns out to be a nightmare with the current design. I might try and accommodate this at some point, but for now you'll have to just make an intermediate fragment