0
0
GraphQLquery~20 mins

Code generation from schema 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 the schema below, what will be the result of the query?

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

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

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

Query:

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

Assuming the data has a book with id "1", title "GraphQL Basics", and author name "Alice".

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

Remember that nested objects must be returned as objects, not strings.

🧠 Conceptual
intermediate
1:30remaining
Which GraphQL schema type correctly represents a list of books?

You want to define a field books in your Query type that returns a list of Book objects. Which schema definition is correct?

Abooks: Book[]
Bbooks: List<Book>
Cbooks: [Book]
Dbooks: {Book}
Attempts:
2 left
💡 Hint

GraphQL uses square brackets to denote lists.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL schema snippet

Which option contains a syntax error in this GraphQL schema snippet?

type Query {
  user(id: ID!): User
}

type User {
  id: ID!
  name: String!
  email: String!
}
A
type User {
  id: ID!
  name: String!
  email String!
}
B
type User {
  id: ID!
  name: String!
  email: String!
}
C
type User {
  id: ID!
  name: String!
  email: String
}
D
type Query {
  user(id: ID!): User
}
Attempts:
2 left
💡 Hint

Check the syntax for field declarations in GraphQL schema.

optimization
advanced
2:30remaining
How to optimize a GraphQL query to avoid over-fetching?

You have a query fetching user with all fields including posts and each post's comments. You only need the user's name and the titles of their posts. Which query is optimized?

A{ user(id: "1") { name posts { title } } }
B{ user(id: "1") { id name email posts { id title content comments { id text } } } }
C{ user(id: "1") { posts { title } } }
D{ user(id: "1") { name posts { title comments { text } } } }
Attempts:
2 left
💡 Hint

Only request the fields you need to reduce data transfer.

🔧 Debug
expert
3:00remaining
Why does this GraphQL query return an error?

Given the schema:

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

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

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

Why does this query cause an error?

{
  book(id: "1") {
    title
    author
  }
}
ABecause the query is missing a mutation keyword
BBecause 'author' is an object type and must have subfields selected
CBecause 'title' is not a valid field of 'Book'
DBecause 'book' requires a list of IDs, not a single ID
Attempts:
2 left
💡 Hint

In GraphQL, object fields require specifying subfields.