Skip to content

Commit 0db4cdd

Browse files
committed
Support empty composite types
Supporting empty types, e.g. objects, input objects and interfaces without any fields, has concrete use cases. - Support for GraphQL APIs without any `Query` operation type fields, for example, if the API only support mutations or subscriptions (see #490). This allows defining an empty `Query` object type, while still supporting introspection. - Support for algebraic data types (see #568), where `__typename` is the only relevant field - Potentially to support "well-known" (but empty) extensions of the introspection schema (see #300) This is a minimalist spec change, which simply removes the relevant items under the type validation sub sections. It would probably be helpful to motivate the change and mention that one or more fields _should_ be present for a (typically) useful schema. The requirement for composite types (object, input objects and interfaces) to define "one or more fields" was introduced in 0599414. This change references graphql/graphql-js#368, motivating the change with: > Since GraphQL always requires you to select fields down to scalar values, an Object type without any defined fields cannot be accessed in any way in a query. > This could be even more problematic for Input Objects where a required input object argument with no fields could result in a field that is impossible to query without producing an error. With regards to these objections: - It's always possible to select `__typename` and therefore even empty object types can be useful (as e.g. algebraic data types) - Passing an empty input object appears syntactically valid: ```gql mutation { update(input: {}) { name } } ``` I think this proposal fulfills the guiding principles by enabling new capabilities motivated by real use cases. This change does not make any previously valid schema invalid, so largely preseves backwards compatibility. Fixes #568 and #490 at the specification level.
1 parent 22feb9c commit 0db4cdd

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

spec/Section 3 -- Type System.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,7 @@ Objects are never valid inputs.
792792
Object types have the potential to be invalid if incorrectly defined. This set
793793
of rules must be adhered to by every Object type in a GraphQL schema.
794794

795-
1. An Object type must define one or more fields.
796-
2. For each field of an Object type:
795+
1. For each field of an Object type:
797796
1. The field must have a unique name within that Object type;
798797
no two fields may share the same name.
799798
2. The field must not have a name which begins with the
@@ -804,8 +803,8 @@ of rules must be adhered to by every Object type in a GraphQL schema.
804803
characters {"__"} (two underscores).
805804
2. The argument must accept a type where {IsInputType(argumentType)}
806805
returns {true}.
807-
4. An object type may declare that it implements one or more unique interfaces.
808-
5. An object type must be a super-set of all interfaces it implements:
806+
2. An object type may declare that it implements one or more unique interfaces.
807+
3. An object type must be a super-set of all interfaces it implements:
809808
1. The object type must include a field of the same name for every field
810809
defined in an interface.
811810
1. The object field must be of a type which is equal to or a sub-type of
@@ -1052,8 +1051,7 @@ Interfaces are never valid inputs.
10521051

10531052
Interface types have the potential to be invalid if incorrectly defined.
10541053

1055-
1. An Interface type must define one or more fields.
1056-
2. For each field of an Interface type:
1054+
1. For each field of an Interface type:
10571055
1. The field must have a unique name within that Interface type;
10581056
no two fields may share the same name.
10591057
2. The field must not have a name which begins with the
@@ -1410,8 +1408,7 @@ Literal Value | Variables | Coerced Value
14101408

14111409
**Type Validation**
14121410

1413-
1. An Input Object type must define one or more input fields.
1414-
2. For each input field of an Input Object type:
1411+
1. For each input field of an Input Object type:
14151412
1. The input field must have a unique name within that Input Object type;
14161413
no two input fields may share the same name.
14171414
2. The input field must not have a name which begins with the

0 commit comments

Comments
 (0)