Running into an issue with conflicting types.
I have a schema definition that follows this format:
type Match {
id: ID
name: String
start: Date
winner: Team
teams: [Team]
}
type Team {
id: ID
name: String
}
When trying to define it in Apollo on the iOS client, I get the build error 'Cannot convert value of type 'Match.Team.Type' to 'GraphQLFragment.Type'. My definitions are
# TeamFragment.graphql
fragment Team on Team {
id
name
}
# MatchFragment.graphql
fragment Match on Match {
id
name
start
winner {
... Team
}
teams {
... Team
}
}
If I remove the definition for the teams field, Team gets created as a GraphQL Fragment. If I add it, Team is defined as a GraphQLSelectionSet and throws a type error.
Am I simply defining these wrong?
As an addendum to this, I created two new fragments, WinningTeam and ParticipatingTeams and assigned them to winner and teams, respectively, and the build succeeds. However, those fragments are exactly the same, and this is not necessarily expected behavior. If I try to assign either of those fragments to both fields, the build fails the same way
Edit: clarity
I'll take a deeper look into this when I get back from moving, but I know we've had some issues with fragments that have the same name as the type name.
In general I'd expect the fragment name to be something more like TeamDetails or TeamIDAndName rather than having the same exact name as the type itself.
Fair enough, but either way, it produces unexpected results
Yeah agreed. I鈥檒l see what I can do when I鈥檓 back.
OK sorry I dropped the ball on this one. @MatrixSenpai I believe that you've basically found an undocumented edge case by naming the fragment the same thing as the type. I'm updating our documentation to tell people not to do this explicitly.
I've not had an issue with naming the fragments exactly the same as they are defined in the schema. The issue comes in when I try to use the same fragment in two different places as children of an object. For example, if I have the following schema
fragment Match on Match {
// ...
winner {
... Team
}
participants {
... Team
}
}
where winner: Team and participants: Array<Team>, that's where I run into an issue. It seems _specifically related_ to using the fragment in both a single context and as a set or collection
Ahh, ok. So what happens if you have:
fragment Match on Match {
// ...
winner {
... WinningTeam
}
participants {
... Team
}
}
If I'm following correctly, that should work. Does it?
Yep, that works perfectly. It's not a _breaking_ issue, just seems a little odd that I can't use the same type 馃槅
Hopefully, the switch to fragments as protocols outlined in #939 should alleviate this - it's a lot easier to have two objects which confirm to the same protocol than two types with the same name at the same level.
(I am going to close out my PR without merging since I misunderstood the use case)
Sounds good to me, I will test out #939 if/when it gets merged in! Until then, leaving open for further review and testing
To be clear #939 itself is an RFC, so that's not something that will be merged, but I'll try to ping you when the fragments-as-protocols stuff is actually merged.
Pinging @designatednerd since it looks like #939 was closed just a few days ago
The RFC was closed and I've started work on the rewrite, but it's going to be A Process. I'll make sure to update this issue when I get to it!