Scalar Types in GraphQL

Scalar types are used to represent simple values. GraphQL query is built using GraphQL Shema. Scalar types are used as building blocks in defining the GraphQL schema. Scalar types are atomic as they hold a single value and it does not contain any subfields like collections or lists. The response of the GraphQL query is represented in a tree-like structure where the scalar type forms the leaf nodes in the tree.

Scalar Types in GraphQL Schema

GraphQL is a powerful open-source Query Language for APIs. In 2012 it was first developed by a team of developers in Facebook and then it was later made public to the general people by making it open-source. Now it is being maintained by the GraphQL community.

GraphQL is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. The benefit of using single endpoint queries is that they help the user in reducing fetching too much data or less amount of data than required. GraphQL datatypes are lists, objects, enums, interfaces, and primitive types which are also known as scalar types(built-in primitive types).

Let us see in this article, how the different scalar types can be used in the GraphQL schema.

Prerequisites:

Below are the prerequisites that we need to install before executing the GraphQL query on our laptop or desktop.

  • Nodejs
  • Npm
  • VsCode IDE

Similar Reads

Scalar Types in GraphQL

Scalar types are used to represent simple values. GraphQL query is built using GraphQL Shema. Scalar types are used as building blocks in defining the GraphQL schema. Scalar types are atomic as they hold a single value and it does not contain any subfields like collections or lists. The response of the GraphQL query is represented in a tree-like structure where the scalar type forms the leaf nodes in the tree....

Common Built-in Scalar Types

Int: Represents a 32-bit signed integer. For Example: 55 Float: Represents a floating-point value. For Example: 15.6 String: Represents a sequence of characters. For Example: “program” Boolean: Represents a true or false value. For Example: True or False ID: Represents a unique identifier, often used for fetching specific objects. For Example: userID=1001...

Using Built-in Scalar Types

In the below example, create a GraphQL schema to define basic information about a person....

Custom Scalar Type

...

Conclusion

...