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
}

Object Types and Fields in GraphQL Schema

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.

Similar Reads

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....

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....

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....

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....

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....

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....

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....