The Node interface pattern helps us fetch any object by its unique ID in a simple way.
Node interface pattern in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
type Post implements Node {
id: ID!
title: String!
}
# Query to fetch any Node by id
type Query {
node(id: ID!): Node
}The Node interface requires an id field that is unique.
Types like User and Post implement Node to share this ID field.
Examples
GraphQL
interface Node {
id: ID!
}GraphQL
type User implements Node {
id: ID!
name: String!
}GraphQL
type Query {
node(id: ID!): Node
}Sample Program
This example shows the Node interface with User and Post types. The query fetches a node by ID and uses fragments to get fields based on the actual type.
GraphQL
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
type Post implements Node {
id: ID!
title: String!
}
type Query {
node(id: ID!): Node
}
# Sample query
# {
# node(id: "1") {
# id
# ... on User {
# name
# }
# ... on Post {
# title
# }
# }
# }
# Sample response for node with id "1" being a User
# {
# "data": {
# "node": {
# "id": "1",
# "name": "Alice"
# }
# }
# }Important Notes
Use fragments (... on TypeName) to get fields specific to each type.
The id field must be unique across all types implementing Node.
This pattern helps clients fetch any object with one query, simplifying API design.
Summary
The Node interface defines a unique ID for all types.
It allows fetching any object by ID using one query.
Use fragments to get type-specific fields when querying a Node.
Practice
1. What is the main purpose of the
Node interface in GraphQL?easy
Solution
Step 1: Understand the Node interface role
The Node interface is designed to give every object type a unique identifier.Step 2: Identify its main use
This unique ID allows fetching any object by ID in a single query.Final Answer:
To provide a unique ID for all object types -> Option AQuick Check:
Node interface = unique ID for all types [OK]
Hint: Node interface always provides unique IDs for all types [OK]
Common Mistakes:
- Thinking Node defines mutations
- Believing Node restricts queries to one type
- Assuming Node auto-generates database schemas
2. Which of the following is the correct way to declare the
Node interface in GraphQL SDL?easy
Solution
Step 1: Recall the Node interface ID type
The Node interface requires anidfield of typeID!, which is a non-null unique identifier.Step 2: Check each option's ID type
interface Node { id: ID! } usesID!, which is correct. Others use wrong types like String, Int, or Boolean.Final Answer:
interface Node { id: ID! } -> Option DQuick Check:
ID field in Node = ID! type [OK]
Hint: Node interface ID must be non-null ID type [OK]
Common Mistakes:
- Using String or Int instead of ID type
- Making ID nullable (missing !)
- Using Boolean as ID type
3. Given this query using the Node interface:
What fields will be returned if the node with ID "123" is a Post?
query { node(id: "123") { id ... on User { name } ... on Post { title } } }What fields will be returned if the node with ID "123" is a Post?
medium
Solution
Step 1: Understand the query structure
The query fetches a node by ID and requests theidfield plus fragments forUserandPosttypes.Step 2: Determine the node type and returned fields
If the node is aPost, thetitlefield from thePostfragment is returned along withid. TheUserfragment is ignored.Final Answer:
{ "id": "123", "title": "GraphQL Basics" } -> Option BQuick Check:
Node query returns fields for actual type fragment [OK]
Hint: Fragments return fields only for matching node type [OK]
Common Mistakes:
- Expecting fields from non-matching fragments
- Ignoring the id field
- Assuming query causes error
4. You wrote this schema snippet:
Why will this schema cause an error?
interface Node { id: ID! }
type User implements Node { id: ID name: String }Why will this schema cause an error?
medium
Solution
Step 1: Compare interface and type field definitions
The Node interface requiresidasID!(non-null). The User type declaresidasID(nullable).Step 2: Understand GraphQL type compatibility rules
Implementing types must match or be more strict. Here, User'sidis less strict (nullable), causing an error.Final Answer:
User's id field must be non-null (ID!) to match Node interface -> Option AQuick Check:
Implementing type fields must match interface exactly [OK]
Hint: Implementing type fields must be equal or stricter than interface [OK]
Common Mistakes:
- Thinking User can't implement Node
- Changing id type to String
- Removing extra fields like name
5. You want to fetch a list of mixed objects (Users and Posts) by their IDs using the Node interface. Which approach correctly fetches their specific fields in one query?
hard
Solution
Step 1: Understand Node interface usage for mixed types
The Node interface allows fetching any object by ID in one query, returning a list of Nodes.Step 2: Use inline fragments to get type-specific fields
To get fields specific to Users and Posts, use inline fragments... on Userand... on Postinside the query.Final Answer:
Use anodes(ids: [ID!]!)query returning [Node], then use inline fragments for User and Post fields -> Option CQuick Check:
Node interface + fragments fetch mixed types in one query [OK]
Hint: Use nodes query with fragments for mixed type fields [OK]
Common Mistakes:
- Querying types separately instead of one query
- Using union instead of interface for this pattern
- Fetching only id without needed fields
