apollo-tooling: Duplicate fields are generated into Fragment interface
I’m using version 0.10.9
and ran into the problem that some properties are generated more than one time into a Fragment interface.
I’ve assembled a very trivial example for this. Schema: zoo.graphql
interface Feedable {
_id: ID!,
kind: String!
feedWith: String!
}
interface Reptile {
scaleSkin: String!
}
type Crocodile implements Feedable, Reptile {
_id: ID!,
kind: String!
feedWith: String!
scaleSkin: String!
victims: Int!
}
type Iguana implements Feedable, Reptile {
_id: ID!,
kind: String!
feedWith: String!
scaleSkin: String!
age: Int!
}
root.graphql
type Query {
getZoo: [Feedable!]!
}
schema {
query: Query
}
And now I setup a very simple query with some fragments: zooQuery.graphql
query getZoo {
getZoo {
...FeedableFragment
}
}
FeedableFragment
fragment FeedableFragment on Feedable {
_id
kind,
feedWith
... on Reptile {
scaleSkin
}
... on Crocodile {
victims
}
... on Iguana {
age
}
}
And now I check the generated interfaces and I have this:
export interface FeedableFragment {
_id: string;
kind: string;
feedWith: string;
_id: string;
kind: string;
feedWith: string;
scaleSkin: string;
victims: number;
_id: string;
kind: string;
feedWith: string;
scaleSkin: string;
age: number;
}
As you can see, the _id
, kind
, feedWith
fields are repeated three times, instead of one.
What’s more, I think that the scaleSkin
and age
properties should be optional properties.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 21 (6 by maintainers)
Yeah that’s right. I just landed a change to hopefully get
__typename
added to all the right places. Next step is making sure they get the actual type names as a ‘string literal’ as__typename
valuesFollowing up on this, I think the simplest thing to do is to allow users to specify
__typename
in the query itself. It’s recommended here: http://graphql.org/learn/queries/#meta-fields, and even though it’s not strictly necessary when a typename query transformer is used, it doesn’t feel right to assume a typename query transformer is used by default.Also, because Flow doesn’t have Typescript user-defined type guards, we’re going to need the
switch(__typename)
type guard anyway.We can change this in the future as a toggle-able option, but for now I want to go with the simplest and least surprising solution that’s applicable to both Flow and Typescript.