Schema
GraphQL Objects
Defining Object Types
GraphQL objects define structured types with fields.
What is a GraphQL Object?
GraphQL Objects are one of the core building blocks of a GraphQL schema. They represent a type with a set of fields, where each field can be of a scalar type or another object type. In other words, objects define the shape of data you can query within your GraphQL API.
Every object type in GraphQL has a name and specifies the fields it supports. Each field has a name and a type, which can be a scalar, an object, or a list, among other types.
Defining a GraphQL Object Type
To define an object type in GraphQL, you typically use the type
keyword followed by the name of the object. You then define its fields inside curly braces. Each field is specified with a name and a type, which can be a scalar or another GraphQL object type.
In the example above, the Book
object type has three fields: title
, author
, and publishedYear
. The title
is a String
, author
is another object type Author
, and publishedYear
is an Int
.
Non-Null and List Modifiers
GraphQL provides modifiers to define whether a field can be null or if it should hold a list of values. These modifiers are !
(non-nullable) and []
(list of values).
In the Author
type, name
is a non-nullable String
, indicating that every Author
must have a name. The books
field is a list of Book
objects, which means an author can have multiple books, and this list itself is non-nullable.
Using Objects in Queries
Once you've defined your object types, you can query these objects and their fields. A GraphQL query specifies which fields you want to retrieve from an object.
This query fetches a Book
by its title
and retrieves the title
, the author's name
, and the publishedYear
of the book "1984".
GraphQL Schema
- Previous
- Scalars
- Next
- Interfaces