0
0
GraphQLquery~20 mins

Bidirectional relationships in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bidirectional Relationships Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying a bidirectional relationship

Given a GraphQL schema where Author has a list of Book objects and each Book has a reference back to its Author, what will be the output of this query?

query {
  authors {
    name
    books {
      title
      author {
        name
      }
    }
  }
}
GraphQL
type Author {
  id: ID!
  name: String!
  books: [Book!]!
}

type Book {
  id: ID!
  title: String!
  author: Author!
}

# Sample data:
# Author: {id: "1", name: "Alice", books: [Book1, Book2]}
# Book1: {id: "101", title: "GraphQL Basics", author: Author}
# Book2: {id: "102", title: "Advanced GraphQL", author: Author}
A[{"name": "Alice", "books": [{"title": "GraphQL Basics", "author": {"name": "Alice"}}, {"title": "Advanced GraphQL", "author": {"name": "Alice"}}]}]
B[{"name": "Alice", "books": [{"title": "GraphQL Basics", "author": null}, {"title": "Advanced GraphQL", "author": null}]}]
C[{"name": "Alice", "books": [{"title": "GraphQL Basics"}, {"title": "Advanced GraphQL"}]}]
D[{"name": "Alice", "books": null}]
Attempts:
2 left
💡 Hint

Think about how the bidirectional link allows nested querying of author inside each book.

🧠 Conceptual
intermediate
1:30remaining
Understanding bidirectional relationships in GraphQL

Which statement best describes a bidirectional relationship in GraphQL schemas?

ATypes are unrelated and cannot reference each other in queries.
BA type references itself recursively without involving other types.
CTwo types reference each other, allowing queries to navigate from one to the other and back.
DA type has a list of scalar values only, without referencing other types.
Attempts:
2 left
💡 Hint

Think about how you can traverse data in both directions in a schema.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in bidirectional schema definition

Which option contains a syntax error in defining a bidirectional relationship between Author and Book?

GraphQL
type Author {
  id: ID!
  name: String!
  books: [Book!]!
}

type Book {
  id: ID!
  title: String!
  author: Author!
}
A
type Author {
  id: ID!
  name: String!
  books: [Book]
}

type Book {
  id: ID!
  title: String!
  author: Author!
}
B
type Author {
  id: ID!
  name: String!
  books: [Book!]!
}

type Book {
  id: ID!
  title: String!
  author: Author!
}
C
type Author {
  id: ID!
  name: String!
  books: [Book!]!
}

type Book {
  id: ID!
  title: String!
  author: Author
}
D
type Author {
  id: ID!
  name: String!
  books: Book!
}

type Book {
  id: ID!
  title: String!
  author: Author!
}
Attempts:
2 left
💡 Hint

Check the type of the books field in Author. Should it be a list or a single object?

optimization
advanced
2:00remaining
Optimizing nested queries in bidirectional relationships

You have a bidirectional relationship between Author and Book. You want to fetch authors and their books but avoid redundant nested author data inside each book to reduce query size. Which query modification achieves this?

ARemove the nested <code>author</code> field inside <code>books</code> in the query.
BAdd <code>@skip(if: true)</code> directive to the <code>author</code> field inside <code>books</code>.
CUse fragments to fetch <code>author</code> inside <code>books</code>.
DFetch only <code>books</code> without any fields.
Attempts:
2 left
💡 Hint

Think about how to avoid fetching nested data you don't need.

🔧 Debug
expert
3:00remaining
Debugging infinite recursion in bidirectional GraphQL query

Consider this GraphQL query on a bidirectional schema:

query {
  authors {
    name
    books {
      title
      author {
        books {
          title
        }
      }
    }
  }
}

What problem will this query cause when executed?

AIt will cause a syntax error due to nested fields.
BIt will cause infinite recursion leading to a server error or timeout.
CIt will return an empty list because of missing fields.
DIt will return only the first author and their books without errors.
Attempts:
2 left
💡 Hint

Think about how the query keeps nesting author and books inside each other.