0
0
GraphQLquery~20 mins

One-to-many relationships in GraphQL - Practice Problems & Coding Challenges

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

Given a GraphQL schema where Author has many Books, what will be the result of this query?

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

type Book {
  id: ID!
  title: String!
  authorId: ID!
}

# Sample data:
# authors = [{id: "1", name: "Alice"}, {id: "2", name: "Bob"}]
# books = [{id: "a", title: "GraphQL Basics", authorId: "1"}, {id: "b", title: "Advanced GraphQL", authorId: "1"}, {id: "c", title: "Intro to Databases", authorId: "2"}]
A[{"name": "Alice", "books": [{"title": "GraphQL Basics"}, {"title": "Advanced GraphQL"}]}, {"name": "Bob", "books": [{"title": "Intro to Databases"}]}]
B[{"name": "Alice", "books": [{"title": "Intro to Databases"}]}, {"name": "Bob", "books": [{"title": "GraphQL Basics"}, {"title": "Advanced GraphQL"}]}]
C[{"name": "Alice", "books": []}, {"name": "Bob", "books": []}]
D[{"name": "Alice"}, {"name": "Bob"}]
Attempts:
2 left
💡 Hint

Remember that books are linked to authors by authorId.

🧠 Conceptual
intermediate
1:30remaining
Understanding one-to-many relationships in GraphQL schema

Which statement correctly describes a one-to-many relationship in a GraphQL schema?

AA type has no fields referencing other types.
BA type has a field that returns a single scalar value only.
CA type has a field that returns a union of unrelated types.
DA type has a field that returns a list of another type, representing multiple related items.
Attempts:
2 left
💡 Hint

Think about how multiple related items are represented.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL schema for one-to-many

Which option contains a syntax error in defining a one-to-many relationship?

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

type Book {
  id: ID!
  title: String!
  authorId: ID!
}
AThe field 'authorId' should be a list type '[ID!]' instead of 'ID!'.
BThe field 'books' should be a list type '[Book!]' instead of 'Book!'.
CThe type 'Book' must have a field 'author' of type 'Author!'.
DThe field 'name' should be nullable 'String' instead of 'String!'.
Attempts:
2 left
💡 Hint

One-to-many means one author has many books.

optimization
advanced
2:30remaining
Optimizing nested queries in one-to-many GraphQL relationships

Given a one-to-many relationship between Author and Books, which approach best reduces over-fetching when querying authors with their books?

ARequest only the fields needed from both authors and books in the query selection set.
BAlways request all fields from authors and books to avoid missing data.
CUse multiple separate queries for authors and books instead of nested queries.
DRequest only authors and ignore books to reduce data size.
Attempts:
2 left
💡 Hint

GraphQL allows selecting exactly what you want.

🔧 Debug
expert
3:00remaining
Debugging a missing nested list in GraphQL one-to-many query

Why does this query return authors without their books?

query {
  authors {
    name
    books {
      title
    }
  }
}

Schema snippet:

type Author {
  id: ID!
  name: String!
  books: Book
}
AThe 'name' field is not requested correctly.
BThe query is missing an argument to filter books by author.
CThe 'books' field is not a list type; it should be '[Book!]!' to return multiple books.
DThe schema is missing the 'Book' type definition.
Attempts:
2 left
💡 Hint

Check the type of the 'books' field in the schema.