Open In App

Fields in GraphQL

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

Fields in GraphQL are the fundamental components that allow clients to specify the data they need from a server. They represent individual pieces of data that can be requested for an object. Understanding how fields work is essential for building efficient GraphQL queries and designing effective schemas.

In this article, We will learn about what are Fields, their different types with examples, and so on.

What are Fields in GraphQL?

  • In GraphQL, fields are the fundamental units of data that can be requested for an object.
  • They represent specific attributes or properties of an object, such as its ID, name, or any other relevant data.
  • Fields are the building blocks of GraphQL queries and are used to specify the exact data that clients want to retrieve from the server.
  • Fields have different types in GraphQL. Some of them are explained below:
  1. Scalar fields
  2. List Fields
  3. Object Fields
  4. Custom Scalar Fields
  5. Non-Null Fields
  6. Interface Fields

1. Scalar Fields

  • Scalar fields in GraphQL represent primitive data types, such as integers, floats, booleans, and strings.
  • Scalar fields hold atomic values and they cannot have subfields.
  • Examples of Scalar fields in GraphQL are shown below:
type Student{
id: ID!
name: String!
email: String
}

Explanation: In this, example the name and email are scalar fields of type String. The exclamation mark(!) represent that the name field is required and should always be there, whereas the email field is optional.

2. List Fields

  • List Fields has arrays or lists of data of a specific type.
  • They are denoted by square brackets followed by the type definition.
  • List fields allow returning multiple instances of a particular field.
  • Examples of List Fields in GraphQL are shown below:
type Article{
id: ID!
title: String!
tags: [Strings!]!
}

Explanation: In this, example the `tag` are list fields of type String. The exclamation mark(`!`) represent that the `tag` field is required and should always be there, the other fields present like `id` and `title` are also required in this example, but user can use customize all the fields as per the requirement of the application.

3. Object Fields

  • Object Field are a bit complex as they are used to represent data types which have their own set of fields.
  • In Object fields you can traverse related data structure. Nesting is also allowed in Object Fields.
  • You can understand Object fields better by understanding the below example.
type Article{
id: ID!
title: String!
author: Author!
}
type Author{
id:ID!
name: String!
email: String!
}

Explanation:

  • In this, example the above there are two objects Article and Author.
  • The object Article, has three fields: `id`,`title`,`author`.
  • The `id` field is of type `ID` and the `title` field is of type `String` both the fields are marked with exclamation mark representing that they are required field.
  • The third field of the object Article is of Object type, it is also a required and should not be null value. The `article` field represent a relationship to another object.
  • The `author` object has three fields: `id`, `name`,`email`.
  • Similar to the `Article` object the id` field is of type `ID` and the `name` field is of type `String` both the fields are marked with exclamation mark representing that they are required field.
  • The `email` field of the `Author` object is marked required.
  • As there is an relationship between the `Article` object and `Author` object, hence this schema allows the clients to query for articles and retrieve information about their author in a structured manner.

4. Custom Scalar Fields

  • Custom scalar fields represents custom-defined scalar types that are not available in GraphQL.
  • Developers can define custom scalar types which can be used to represent specialized data format,such as dates.
  • Examples of Custom Scalar Fields in GraphQL are shown below:
scalar Date
type Article {
id: ID!
title: String!
datepublished: Date!
}

Explanation:

  • In this above example, a custom scalar type called `Date`.
  • The `scalar` keyword is used to declare a custom scalar type in GraphQL.
  • The `Date` scalar type represents date in specific format, which can either be a string or a custom serialized format.
  • The object Article, has three fields: `id`,`title`,`datepublished`.
  • The `id` field is of type `ID` and the `title` field is of type `String` both the fields are marked with exclamation mark representing that they are required field.
  • The `datepublished` field is of type `Date`, indicating it’s required field with a non -null value and it should be of custom scalar type `Date`. Using a custom scalar type developers can ensure that the `datepublished` is of the format that meet the requirement of the application.

5. NonNull Fields

  • Non-null fields represent fields that must always have a non-null value. They are denoted by an exclamation mark (!) after the type defination of the field.
  • They ensure that the data returned by the object for this field should always have a value.
  • If any user try to attempt to query a non-null field without providing a value for it, GraphQL will return an error which would indicate that a field is missing.
  • Examples of NonNull Fields in GraphQL are shown below:
type Author{
id:ID!
name: String!
email: String!
}

Explanation: In the above example, the fields of the object `Author` are all marked with an exclamation mark (!) which means all the three fields , `id`,`name`,`email` are required for the object Author so that the above Schema should be error free.

6. Interface Fields

  • Interface fields define a common set of fields that multiple types can implement. This allows for polymorphic queries, where a single query can fetch fields that are in common with multiple types that implement the same interface.
  • In GraphQL, interfaces are abstract types that define a set of fields. Interfaces are declared using the `interface` keyword followed by the interface name and the list of fields.
  • Examples of Interface Fields in GraphQL are shown below:
interface Animal {
id: ID!
name: String!
}
type Cat implements Animal {
id: ID!
name: String!
breed: String!
}
type Dog implements Animal {
id: ID!
name: String!
color: String!
}

Explanation:

  • In this example, we define an interface called `Animal` which has fields `id` and `name`.
  • The two objects types `Dog` and `Cat`, both implement the `Animal` interface.
  • Each type `Dog`and `Cat` include the fields required by the `Animal` interface (`id` and `name`) as well as additional fields that are specific to that object. For eg: `breed` for `Dog` and `color` for `Cat`.

Conclusion

Overall, fields play a important role in defining the structure of GraphQL queries and schemas. They allow clients to request specific data, reducing over-fetching and under-fetching. By utilizing different types of fields like scalar fields, list fields, object fields, custom scalar fields, non-null fields, and interface fields, developers can create flexible and powerful APIs that meet the exact data requirements of their applications. Understanding the different types of fields and how they are used is key to leveraging the full potential of GraphQL.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads