Back to Blog
Lesson 4 of the GraphQL: Your First GraphQL Schema & Server course
API ArchitectureJuly 15, 20263 min read

Understanding the GraphQL Schema: Building Your API Contract

Master the GraphQL Schema Definition Language (SDL). Learn to define object types and establish a strict API contract for your backend services.

GraphQLSDLSchemaAPI DesignBackend
A detailed close-up of a printed contract document on a wooden table surface.

Previously in this course, we explored the Anatomy of a Query and discussed why a Single Endpoint model relies on a rigid contract to function. Today, we move from consuming data to defining it by building your first GraphQL schema using the Schema Definition Language (SDL).

The Schema as the Source of Truth

In REST, the "contract" is often implicit—it lives in documentation like Swagger or, worse, in the code itself. If the backend changes a field name, the frontend breaks silently.

GraphQL flips this. The Schema is the definitive source of truth. It is a strictly typed blueprint that describes exactly what data your API can provide and how clients can ask for it. The SDL (Schema Definition Language) is the human-readable syntax used to write this blueprint. Because it is language-agnostic, your schema remains consistent whether you implement your server in Node.js, Python, or Go.

Defining Your First Object Type

At the heart of the SDL are Object Types. An object type represents a resource in your system, such as a User, Product, or Book.

Think of an object type as a template. Each type has a name and a set of fields, where each field is associated with a specific data type.

Here is what a basic Book object looks like in SDL:

GraphQL
type Book {
  id: ID
  title: String
  author: String
  publishedYear: Int
}

Breaking down the SDL syntax:

  1. type: The keyword used to define a new object.
  2. Book: The name of the type (usually PascalCase).
  3. id, title, etc.: The fields available for this object.
  4. ID, String, Int: These are "Scalars." They represent the leaf nodes of your data tree—the actual values that can't be broken down further.

The API Contract in Action

When you define this schema, you are establishing a contract. If a client requests a Book, the API guarantees it will return these fields. If a client asks for a field not in this schema, the GraphQL server will reject the request before it even reaches your database.

This strictness is why API Design using a Schema Registry is so powerful in larger organizations; it forces both sides of the application to adhere to the same rules.

Hands-on Exercise: Defining a Library Schema

Let’s advance our running project by sketching out the schema for a simple library application.

Your Task:

  1. Define a Library type.
  2. It should contain a name (String), location (String), and isPublic (Boolean).
  3. Define an Author type with a name (String) and birthYear (Int).

Write these out in a text file or your code editor to get comfortable with the syntax.

Common Pitfalls to Avoid

  • Case Sensitivity: GraphQL types should always be PascalCase (e.g., Book), while fields should be camelCase (e.g., publishedYear).
  • Confusing Scalars with Types: Remember that String or Int are built-in scalars. Don't try to define them yourself.
  • Missing the Root Types: While we've defined custom objects, a valid schema also requires "Root Types" (Query, Mutation, Subscription) to act as entry points. We will cover those in detail in upcoming lessons.

FAQ

Is the SDL the same as the code? No. The SDL is a language-neutral text format. Your server-side code (like Node.js) will parse this SDL to build an internal representation of your schema.

Can I add fields to an object later? Yes. GraphQL is designed to be evolvable. You can add new fields to your object types without breaking existing clients who don't know about those new fields yet.

Does every field need a type? Yes. Every single field in your schema must be assigned a scalar or another object type.

Recap

The SDL is the backbone of your GraphQL API. By defining your data structures as explicit object types, you create a self-documenting, strongly-typed contract that prevents data mismatch errors. You’ve now learned how to define basic object types—the building blocks of any complex API.

Up next: We will explore the Client-Server Relationship and see how these object types are requested during the request-response cycle.

Similar Posts