Open In App

Types in GraphQL

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

GraphQL is a strongly typed query language used as a manipulative language for various APIs. It is also called a query language for APIs. GraphQL also helps to describe our data. GraphQL services can run in any language In this, Types are the fundamental concepts that define various data types present in GraphQL and It is also used to define the schema using different data types. There are multiple types in GraphQL.

Types in GraphQL

An application that possesses GraphQL service allows users to make queries by defining a set of types that describes the set of all possible data needed. So, when a query request comes in, these are validated and executed against the schema defined. The different types of Types in GraphQL are as follows

  1. Scalar Types
  2. Object Types
  3. Query Types
  4. Not Null Types
  5. Enumeration Types
  6. List Types
  7. Union Types
gpl-min-(1)-min

Types in GraphQL – GeeksForGeeks

1. Scalar Types

It consist of all the primitive data types. Primitive data types are the basic building blocks for other GraphQL Types and they can store only a single value. GraphQL object type have a specific name and fields and these fields are used to resolve concrete data and they represent the leaves of the query.

Some Scalar types in GraphQL

Int - It is a 32-bit signed integer or number
Float - It is a floating point value i.e. signed double precision
String - It is a sequence of characters
Boolean - It can be either True or False
ID - It is an unique identifier that is used to fetch an object

Syntax:

Here it is a pair of name-field. “field_name” is a name and “data_type” is a field corresponds to that name.

field_name: data_type

Example:

type Person {
id: ID!
name: String!
age: Int!
}

2. Object Types

It is the basic and most used Type in GraphQL. It represents a group of fields in which each field has a specific name and a value corresponding to it i.e. These Object Types are that we can fetch from our service and also the values-fields that it contains. It includes Scalar Types and other Types.

Syntax:

type Object_name
{
field_name1: data_type
field_name2: data_type
....
....
field_nameN: data_type
}

Example:

type Student {
id: ID!
name: String!
hobbies: [Hobby!]!
}

type Hobby {
id: ID!
title: String!
}

3. Query Types

Query in GraphQL is used to fetch data from APIs. A query in GraphQL schema may or may not contain a “mutation” type field. Query Types are same as Object Types and these query Types defines the entry point for every query. In Query Type, a request is made from the client side to the GraphQL server.

Syntax:

type Query {
field_name1: data_type
field_name2: data_type
field_name2(parameter1:data_type, parameter2: data_type,......parameterN:data_type): data_type
}

Example:

type Query  {
about: String
}

4. Not Null Types

It is used to define that a particular field is required i.e. it cannot be null. There must be some value that the field should have. It can be represented by exclamation mark (!). Add this exclamation mark (!), at the end of the type to make it Not Null.

Syntax:

field_name: data_type!

Example:

type Student {
name: String!
age: Int!
}

5. Enumeration Types

Enumeration Types are also called as “Enum” Types and it is used when there is only one predefined set of values for a field. It is restricted to a particular set of allowed values. It is similar to Scalar Types and Enum allows only if the conditions below are satisfied.

  • If a argument is one of the allowed value then it can validate
  • A field will be a finite set of values, when it communicates through the type system

Synatx:

type ENUM_name{
value1
value2
value3
}

Example:

type Relationship {
SINGLE
COMMITTED
ENGAGED
}

6. List Types

List Types are used to represent an ordered list or an array of values of specific type i.e. scalar types, enumeration types, object types, etc. It is represented by using “type modifier []“. Various other Types are enclosed within the type modifier [].

Syntax:

field_name: [data_type]

Example:

type CricketTeam {
title: String!
members: [Person!]!
}

7. Union Types

Union Types in GraphQL is used to return union of multiple object types i.e. It allows the schema to return one of the multiple object. Union Types can not define shared fields among various Types. It is represented by using Union Sign ” | “.

Syntax:

union Object_name = object_1 | Object_2 | Object_3

Example:

union Entertainment = Song | WebSeries

type Query {
allEntertainment: [Entertainment] # This list can include both Song and WebSeries objects
}

Conclusion

Types in GraphQL are also used to define a schema and we can use that schema in queries to fetch data from the database through APIs, whenever user wants. There are some of the main abstract types in GraphQL. Checkout, if you want to know more about GraphQL


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads