Problem: Model a GraphQL Query Using a Nested Builder
Problem statement
You’re building a backend system that generates dynamic GraphQL queries for server-to-server communication. Developers need a way to programmatically construct deeply nested query strings using a clear, fluent API.
Your job is to implement a GraphQLQueryBuilder that allows developers to:
Start a root query.
Add fields to that query.
For any field, optionally nest a sub-selection using another builder.
Output a final query string that’s valid GraphQL syntax.
Goal
Build a fluent API using GraphQLQueryBuilder that supports:
.field(name: string): Adds a top-level field..select(field: string, subBuilderFn: (b: GraphQLQueryBuilder) => void): Adds a field with a nested selection set, defined using a newGraphQLQueryBuilderinstance..build(): Returns the full query string as a formatted GraphQL query, with proper indentation and nested braces.
{field1field2parent {child1child2}}
Constraints
Do not hardcode field names—support any valid field string passed by the user.
.select()must create a new builder instance for nested fields, preserving parent-child structure..build()must return a single formatted string—not an object, array, or intermediary structure.
Sample output
The examples below illustrate what the output should look like:
const query1 = new GraphQLQueryBuilder().field('id').field('title').select('author', (b) => {b.field('name').field('email');}).build();console.log(query1);/* Expected output:{idtitleauthor {name}}*/const query2 = new GraphQLQueryBuilder().select('user', (b) => {b.field('id').select('profile', (p) => {p.field('bio').field('avatar');});}).build();console.log(query2);/* Expected output:{user {idprofile {bioavatar}}}*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Model a GraphQL Query Using a Nested Builder
Problem statement
You’re building a backend system that generates dynamic GraphQL queries for server-to-server communication. Developers need a way to programmatically construct deeply nested query strings using a clear, fluent API.
Your job is to implement a GraphQLQueryBuilder that allows developers to:
Start a root query.
Add fields to that query.
For any field, optionally nest a sub-selection using another builder.
Output a final query string that’s valid GraphQL syntax.
Goal
Build a fluent API using GraphQLQueryBuilder that supports:
.field(name: string): Adds a top-level field..select(field: string, subBuilderFn: (b: GraphQLQueryBuilder) => void): Adds a field with a nested selection set, defined using a newGraphQLQueryBuilderinstance..build(): Returns the full query string as a formatted GraphQL query, with proper indentation and nested braces.
{field1field2parent {child1child2}}
Constraints
Do not hardcode field names—support any valid field string passed by the user.
.select()must create a new builder instance for nested fields, preserving parent-child structure..build()must return a single formatted string—not an object, array, or intermediary structure.
Sample output
The examples below illustrate what the output should look like:
const query1 = new GraphQLQueryBuilder().field('id').field('title').select('author', (b) => {b.field('name').field('email');}).build();console.log(query1);/* Expected output:{idtitleauthor {name}}*/const query2 = new GraphQLQueryBuilder().select('user', (b) => {b.field('id').select('profile', (p) => {p.field('bio').field('avatar');});}).build();console.log(query2);/* Expected output:{user {idprofile {bioavatar}}}*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.