Skip to main content
GraphQL Basics
CHAPTER 05 Beginner

GraphQL Schema Fundamentals

Updated: May 13, 2026
20 min read

# CHAPTER 5

GraphQL Schema Fundamentals

1. Introduction

At the absolute core of every GraphQL API is the Schema. You cannot have a GraphQL server without one. The schema serves as a strict contract between the client (the frontend) and the server (the backend). It defines exactly what data can be requested, what data can be changed, and how everything is connected. In this chapter, we will explore the Schema Definition Language (SDL), understand how to define Types and Fields, and learn about the Root Query—the entry point to your API.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain what a GraphQL Schema is and why it is necessary.
  • Read and write GraphQL Schema Definition Language (SDL).
  • Define custom Object Types and their corresponding fields.
  • Understand the concept of "Root Types" (Query, Mutation).
  • Architect a basic, real-world schema structure.

3. Beginner-Friendly Explanation

Imagine building a new house. Before the builders pour concrete or lay bricks, an architect creates a blueprint. The blueprint defines exactly how many rooms there are, where the doors are, and how the rooms connect.

In GraphQL, the Schema is your blueprint.

  • You define Types (the rooms): e.g., a User, a Product.
  • You define Fields (what's inside the rooms): e.g., name, price, email.
  • You define the Root Query (the front door): How do people enter the house to find the rooms?

Because of this strict blueprint, the frontend developer knows *exactly* what they can ask for without ever having to guess or look at PHP database code.

4. Real-World Examples

  • The Library App: If you are building an API for a library, your schema would define a Book type, an Author type, and a Member type. The schema explicitly states that a Book has a title (String) and an author (which points to the Author type).
  • Auto-generating Documentation: Because the schema acts as a strict typing system, tools like Apollo Explorer automatically read the schema and generate beautiful, clickable documentation for your API. You never have to manually write API docs again!

5. Detailed Code Examples

GraphQL has its own simple language for writing schemas called SDL (Schema Definition Language). Let's look at how we define types.

Defining Types using SDL:

graphql
1234567891011121314151617
# This defines a custom object type
type User {
  id: ID!
  username: String!
  email: String!
  age: Int
  isActive: Boolean
}

# The Root Query is how clients fetch data
type Query {
  # Field name: returns a single User
  getUser(id: ID!): User
  
  # Returns a list of Users
  getAllUsers: [User!]!
}

*(Note: The ! exclamation mark means the field is required and cannot be null.)*

6. Query Examples

Based on the schema above, a client can execute the following query:
graphql
123456
query {
  getUser(id: "123") {
    username
    isActive
  }
}

7. Mutation Examples

Just like the Root Query type, there is a Root Mutation type for changing data.

In the Schema:

graphql
123
type Mutation {
  createUser(username: String!, email: String!): User
}

The Client's Execution:

graphql
123456
mutation {
  createUser(username: "johndoe", email: "john@example.com") {
    id
    username
  }
}

8. Schema Examples (PHP vs SDL)

In Chapter 4, we built the schema programmatically in PHP. Alternatively, you can write the schema in a .graphql file using SDL, and tell PHP to read it!

schema.graphql

graphql
1234567
type Post {
  title: String!
  content: String
}
type Query {
  latestPost: Post
}

PHP Loading SDL:

php
1234567
<?php
use GraphQL\Utils\BuildSchema;

$contents = file_get_contents(&#039;schema.graphql');
$schema = BuildSchema::build($contents);
// You can now execute queries against this schema!
?>

9. Best Practices

  • Design the Schema First: Embrace "Schema-First Design." Before writing any PHP or frontend code, backend and frontend developers should sit down, agree on the Schema SDL, and save it. Both teams can then work independently.
  • Use Descriptive Names: Name your fields clearly. Use camelCase for fields (firstName) and PascalCase for Types (UserProfile).
  • Use Comments: You can add descriptions to your schema using quotes (""). This populates the API documentation.
graphql
1234
"""
Represents a registered customer on the platform.
"""
type Customer { ... }

10. Common Mistakes

  • Forgetting Required Flags (!): If you know an ID or a Name will *always* exist in the database, mark it with !. If you don't, frontend developers have to write unnecessary null-checking code.
  • Leaking Database Structure: Don't just copy your MySQL tables 1:1 into GraphQL. The schema should represent the *business logic* and how the UI needs the data, not how the database stores it.

11. Mini Exercises

  1. 1. Look at this SDL: type Movie { title: String!, rating: Float }. Is the rating required or optional?
  1. 2. Write a schema definition for a Song that has an id, a title, and a duration in seconds.
  1. 3. Define a Root Query that allows fetching a list of Songs.

12. Coding Challenges

Challenge 1: Write a complete GraphQL SDL snippet that defines an Employee type (with id, name, and salary) and a Query type to fetch a single Employee by ID. Ensure the ID and Name are strictly required.

13. MCQs with Answers

Question 1

What does SDL stand for in GraphQL?

Question 2

What does the exclamation mark (!) indicate in a GraphQL schema?

Question 3

What are the special "Root Types" in a GraphQL schema?

14. Interview Questions

  • Q: Explain "Schema-First Design" and why it benefits development teams.
  • Q: How do you define a list/array of items in a GraphQL schema?
  • Q: If you have an id field that should never be null, how do you write that in SDL?

15. FAQs

Q: Can I change my schema later? A: Yes! However, you should be careful about removing fields that existing clients are using. Usually, you add a @deprecated directive to old fields and add new ones alongside them.

Q: Do I have to write the schema in a .graphql file, or in PHP arrays? A: Either approach works. Writing it in a .graphql file (Schema-First) is generally preferred because it is easier to read and share with frontend teams.

16. Summary

In this chapter, we explored the beating heart of GraphQL: the Schema. We learned how to use the Schema Definition Language (SDL) to create blueprints for our data. We created custom Object Types, assigned them fields, and learned how to make fields required using the ! symbol. Finally, we looked at how Root Types (Query and Mutation) act as the entry points for the client to interact with the API.

17. Next Chapter Recommendation

We have seen basic types like String and Int. But what if you need complex data types, enumerations, or custom formats like Dates? Proceed to Chapter 6: GraphQL Types and Scalars to dive deep into the specific data types that make up a GraphQL Schema.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·