0
0
GraphQLquery~20 mins

Schema definition in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this GraphQL query?

Given this GraphQL schema:

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

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

type Query {
  book(id: ID!): Book
}

And this query:

{
  book(id: "1") {
    title
    author {
      name
    }
  }
}

Assuming the data has a book with id "1" titled "GraphQL Basics" by author "Alice", what is the expected output?

A{"data": {"book": {"title": "GraphQL Basics", "author": {"name": "Alice"}}}}
B{"data": {"book": {"title": "GraphQL Basics"}}}
C{"data": {"book": {"author": {"name": "Alice"}}}}
D{"errors": [{"message": "Field 'author' not found"}]}
Attempts:
2 left
💡 Hint

Remember the query asks for both title and author.name.

🧠 Conceptual
intermediate
1:30remaining
Which GraphQL type correctly defines a non-nullable list of nullable strings?

In GraphQL schema definition, how do you define a field that is a list which cannot be null, but can contain null strings?

AString!
B[String!]
C[String!]!
D[String]!
Attempts:
2 left
💡 Hint

Think about the placement of the exclamation mark ! and what it means for the list and its items.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this GraphQL schema snippet

What is wrong with this GraphQL type definition?

type User {
  id: ID!
  name: String
  email: String!
  friends: [User]
  age: Int!
}
AField 'age' cannot be non-nullable without a default value.
BMissing exclamation mark on 'friends' field to indicate non-null list.
CNo syntax error; this schema is valid.
DCannot have a field 'friends' of type [User] without specifying non-null on User.
Attempts:
2 left
💡 Hint

Check if all fields follow GraphQL type syntax rules.

optimization
advanced
2:30remaining
How to optimize a GraphQL schema for fetching nested data efficiently?

You have a schema with types Post and Comment. Each post has many comments. Which schema design helps avoid the N+1 query problem when fetching posts with comments?

ADefine <code>comments</code> field on <code>Post</code> as a list and resolve each comment individually in the resolver.
BUse a batch loader or data loader in the resolver to fetch all comments for multiple posts in one query.
CRemove the <code>comments</code> field and fetch comments separately after fetching posts.
DMake <code>comments</code> a scalar string field containing serialized comments.
Attempts:
2 left
💡 Hint

Think about how to reduce multiple database calls when resolving nested fields.

🔧 Debug
expert
3:00remaining
Why does this GraphQL schema cause a circular reference error?

Consider this schema snippet:

type Employee {
  id: ID!
  name: String!
  manager: Employee
  subordinates: [Employee!]
}

When trying to generate schema documentation, a circular reference error occurs. Why?

ABecause the schema lacks a terminating condition or depth limit for recursive references.
BBecause the <code>manager</code> field is nullable but <code>subordinates</code> list items are non-nullable, causing conflict.
CBecause <code>Employee</code> references itself directly without using an interface or union type.
DBecause GraphQL does not support recursive type definitions referencing themselves.
Attempts:
2 left
💡 Hint

Think about how recursive types can cause infinite loops in tools processing the schema.