Open In App

Object Types and Fields in GraphQL Schema

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

GraphQL is a query language designed to provide a more efficient and flexible alternative to traditional REST APIs. One of its key features is the ability to define Object Types and Fields, which allow developers to structure the data they want to fetch from a server.

Understanding how to create Object Types and Fields is fundamental to building GraphQL schemas and querying data effectively. In this article, We will understand what is object types, fields, and how to create them in GraphQL schema with the help of examples and so on.

What is Object Type in GraphQL?

  • The main purpose of using the object type is to create an object that will fetch a set of data from the service. In other words, the main purpose of the object type is to create the structure for returning a certain set of data from a service or API.
  • The returned data can be of any data type like String, Int, boolean, or other object type as well.
  • The keyword “type” is used to define object type in GraphQL.
  • Service consists of a huge amount of data and in this case, if we just want to retrieve some set of data that too with one GraphQL call then this object type is very helpful in this case.

For example, the Service consists of around 20 data items about students. If a teacher wants to fetch the data related to a certain data item then we can create an object type and mention the required data items to be returned from the service.

What is Fields in GraphQL?

Fields are associated with object type. Fields are nothing but the data item to be returned from the service. In object type, we need to mention the field name and the data type of the field.

In Graphql, we have 5 types of data type for fields which are used to define the fields as follows:

  • String: It is used for text or character data.
  • Boolean: It is a true or false value.
  • Int: It is used to return numbers.
  • Float: It is used to return a number with a decimal.
  • ID: It is to return a unique identifier which is also a string. It returns the unique ID of an object.

How to Create Object Type and Fields in GraphQL?

It is simple to create to object type and fields to retrieve the data from service. For this, we need to use a keyword called type which is used to declare the Object Type. Graphql will recognise the object type with help of the type keyword. Below is the syntax for creating object type and fields.

Syntax:

type Object_Type_Name {
field_1 : data_type
field_2 : data_type
.
.
field_n : data
}

Explanation:

  • Here, the type is a keyword to declare the object type or object.
  • Object_Type_Name is a user defined object name.
  • field_1 …. field_n are fields or data item names to be returned from service.
  • data_type is nothing but the data type of the field.

Characteristics of data type:

  1. data_type ! defines that the returned type shouldn’t be null.
  2. [object_type] defines that the returned object will be assigned in the form of list to the field.
  3. String, Int, Boolean, Float and ID are Scalar data type.
  4. If we pass Object type as field then it is called as dynamic data type.

Example 1: Student Object in GraphQL Schema

Below is the example of Student Object which consists the field names of name and roll_number. String data type is the data type of name and roll_number. If we execute this Graphql schema, then the Graphql will only fetch name and roll_number from service for Student Object.

type Student {
name : String
roll_number : String
}

Example 2: Student Object with Non-nullable Fields in GraphQL Schema

Below is the example of Student Object which consists the field names of name, roll_number and address. The name and roll_number shouldn’t be empty or null. In that case, we can use ! to achieve this requirement.

type Student {
name : String !
roll_number : String !
address: String }

The above name and roll_number fields are nonnullable. If our service or API tries or attempts to return null for a nonnullable field, an error will be shown.

Example 3: Returning Data Between Object Types in GraphQL Schema

Below is the example is to return data from one object type to another object type. The Department will contain the data of course object type.

 // The below object type is to return CSE courses
type Course {
java: String
python: String
javascript: String
}
// The below object type is to return CSE department name and Course object type will be returned for subjects field in list format
type Department {
name : String
subjects : [ Course ]
}

Conclusion

In conclusion, Object Types and Fields are core concepts in GraphQL that enable developers to define the structure of their data models and query specific information from a server. By mastering these concepts, developers can build more efficient and maintainable GraphQL APIs that meet the needs of their applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads