0
0
GraphQLquery~5 mins

Scalar types (String, Int, Float, Boolean, ID) in GraphQL

Choose your learning style9 modes available
Introduction

Scalar types are the basic building blocks for data in GraphQL. They represent simple values like text, numbers, or true/false.

When you want to store a person's name as text.
When you need to save an age as a whole number.
When you want to record a price with decimals.
When you need to store a true or false value, like if a user is active.
When you want to uniquely identify an object with an ID.
Syntax
GraphQL
type Example {
  name: String
  age: Int
  price: Float
  isActive: Boolean
  id: ID
}

String is for text like names or descriptions.

ID is a unique identifier, often used for objects.

Examples
This defines a person with two text fields for names.
GraphQL
type Person {
  firstName: String
  lastName: String
}
This defines a product with a decimal price and whole number quantity.
GraphQL
type Product {
  price: Float
  quantity: Int
}
This defines a user with a true/false status and a unique ID.
GraphQL
type User {
  isActive: Boolean
  id: ID
}
Sample Program

This example shows a Book type with different scalar fields for common data.

GraphQL
type Book {
  title: String
  pages: Int
  rating: Float
  isPublished: Boolean
  bookId: ID
}
OutputSuccess
Important Notes

Scalar types cannot contain other fields or objects.

Use ID when you want a unique identifier, but it is treated like a string.

Boolean values are only true or false, no other options.

Summary

Scalar types represent simple data values in GraphQL.

Common scalar types are String, Int, Float, Boolean, and ID.

They help define the shape of data you can query or send.