graphql / graphql/graphql-spec
The ability to represent empty objects
- Dominant language
- JavaScript
- Stars
- 14.6k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Based on this: https://github.com/graphql/graphql-js/issues/937#issuecomment-432857507
Currently we can't represent an empty object type:
```graphql
type Mutation {
createUser(email: String!, password: String!): CreateUserPayload!
}
type UserWithEmailAlreadyExists
type WeakPassword
type AuthPayload {
user: User!
token: AccessToken!
}
union CreateUserPayload = UserWithEmailAlreadyExists | WeakPassword | AuthPayload
```
This fails with `graphql-js` with the following error:
```
Error: Type UserWithEmailAlreadyExists must define one or more fields.
Type WeakPassword must define one or more fields.
at assertValidSchema (/Users/skainswo/dev/kumo/newer-world/api/node_modules/graphql/type/validate.js:71:11)
at Object.validate (/Users/skainswo/dev/kumo/newer-world/api/node_modules/graphql/validation/validate.js:55:35)
at doRunQuery (/Users/skainswo/dev/kumo/newer-world/api/node_modules/apollo-server-core/src/runQuery.ts:181:30)
at /Users/skainswo/dev/kumo/newer-world/api/node_modules/apollo-server-core/src/runQuery.ts:80:39
at process._tickCallback (internal/process/next_tick.js:68:7)
```
The usecase is legit though.
There is an explicit check to disallow types with zero fields at `graphql-js`:
https://github.com/graphql/graphql-js/blob/4116e2fc4fe36688f683258388f4a2d52076d199/src/type/validate.js#L273-L278
How about explicitly allowing empty fields in the spec? It's super useful for implementing algebraic types.
Contributor guide
Assessment
This issue has not been assessed yet.