Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Relay Specification Compliance with GraphQL
📖 Scenario: You are building a simple GraphQL API for a blog. The API must follow the Relay specification to support pagination and global object identification.
🎯 Goal: Create a GraphQL schema that implements Relay-compliant connections for a list of blog posts, including nodes, edges, and pageInfo fields.
📋 What You'll Learn
Create a Post type with id and title fields
Implement a PostConnection type with edges and pageInfo
Use cursor and node in edges
Add a posts query that returns a PostConnection
Include first and after arguments for pagination
💡 Why This Matters
🌍 Real World
Relay-compliant GraphQL APIs are used in modern web apps to efficiently fetch and paginate data.
💼 Career
Understanding Relay specification is important for frontend and backend developers working with GraphQL in production.
Progress0 / 4 steps
1
Define the Post type with id and title
Create a GraphQL type called Post with two fields: id of type ID! and title of type String!.
GraphQL
Hint
Use type Post { id: ID! title: String! } to define the Post type.
2
Create the PostEdge and PostConnection types
Add a PostEdge type with cursor of type String! and node of type Post. Then create a PostConnection type with edges as a list of PostEdge and pageInfo of type PageInfo!.
GraphQL
Hint
Define PostEdge with cursor and node, then PostConnection with edges and pageInfo.
3
Add the PageInfo type for pagination info
Create a PageInfo type with four fields: hasNextPage and hasPreviousPage of type Boolean!, and startCursor and endCursor of type String.
GraphQL
Hint
PageInfo holds pagination booleans and cursors.
4
Add the posts query with first and after arguments
Add a Query type with a posts field that returns a PostConnection. The posts field should accept two arguments: first of type Int and after of type String.
GraphQL
Hint
The Query type defines the entry point for fetching posts with pagination arguments.
Practice
(1/5)
1. What is the main purpose of the edges field in a Relay-compliant GraphQL connection?
easy
A. To store metadata about the entire list
B. To hold the list of items along with their cursors for pagination
C. To define the total count of items in the list
D. To specify the GraphQL schema version
Solution
Step 1: Understand Relay connection structure
Relay connections use edges to represent each item with its cursor for pagination.
Step 2: Identify the role of edges
The edges field contains nodes (items) and cursors, enabling smooth pagination.
Final Answer:
To hold the list of items along with their cursors for pagination -> Option B
Quick Check:
edges = items + cursors [OK]
Hint: Edges always pair items with cursors for pagination [OK]
Common Mistakes:
Confusing edges with pageInfo
Thinking edges store only items without cursors
Mixing edges with totalCount field
2. Which of the following is the correct syntax to request the first 5 items in a Relay connection named users?
easy
A. { users(first: 5) { edges { node { id } } } }
B. { users(limit: 5) { edges { node { id } } } }
C. { users(count: 5) { edges { node { id } } } }
D. { users(take: 5) { edges { node { id } } } }
Solution
Step 1: Recall Relay pagination argument
Relay uses first to specify how many items to fetch from the start.
Step 2: Check syntax correctness
Only first: 5 is valid; limit, count, and take are not Relay standard arguments.
Final Answer:
{ users(first: 5) { edges { node { id } } } } -> Option A
Quick Check:
Use first for Relay pagination [OK]
Hint: Use 'first' to fetch initial items in Relay queries [OK]
Common Mistakes:
Using non-Relay arguments like limit or count
Omitting edges or node fields
Confusing Relay with REST query parameters
3. Given this GraphQL query on a Relay connection:
D. There are more posts available after the current 2
Solution
Step 1: Understand pageInfo.hasNextPage
This field tells if more items exist beyond the current page.
Step 2: Interpret the returned value
The value true means more posts exist after the fetched two.
Final Answer:
There are more posts available after the current 2 -> Option D
Quick Check:
hasNextPage = true means more data [OK]
Hint: True hasNextPage means more items exist [OK]
Common Mistakes:
Assuming true means no more data
Confusing hasNextPage with hasPreviousPage
Ignoring pageInfo in Relay connections
4. You wrote this Relay connection query:
{ comments(last: 3) { edges { node { text } } } }
But the server returns an error:
"Field 'last' is not supported on this connection"
What is the likely cause?
medium
A. The connection does not support backward pagination with 'last'
B. The 'last' argument must be replaced with 'first'
C. The query is missing the 'before' cursor argument
D. The 'edges' field is misspelled
Solution
Step 1: Understand Relay pagination directions
Relay supports forward pagination with first and backward with last.
Step 2: Identify server limitation
Some connections only support forward pagination; thus, last is unsupported.
Final Answer:
The connection does not support backward pagination with 'last' -> Option A
Quick Check:
Unsupported 'last' means no backward pagination [OK]
Hint: Check if connection supports 'last' for backward pagination [OK]
Common Mistakes:
Replacing 'last' with 'first' without cursor
Assuming 'edges' spelling causes error
Ignoring need for 'before' cursor with 'last'
5. You want to fetch a paginated list of products using Relay spec. You need to get the first 3 products, then fetch the next 3 after the last cursor. Which sequence of queries correctly follows Relay pagination?