Skip to content

Types: Object, Scalar, and Enum

DodaTech Updated 2026-06-28 3 min read

In this tutorial, you will learn about Types: Object, Scalar, and Enum. We cover key concepts, practical examples, and best practices to help you master this topic.

GraphQL has a strong type system categorizing data into object types with fields, scalar types for leaf values, and enum types for constrained allowed values. Understanding these types is essential for schema design.

What You'll Learn

  • Object type structure: fields, arguments, and return types
  • Built-in scalar types: String, Int, Float, Boolean, ID
  • Custom scalar types for domain-specific values
  • Enum types for fixed value sets

Why It Matters

The type system enforces data shape guarantees at the API boundary. Clients never have to guess the type of a field. Custom scalars and enums make schemas self-documenting and reduce validation code.

Real-World Use

Shopify uses custom scalar types like DateTime, JSON, and URL to Express domain semantics. GitHub's schema uses enums for Repository visibility (PUBLIC, PRIVATE, INTERNAL) and issue states (OPEN, CLOSED).

flowchart LR
    TypeSystem[GraphQL Type System] --> ObjectType[Object Type]
    TypeSystem --> ScalarType[Scalar Type]
    TypeSystem --> EnumType[Enum Type]
    ObjectType --> Fields[Fields with types]
    ScalarType --> BuiltIn[Built-in: String Int Float Boolean ID]
    ScalarType --> Custom[Custom: Date JSON URL Email]
    EnumType --> Values[Named values: LOW MEDIUM HIGH]

Teacher Mindset

Object types describe entities with multiple properties. Scalars are leaf nodes that terminate the graph. Enums constrain input to predefined values. Think of scalars as the atoms and objects as the molecules of your schema.

Code Examples

# Example 1: Object type with various field types
type Product {
  id: ID!
  name: String!
  price: Float!
  inStock: Boolean!
  tags: [String!]!
}
# Example 2: Custom scalar definition and usage
scalar Date
scalar Email
scalar JSON

type User {
  id: ID!
  email: Email!
  registeredAt: Date!
  metadata: JSON
}
# Example 3: Enum type with values
enum OrderStatus {
  PENDING
  CONFIRMED
  SHIPPED
  DELIVERED
  CANCELLED
}

type Order {
  id: ID!
  status: OrderStatus!
  total: Float!
}

Common Mistakes

  • Using String for domain-specific values instead of custom scalars or enums
  • Making all fields nullable when non-null (ID!) is more appropriate
  • Forgetting to implement serialize, parseValue, and parseLiteral for custom scalars
  • Using enums for dynamic values that change frequently
  • Naming enum values inconsistently (UPPER_CASE vs PascalCase)

Practice

  1. Define an enum Color with values RED, GREEN, BLUE.
  2. Create a custom scalar PhoneNumber.
  3. Design an object type Review with fields for rating, body, and author.
  4. Implement parseValue and parseLiteral for a custom Currency scalar.
  5. Challenge: Create a schema for a movie database with types for Movie, Actor, Genre enum, and a custom Year scalar.

FAQ

What is the difference between String and ID?

ID is a special scalar for unique identifiers. It serializes as a String but signals that the value is not meant to be human-readable.

Can I create custom scalars in any GraphQL server?

Yes. Most GraphQL implementations (Apollo, graphql-js, graphql-ruby) support custom scalar definitions with serialize/parseValue/parseLiteral methods.

Should I use String or Enum for status fields?

Use Enum when the allowed values are fixed and known at schema definition time. Use String when the value set is dynamic or user-defined.

What types of fields can object types have?

Object type fields can return any other type: scalars, enums, other objects, interfaces, unions, or lists of any of these.

Can an object type implement multiple interfaces?

Yes. An object type can implement one or more interfaces, which guarantees it includes the interface fields.

Mini Project

Extend your library schema from the previous lesson by adding a custom Date scalar for publication dates, an enum Genre, and a custom JSON scalar for book metadata. Ensure all fields use appropriate typing.

What's Next

Next, you will learn how to model relationships between types: one-to-one, one-to-many, and many-to-many associations.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro