dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.

Home Page:https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing fragment in Interface `Fragments` type

th3coop opened this issue · comments

When you have nested Interfaces in your schema, the "child" Interface is missing from the "parent" Interface.Fragments type.

Given an SDL:

type Query {
  characters: [Character!]!
}
interface Character{
  name: String!
}
interface Biological implements Character{
  name: String!
  homePlanet: String!
}
type Human implements Biological & Character {
  name: String!
  homePlanet: String!
  hasTonsels: Boolean!
}
type Android implements Character{
  name: String!
  designation: String!
}

The resulting Interface.Character.Fragments type will be as follows:

type alias Fragments decodesTo =
    { onHuman : SelectionSet decodesTo Api.Object.Human
    , onAndroid : SelectionSet decodesTo Api.Object.Android
    }

But it should be this:

type alias Fragments decodesTo =
    { onHuman : SelectionSet decodesTo Api.Object.Human
    , onBiological : SelectionSet decodesTo Api.Interface.Biological
    , onAndroid : SelectionSet decodesTo Api.Object.Android
    }

Some notes from the digging I did last night:

It looks like the issue is that when extracting the Interface data from the Introspection data, possibleTypes is used to see ultimately, what fragments it can have. Child interfaces are not included in that list:
https://github.com/dillonkearns/elm-graphql/blob/master/generator/src/Graphql/Generator/Group.elm#L46

To include the child Interface, it looks like you (the royal you (is that a thing?)) need to look at ALL the available Interfaces and see if their "interfaces": [], field is populated, and if it is, update any Interfaces listed with the current Interface name as a "possibleType" so it makes it into the Fragments type of the parent Interface.

Another option could be to not look at the possibleTypes field at all and only look at the interfaces field for every type then update the Interfaces possibleTypes. I could imagine a performance hit with this option though with really big and complex schemas though how much of one, I have not the slightest clue.