Skip to main content
GraphQL Basics
CHAPTER 06 Beginner

GraphQL Types and Scalars

Updated: May 13, 2026
15 min read

# CHAPTER 6

GraphQL Types and Scalars

1. Introduction

In the previous chapter, we learned that the Schema is the blueprint of a GraphQL API. But a blueprint is only as good as the materials used to build it. In GraphQL, those materials are Types. To accurately describe your data, you must understand the different data types available to you. In this chapter, we will explore the default Scalar types built into GraphQL, learn how to create Custom Scalars, and discuss how to use Enums to restrict data to specific values.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Identify and use the 5 built-in GraphQL Scalar types.
  • Understand the concept of Custom Scalars (like Date or JSON).
  • Use Enumeration (Enum) types to enforce strict data values.
  • Differentiate between Object types and Scalar types.

3. Beginner-Friendly Explanation

Think of an Object Type like a filing cabinet (e.g., User). It holds many things. Think of a Scalar Type like a single sheet of paper inside that cabinet. It is the absolute lowest level of data. It cannot be broken down any further.

If you ask for a User (Object), GraphQL says, "I can't just give you a 'User', you need to tell me *which pieces of paper* inside the User you want." You then ask for the name (String) and age (Int). Strings and Integers are Scalars. They are the actual data that gets sent over the internet.

4. Real-World Examples

  • E-commerce Product: A Product is an Object. Its price is a Float (Scalar). Its inStock status is a Boolean (Scalar). Its category might be restricted to only "ELECTRONICS", "CLOTHING", or "FOOD". That restriction is an Enum.
  • Event Management: You have an Event Object. You want to store when the event happens. GraphQL doesn't have a default "Date" type. So, you create a *Custom Scalar* called DateTime to handle calendar dates properly.

5. Detailed Code Examples

Let's define a schema that utilizes the various built-in scalars and an Enum.

Schema SDL:

graphql
1234567891011121314151617181920
# Enum: A special scalar restricted to these exact values
enum AccountStatus {
  ACTIVE
  SUSPENDED
  BANNED
}

# Object Type utilizing various Scalars
type UserProfile {
  id: ID!               # Unique identifier
  username: String!     # Text
  age: Int              # Whole number
  accountBalance: Float # Decimal number
  isVerified: Boolean   # True or false
  status: AccountStatus # Enum
}

type Query {
  getProfile: UserProfile
}

6. Query Examples

When querying, you request the scalars inside the object.
graphql
123456789
query {
  getProfile {
    id
    username
    accountBalance
    isVerified
    status
  }
}

7. Mutation Examples

When sending data via a mutation, the server strictly checks if the input matches the defined scalars and enums.
graphql
123456
mutation {
  updateStatus(id: "101", newStatus: SUSPENDED) {
    id
    status
  }
}

*(Notice that SUSPENDED is not in quotes. It is an Enum, not a String!)*

8. Schema Examples (Custom Scalars)

GraphQL only comes with 5 default scalars (String, Int, Float, Boolean, ID). If you want a Date or a URL, you must define it yourself.

Defining Custom Scalars:

graphql
12345678
scalar DateTime
scalar EmailAddress

type Message {
  content: String!
  sentAt: DateTime!
  senderEmail: EmailAddress!
}

*Note: While you define scalar DateTime in the schema, you must write the PHP validation logic in your backend to ensure it is actually a valid date.*

9. Best Practices

  • Use Enums Instead of Strings: If a field only has a set list of allowed values (like roles: ADMIN, USER, GUEST), always use an Enum. It prevents typos and makes the API self-documenting.
  • Use the ID type: Even though an ID might look like a String or an Integer in your database, always type it as ID in GraphQL. It tells the frontend developer, "This field is meant as a unique identifier, not for math or display."
  • Leverage Community Scalars: Instead of writing complex validation logic for custom scalars (like validating an Email format), look for open-source GraphQL scalar libraries in PHP.

10. Common Mistakes

  • Confusing Int and Float: Trying to pass a decimal value (e.g., 10.50) to an Int field will crash the query. Always use Float for money or measurements.
  • Putting Quotes Around Enums: In JSON/GraphQL queries, Enums are written without quotes (status: ACTIVE, not status: "ACTIVE").

11. Mini Exercises

  1. 1. List the 5 default scalar types in GraphQL.
  1. 2. If you want to store a user's height in meters (e.g., 1.75), which scalar should you use?
  1. 3. Look at this schema: type Item { weight: Int }. Can a client request 12.5 as the weight?

12. Coding Challenges

Challenge 1: Write an SDL schema for a Vehicle. It should have a unique identifier, a text field for the manufacturer, a true/false field for whether it is electric, and an Enum field called VehicleType that can only be SEDAN, TRUCK, or SUV.

13. MCQs with Answers

Question 1

Which of the following is NOT a built-in GraphQL scalar type?

Question 2

What is the primary purpose of an Enum type?

Question 3

How is the ID scalar serialized (formatted) when sent back to the client?

14. Interview Questions

  • Q: Explain the difference between an Object type and a Scalar type.
  • Q: Why would you define a Custom Scalar instead of just using a String?
  • Q: What is an Enum, and when should you use one?

15. FAQs

Q: Can a Custom Scalar be anything, even a complex JSON object? A: Yes. You can define a scalar JSON that tells GraphQL, "Accept whatever complex JSON object is passed here without strictly checking the internal fields." This is useful for unstructured data but bypasses GraphQL's type-safety.

Q: Do Enums have to be uppercase? A: By convention, yes, but it is not strictly required by the language. It makes them easily distinguishable from standard strings.

16. Summary

In this chapter, we explored the foundational building blocks of GraphQL data. We learned the 5 built-in scalars (Int, Float, String, Boolean, ID) that represent raw data. We discussed how to enforce strict data constraints using Enum types. Finally, we looked at how to expand GraphQL's capabilities by defining Custom Scalars for specific data formats like Dates or Emails.

17. Next Chapter Recommendation

With schemas and types fully understood, it is time to master the art of fetching data. Proceed to Chapter 7: GraphQL Queries to learn advanced techniques for structuring your requests, navigating relationships, and retrieving exactly what you need.

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: ·