Replies: 5 comments 8 replies
-
well, this is insane then. So if I (human) looking at an Intersection definition, and I have to figure out what is there, at the end, what fields would be available for selection, I will have to go through the lists of types and interfaces, manually track who implements what and do all types implement all interfaces etc, So instead of helping me, human, it actually makes it much more difficult for me The union-with-interfaces approach actually makes more sense. A union with a bunch of types and an interface makes it explicit for me (reader), that at least these fields are available in every one of the types, you do not have to go thru all types and verify it yourself. |
Beta Was this translation helpful? Give feedback.
-
I do not understand it at all, what is unclear about an interface?
who is YOU? the server side developer, designing and implementing GraphQL API and writing up its schema? He is then in full charge of implementation, and can ensure only those types are returned by the field resolver |
Beta Was this translation helpful? Give feedback.
-
quick question - is there any similar concept to Intersection, in any programming languages? Because for other things like interfaces, we can fall back on our pre-existing knowledge in programming. Here I am afraid there's nothing to fallback on? is this why I am so confused and still not getting the concept? |
Beta Was this translation helpful? Give feedback.
-
no, that does not make it as an example of Intersection in other languages; it is a syntactic sugar, just named Intersection. catch(SomeException|OtherException|ThirdExc ex) or making things like They do not declare it as distinct type reusable elsewhere. Just a shorter way for multiple catch blocks or 'is' statements |
Beta Was this translation helpful? Give feedback.
-
Summary: Motivation:
Use regular unionsIf we define HousePetNode as Pro: simple. Use unions with additional constraintIf we define HousePetNode as Pro: One cannot introduce a type to
Use intersectionsIf we define HousePetNode as Pro:
Use duplicate interfacesWe can define HousePetNode as Pro:
|
Beta Was this translation helpful? Give feedback.
-
As an alternative to "unions implementing interfaces" (graphql/graphql-spec#939), a new intersection type could be introduced that models the same behavior.
Spec PR: graphql/graphql-spec#941
Implementation PR: graphql/graphql-js#3550
Example Behavior
Motivation
intersection "implementing" interface
intersection "implementing" interface AND narrowing unions
Tradeoffs between intersections and simply allowing unions to declare "implementation" of interfaces
What are some of the downsides?
Open questions
Example and further discussion for question 1
Should we allow:
vs. requiring
As we can see for above, ancestor interfaces are already required for readability reasons on object and interface types. The same argument could be made here -- and I would tend to agree with it! Note that the spec/implementation PRs now implements this requirement.
Example and further discussion for question 2
Should we allow:
Or require:
One could argue that similarly to the required verbosity for interfaces, we should require intersections to explicitly list all of their members and not include any other intersections. The spec/PR now implements this requirement.
Example and further discussion for question 3
Should we allow:
or require:
An argument in favor of allowing the former is that for this particular intersection, name is always defined, so this should be fine. An argument against allowing could be just from theory => the intersection itself does not define any fields, it just does so transitively through the interface it includes, so it is "wrong" to allow querying on the type. Because some intersections may contain only unions and not have any fields, it might be simpler to "teach" intersections as being more similar to unions, and never allow querying for fields directly on the intersection. I lean against allowing it, but not strongly.
Example and further discussion for question 4
Intersection changes could break all sorts of queries. Adding another union or interface MAY remove types, if any of the previously included types are not in the newly added union or do not implement the newly added interface. For example, adding
AnotherInterface
to the intersection would cause all types to be dropped. Removing an interface could also break a type, for the same reason that removing an interface from an object type is breaking. Removing the last union causes all types to drop from the intersection.Intersections are therefore to some degree more immutable than other types. The current implementation of this PR assumes that includes removal or addition of ANY constraining types within the results of
findBreakingChanges
. This could be pared back, as adding constraining types may not cause additional object types to drop if the new types are more expansive than the old. Removing a union should only cause types to drop if it is the last remaining union (at least as long as unions can only containing object types and not interfaces).Beta Was this translation helpful? Give feedback.
All reactions