Bird
Raised Fist0
GraphQLquery~20 mins

Schema definition in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of the schema block in a GraphQL schema definition?
easy
A. It specifies the database connection details.
B. It defines the main entry points like query and mutation types.
C. It creates new object types with fields.
D. It lists all the scalar types available.

Solution

  1. Step 1: Understand the role of the schema block

    The schema block in GraphQL defines the entry points for the API, such as which types are used for queries and mutations.
  2. Step 2: Differentiate from other schema parts

    Object types with fields are defined using type, not the schema block. Database connections and scalar types are not defined here.
  3. Final Answer:

    It defines the main entry points like query and mutation types. -> Option B
  4. Quick Check:

    Schema block = entry points (query, mutation) [OK]
Hint: Remember schema block sets query and mutation roots [OK]
Common Mistakes:
  • Confusing schema block with type definitions
  • Thinking schema block defines database details
  • Assuming schema block lists scalar types
2. Which of the following is the correct syntax to define a simple object type Book with a title field of type String in GraphQL?
easy
A. schema Book { title: String }
B. object Book { title: String }
C. type Book { title: String }
D. define Book { title: String }

Solution

  1. Step 1: Recall GraphQL syntax for object types

    GraphQL uses the keyword type to define object types, followed by the type name and fields inside curly braces.
  2. Step 2: Check each option

    Only type Book { title: String } uses the correct keyword type and proper syntax. Others use invalid keywords like object, schema, or define.
  3. Final Answer:

    type Book { title: String } -> Option C
  4. Quick Check:

    Object type syntax = type + name + fields [OK]
Hint: Use 'type' keyword to define objects in GraphQL [OK]
Common Mistakes:
  • Using 'object' or 'define' instead of 'type'
  • Confusing schema block with type definition
  • Missing curly braces around fields
3. Given the schema definition:
type Query {
  book: Book
}
type Book {
  title: String
  pages: Int
}

What fields can be queried under book?
medium
A. Only title
B. No fields, book is scalar
C. Only pages
D. title and pages

Solution

  1. Step 1: Identify the type of book field in Query

    The book field in Query returns a Book object type.
  2. Step 2: Check fields inside Book type

    The Book type has two fields: title (String) and pages (Int), so both can be queried.
  3. Final Answer:

    title and pages -> Option D
  4. Quick Check:

    Object fields = title + pages [OK]
Hint: Object fields are all listed inside its type definition [OK]
Common Mistakes:
  • Assuming only one field is accessible
  • Thinking book is a scalar, not an object
  • Ignoring fields inside the Book type
4. Identify the error in this GraphQL schema snippet:
schema {
  query: Query
}
type Query {
  book: Book
}
type Book {
  title String
}
medium
A. Missing colon ':' after 'title' field name
B. Missing curly braces around Query type
C. Schema block should be named 'type Schema'
D. Book type cannot be used in Query

Solution

  1. Step 1: Check field syntax in Book type

    The field title String is missing a colon ':' between the field name and type. It should be title: String.
  2. Step 2: Verify other parts of the schema

    The schema block and Query type are correctly defined with proper braces and names.
  3. Final Answer:

    Missing colon ':' after 'title' field name -> Option A
  4. Quick Check:

    Field syntax requires colon ':' [OK]
Hint: Always put ':' between field name and type [OK]
Common Mistakes:
  • Omitting colon ':' after field names
  • Confusing schema block syntax
  • Misnaming schema block as a type
5. You want to add a mutation to create a new Book with fields title (String) and pages (Int). Which schema definition correctly adds this mutation?
hard
A. type Mutation { createBook(title: String, pages: Int): Book } schema { mutation: Mutation }
B. schema { mutation: Mutation } type Mutation { createBook(title String, pages Int): Book }
C. type Mutation { createBook(title: String, pages: Int): Book } schema { query: Query }
D. type Mutation { createBook(title: String, pages: Int): Book }

Solution

  1. Step 1: Define the Mutation type with correct field syntax

    The mutation field createBook must have arguments with colons, e.g., title: String, and return type Book. schema { mutation: Mutation } type Mutation { createBook(title String, pages Int): Book } has missing colons, so it's invalid.
  2. Step 2: Link the Mutation type in the schema block

    The schema block must include mutation: Mutation to expose the mutation. type Mutation { createBook(title: String, pages: Int): Book } schema { mutation: Mutation } correctly includes this after defining Mutation.
  3. Final Answer:

    type Mutation { createBook(title: String, pages: Int): Book } schema { mutation: Mutation } -> Option A
  4. Quick Check:

    Mutation type + schema mutation entry = correct [OK]
Hint: Mutation must be linked in schema block with correct syntax [OK]
Common Mistakes:
  • Omitting colons in argument definitions
  • Not linking mutation type in schema block
  • Only defining mutation type without schema entry
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard