graphql / graphql/graphql-spec

can't enforce presence of array elements

Open
#720 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Neither of the GraphQL type decorations can enforce that an array should have some items:
- `[Type!]!` means a mandatory array of non-null items, but there can be 0 items
- `[Type]!` means a mandatory array of items that can be null or missing altogether
- `[Type!]` means an optional array of non-null items, and there can be 0 items

So there's no way to say "this multi-valued field must have at least one value"

https://www.apollographql.com/blog/using-nullability-in-graphql-2254f84c4ed7 by @stubailo puts it like this: "One interesting conclusion here is that there’s no way to specify that the list can’t be empty — an empty list `[]` is always valid, regardless of whether the list or items are non-null."

Interesting indeed; but I think this really is a failure of the spec to capture this basic requirement. You can control whether array elements are non-null, which is not a common requirement (and given #719, you should never use); but you cannot state there must be some elements, which is a very common requirement.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.