GraphQL Types and Scalars
# 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
Productis an Object. Itspriceis aFloat(Scalar). ItsinStockstatus is aBoolean(Scalar). Itscategorymight be restricted to only "ELECTRONICS", "CLOTHING", or "FOOD". That restriction is anEnum.
-
Event Management: You have an
EventObject. You want to store when the event happens. GraphQL doesn't have a default "Date" type. So, you create a *Custom Scalar* calledDateTimeto 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:
6. Query Examples
When querying, you request the scalars inside the object.7. Mutation Examples
When sending data via a mutation, the server strictly checks if the input matches the defined scalars and enums.*(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:
*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
IDmight look like a String or an Integer in your database, always type it asIDin 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 anIntfield will crash the query. Always useFloatfor money or measurements.
-
Putting Quotes Around Enums: In JSON/GraphQL queries, Enums are written without quotes (
status: ACTIVE, notstatus: "ACTIVE").
11. Mini Exercises
- 1. List the 5 default scalar types in GraphQL.
-
2.
If you want to store a user's height in meters (e.g.,
1.75), which scalar should you use?
-
3.
Look at this schema:
type Item { weight: Int }. Can a client request12.5as the weight?
12. Coding Challenges
Challenge 1: Write an SDL schema for aVehicle. 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
Which of the following is NOT a built-in GraphQL scalar type?
What is the primary purpose of an Enum type?
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 ascalar 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.