graphql / graphql/graphql-spec

Named Lists

Open
#914 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

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 #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

```gql
"""
list with no duplicates
"""
list Set
list NonEmpty

type User {
name: String!
}

type Query {
ids:Set
users: NonEmpty
}
```

### validation of Named Lists

```ts
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`.

```json
"name": "ids",
"type": {
"name": "Set",
"kind": "LIST",
"type": {
"name":"Int",
...
}
}
```

introspection of field `users`.

```json
"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](https://github.com/nalchevanidze/iris)

initial discussions about the proposal could be found at:
- [morpheus](https://github.com/morpheusgraphql/morpheus-graphql/pull/479)
- [morpheus-proposals](https://github.com/morpheusgraphql/graphql-proposals)

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.