You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rfcs/InputUnion.md
+42-2Lines changed: 42 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,6 +148,8 @@ The premise of this RFC - GraphQL should contain a polymorphic Input type.
148
148
149
149
Any data structure that can be modeled with output type polymorphism should be able to be mirrored with Input polymorphism. Minimal transformation of outputs should be required to send a data structure back as inputs.
150
150
151
+
* Objection: input types and output types are distinct. Output types support aliases and arguments whereas input types do not. Marking an output field as non-nullable is a non-breaking change, but marking an input field as non-nullable is a breaking change.
Adding a new member type to an Input Union or doing any non-breaking change to existing member types does not result in breaking change. For example, adding a new optional field to member type or changing a field from non-nullable to nullable does not break previously valid client operations.
@@ -160,10 +162,48 @@ If a solution places any restrictions on member types, compliance with these res
160
162
161
163
In addition to containing Input types, member type may also contain Leaf types like `Scalar`s or `Enum`s.
162
164
163
-
* Objection: Multiple Leaf types serialize the same way, making it impossible to distinguish the type. For example, a `String`, `ID` and `Enum`.
164
-
* Potential restrictions to fix: Limit to one leaf type in a union?
165
+
* Objection: multiple Leaf types serialize the same way, making it impossible to distinguish the type without additional information. For example, a `String`, `ID` and `Enum`.
166
+
* Potential solution: only allow a single built-in leaf type per input union.
165
167
* Objection: Output polymorphism is restricted to Object types only. Supporting Leaf types in Input polymorphism would create a new inconsistency.
166
168
169
+
### Changing field from an input type to an input union including that type is non-breaking
170
+
171
+
Since the input object type is now a member of the input union, existing input objects being sent through should remain valid.
172
+
173
+
* Objection: achieving this by indicating the default in the union (either explicitly or implicitly via the order) is undesirable as it may require multiple equivalent unions being created where only the default differs.
174
+
* Objection: achieving this by indicating a default type in the input field is verbose/potentially ugly.
175
+
176
+
### Input unions may include other input unions
177
+
178
+
To ease development.
179
+
180
+
### Input unions don't need "discriminator" field
181
+
182
+
Differentiate types structurally, e.g. by checking the input value against each candidate input type in turn until a viable match is found.
183
+
184
+
* Objection: many input objects may not have unique required attributes (i.e. are heterogenous), leading to ambiguity.
185
+
* Potential solution: input union order is signficant.
186
+
* Objection: making a field in an input object nullable may result in an existing input value being interpretted as a different input object type at a later time (breaks forwards compatibility).
187
+
* Objection: does not work with scalars.
188
+
* Objection: this may have a noticable performance cost for larger input unions.
189
+
190
+
### Input unions don't use wrapper objects
191
+
192
+
For consistency with output unions/interfaces each union should resolve to a single JSON object; i.e. `{__typename:"MyInput",value:3}` rather than `{MyInput:{value:3}}`.
193
+
194
+
* Objection: wrapper objects can be more concise than including `__typename` or similar in each value.
195
+
* Objection: the "tagged union" pattern is already in common usage.
196
+
* Objection: this pattern enables support for leaf types (e.g. scalars) in unions.
197
+
* Objection: this pattern enables support for individual input object list types in unions.
198
+
199
+
### Input unions should be easy to upgrade from existing solutions
200
+
201
+
Many people in the wild are solving the need for input unions with validation at run-time (e.g. using the "tagged union" pattern). Formalising support for these existing patterns in a non-breaking way would enable existing schemas to become retroactively more type-safe.
202
+
203
+
### A GraphQL schema that supports input unions can be queried by older GraphQL clients
204
+
205
+
Preferably without loss of functionality.
206
+
167
207
## Use Cases
168
208
169
209
There have been a variety of use cases described by users asking for an abstract input type.
0 commit comments