Skip to content

Named Lists #914

Open
Open
@nalchevanidze

Description

@nalchevanidze

Named Lists

Motivation

Since GraphQL has only one type (List) for collections, we cannot represent collections with certain constraints.

examples of constrained lists:

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions