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 non–nullable. If our service or API tries or attempts to return null for a non–nullable field, an error will be shown.
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.