Course contentsShow
← Web Development
Lesson 1523 of 2,98228. GraphQL FundamentalsFree lesson

Object Types and Fields

Defining custom object types with fields that represent the data available in your API.

Object Types and Fields

You'll learn how to define custom object types in GraphQL that model your data and expose fields clients can query.

What Are Object Types?

In GraphQL, object types are the building blocks of your schema. They represent the "things" in your API—users, posts, products, comments—whatever entities your application deals with. Each object type is a collection of named fields, and each field has a type of its own.

Think of an object type like a blueprint or class definition. It describes what data is available and what shape it takes, but it doesn't contain actual data—that comes from your resolvers.

Defining an Object Type

Using the Schema Definition Language (SDL), you define an object type with the type keyword:

type User {
  id: ID!
  name: String!
  email: String!
  age: Int
}

Here, User is the object type, and id, name, email, and age are its fields. Each field has a type (like ID!, String!, or Int). The exclamation mark means the field is non-nullable—it must always have a value.

Fields Can Be Scalars or Other Objects

Fields don't just hold scalar types. They can reference other object types, creating relationships:

type Post {
  id: ID!
  title: String!
  author: User
}

The author field returns a User object, letting clients traverse relationships in a single query.

Why This Matters

Object types give your GraphQL API structure and predictability. Clients know exactly what fields are available and what types to expect, enabling powerful tooling like autocomplete and validation.

Key Takeaway: Object types define the entities in your GraphQL schema, and fields describe the data each entity exposes—creating a strongly-typed contract between client and server.