Open In App

GraphQL | Scalars

Last Updated : 28 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Scalars are content of the GraphQL Type System. GraphQL scalars are the value types that every field in a GraphQL document ultimately resolves to. It is primitive data types that store single value. There are five built-in scalars in GraphQL but you can define custom made scalars too.

Built-in scalars: Scalar types represent primitive leaf values in a GraphQL type system. GraphQL responses take the form of a hierarchical tree; the leaves on these trees are GraphQL scalars.

  • int: It represent 32-bit signed whole-number numerical value. The server responding as Int should use this Int specification.
  • float: It represents double-precision signed fractional values. Te define this spec, IEEE 754 standard is used.
  • string: It represents text in UTF-8 encoding.
  • Boolean: This scalar is either true or false.
  • id: unique identifier used for the purposes of identification(acting as a key) of data. The ID is treated as a String for its serialization. Serialization is transforming an object to Boolean, Number or String here.

Example: Structure of GraphQL Scalar.

  • Query:
    {
      pokemon {
        name
        pokemonDetails
      }
    }
  • Output:
    {
     "data": {
       "pokemon": {
          "name":"Bulbasaur",
          "pokemonDetails": {
             "typePokemon":"land",
             "levelPokemon":3
           }
        }
      }
    }

The GraphQL scalars have 3 methods. The purpose of these methods is the identification and validation of the data before it is transferred between the client and the server.

  • Validation: The data passed between client and server is validated to be a specific type, so that the receiver can properly handle it. This validation should be placed on all the appropriate points where such validation is required. (<-validator media <-validator<-client)
  • Serialization: Before transferring the data, it is serialized to convert to a type appropriate for transmission. For example, a complex JSON object containing the details of a student is serialized to a String and then sent.
  • Parsing: When the data is received(serialized by the sender side), it is parsed to convert it into an appropriate form. For example, a Date object is serialized by the sender and sent to the receiver. The receiver then parses it to convert it back to the Date object.

methods arrow diagram

Custom Scalars: GraphQL gives developers the power to define custom scalars, appropriate to their own needs. We can declare a custom scalar like below and add it to the schema. To define your own scalar, validations, serializations, and parsers have to be written from scratch. There is also a great package called graphql-scalars ready for use that contains common scalars that might be required.

  • Syntax:
    scalar customScalar

Example: Now, let’s look at some examples of how these scalars play out. To demo, we will use the below schema.js file to perform this task.

  • schema.js:
    type Attack {
      name: String
      type: String
      damage: Int
    }
    
    type Pokemon {
      id: ID!
      number: String
      name: String
      weight: PokemonDimension
      height: PokemonDimension
      classification: String
      types: [String]
      resistant: [String]
      attacks: PokemonAttack
      weaknesses: [String]
      fleeRate: Float
      maxCP: Int
      evolutions: [Pokemon]
      evolutionRequirements: PokemonEvolutionRequirement
      maxHP: Int
      image: String
    }
    
    type PokemonAttack {
      fast: [Attack]
      special: [Attack]
    }
    
    type PokemonDimension {
      minimum: String
      maximum: String
    }
    
    type PokemonEvolutionRequirement {
      amount: Int
      name: String
    }
    
    type Query {
      query: Query
      pokemons(first: Int!): [Pokemon]
      pokemon(id: String, name: String): Pokemon
    }
  • Output:

Scalars features:

  • For the most part, GraphQL Scalars help in the basic functioning of the GraphQL engine. They do this by providing the five basic ‘value types’ for ‘fields’ that can be transferred between server and client. These basic types are: Int, Float, String, Boolean and ID.
  • For software that is a little more complex, GraphQL provides the programmer with the ability to define ‘custom scalar types’ to handle ‘GraphQL Operations’ better(in terms of efficiency, effectiveness, and security)
  • GraphQL in-built scalars also has an ‘id’ value type that can be used to uniquely label every data record.
  • GraphQL scalars are used in building schemas that act as a model for retrieving data from the database.

Reference: https://graphql-ruby.org/type_definitions/scalars.html


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads