Use the Node Interface in Queries
The aim of passing the node interface in queries is to retrieve the service data of the field. It’s helpful for developers to know about the actual data of the requested node interface.
In the above example, we just got to know what is the actual type and kind of the interface field. If we want to retrieve the data of the particular student like name, age etc., then it will be done by implementing the node with object type.
Syntax:
interface Node_Name {
field_name : field_type
}
type type_name implements Node_Name {
field_name : field_to_be_displayed
}
Example: Display the particular student information
Let’s create the node interface with the global field to be passed on the query. Now, implement the node interface with the type Student and specified fields to be displayed in the response.
Query:
interface Node {
roll_number : ID !
}
type Student implements Node {
name : name !
age : age !
}
Now, if we want to retrieve the particular student data then pass the roll_number of that student to get the response.
Pass it in the below format query. studentNode is user defined query.
{
studentNode : Node (roll_number : "14CSE")
{
roll_number
... on Student {
name
age
}
}
}
If we execute the above query then you will get the response of the particular student in the below format.
Response:
{
"studentNode" :
{
"roll_number" : "14CSE",
"name" : "John Alan",
"age" : "15",
}
}
Explanation: If you execute the above query then you will get the response of the particular student in the below format.
Global Object Identification in GraphQL
Global Object Identification in GraphQL is a crucial concept that enables precise data retrieval from APIs by using unique identifiers. It enhances the efficiency of GraphQL schemas by allowing developers to fetch specific data easily.
By defining a node interface and implementing it in object types, developers can effectively retrieve desired information, making GraphQL a powerful tool for querying and manipulating data. In this article, we will learn about Global Object Identification in GraphQL by understanding their needs, Implementation in depth with the help of examples, and so on.