Open
Description
Named Lists
Motivation
Since GraphQL has only one type (List) for collections, we cannot represent collections with certain constraints.
examples of constrained lists:
- Set: no duplicated elements
- NonEmpty: contains at least 1 element.
- Map: elements must provide unique field
key
(see Extend Wrapping Types with additional primitive Tuple #904).
if we could communicate this constraints, clients could eliminate invalid queries before they are sent and would not need to validate response types. e.g, the client would not need to check a resulting list is not empty.
Definition of Named Lists
"""
list with no duplicates
"""
list Set
list NonEmpty
type User {
name: String!
}
type Query {
ids:Set<Int>
users: NonEmpty<User>
}
validation of Named Lists
const validateNonEmpty = list => {
if (list.length < 1)
throw new Error ("empty List!")
}
return list
}
const resolverMap = {
NonEmpty: new GraphQLWrapperType({
name: 'NonEmpty',
kind: 'LIST',
description: 'NonEmpty list',
parseValue: validateNonEmpty,
serialize: validateNonEmpty,
})
}
Introspection
introspection of field ids
.
"name": "ids",
"type": {
"name": "Set",
"kind": "LIST",
"type": {
"name":"Int",
...
}
}
introspection of field users
.
"name": "users",
"type": {
"name": "NonEmpty",
"kind": "LIST",
"type": {
"name":"User",
...
}
}
Non-breaking change
Older clients that do not support named lists, can still recognise them as regular lists and will not break. However, they do not benefit from knowing this constraints.
references
the current proposal is already implemented in the experimental language iris
initial discussions about the proposal could be found at:
Metadata
Metadata
Assignees
Labels
No labels