What Are Scalar Types In GraphQL?
Tags: graphql, todayilearned, discuss, beginners
Published: 2021-07-12 18:53:02

_originally posted on [StepZen](https://stepzen.com/blog/what-are-scalar-types-in-graphql)_

## Introduction

You see scalar types everywhere you look in GraphQL. Take this type definition for example:

```graphql
type Tracking {
  origin: String!
  destination: String
  weight_lbs: Int
}
```

Here, `String` and `Int` are two of GraphQL's built-in scalar types. We'll talk about what that means and how you can add your own scalars to your GraphQL schemas.

## Definition

Here's how [GraphQL.org defines scalar types](https://graphql.org/learn/schema/#scalar-types):

> A GraphQL object type has a name and fields, but at some point those fields have to resolve to some concrete data. That's where the scalar types come in: they represent the leaves of the query.

So, assuming that we are familiar with [GraphQL queries](https://graphql.org/learn/schema/#the-query-and-mutation-types), what are these leaves? In Graph Theory, we can structure our query in terms of a graph. Let's look at a graph for the query we defined above:

![query screenshot](https://codimd.s3.shivering-isles.com/demo/uploads/upload_b7699e426ff12abe5ddfd00ed033f938.png)

The nodes of the `Breed` query tree are its leaves. That is, they are nodes with vertex degree 1. [Vertex degrees](https://www.varsitytutors.com/hotmath/hotmath_help/topics/degree-of-a-vertex) are the number of edges (in this illustration, the arrows) connected to a vertex. These leaves will resolve to a scalar type, which can be one of GraphQL's built in scalar types or a custom type defined in the schema.

## GraphQL's Scalar Types

GraphQL has its own [set of scalar types](https://graphql.org/learn/schema/) that you don't have to initiate.

* **Int**: A signed 32‐bit integer.
* **Float**: A signed double-precision floating-point value.
* **String**: A UTF‐8 character sequence.
* **Boolean**: true or false.
* **ID**: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

But what if you want to use a scalar that's not on the list, like `DateTime`, you can [create your own scalar type](https://graphql.org/learn/schema/#scalar-types)-- or, just use one of StepZen's if it fits your needs.

## StepZen's Scalar Types

StepZen 'builds in' `Date`, `DateTime` & `JSON` scalar types so you can use them without having to implement your own (goodbye, extensive resolver logic!).

So, using a `DateTime` scalar in a StepZen GraphQL API is as simple as adding it to your schema:

```graphql
type Tracking {
  origin: String!
  destination: String
  weight_lbs: Int
  dateDelivered: DateTime
}
```

So a query like:

```graphql
query MyQuery {
  deliveryByOrigin(origin: "London") {
    destination
    origin
    weight_lbs
    dateDelivered
  }
```

returns your data for `dateDelivered`:

```json
"data": {
    "deliveryByOrigin": {
      "destination": "Cairo",
      "origin": "London",
      "weight_lbs": 12,
      "dateDelivered": "02-10-2020"
    }
```


## Conclusion

If you'd like to learn more about GraphQL and/or StepZen, head on over to [our blog](https://stepzen.com/blog) for a deep dive, or take a [look at our docs](https://stepzen.com/docs).