0
0
GraphQLquery~5 mins

List types in GraphQL

Choose your learning style9 modes available
Introduction

List types let you store multiple values of the same kind together. It helps when you want to group related items in one place.

When you want to get a list of books from a library database.
When you need to store multiple phone numbers for one person.
When you want to return all comments on a blog post.
When you want to send a list of products in a shopping cart.
When you want to show all friends of a user in a social app.
Syntax
GraphQL
type ExampleType {
  fieldName: [Type]
}

Square brackets [] around a type mean it is a list of that type.

The list can have zero or more items.

Examples
This means a user has a name and a list of favorite colors.
GraphQL
type User {
  name: String
  favoriteColors: [String]
}
A post has a title and a list of tags.
GraphQL
type Post {
  title: String
  tags: [String]
}
A library has a list of books, each book has a title and author.
GraphQL
type Library {
  books: [Book]
}

type Book {
  title: String
  author: String
}
Sample Program

This GraphQL schema defines a user with a list of favorite colors. The query asks for the user's name and all their favorite colors.

GraphQL
type Query {
  getUser: User
}

type User {
  name: String
  favoriteColors: [String]
}

# Sample query to get user data
query {
  getUser {
    name
    favoriteColors
  }
}
OutputSuccess
Important Notes

Lists can be empty, which means no items are present.

You can have lists of any type, including custom types.

Remember to use square brackets [] to mark a field as a list.

Summary

List types hold multiple values of the same type.

Use square brackets [] to define a list.

Lists can be empty or have many items.