0
0
GraphQLquery~20 mins

Why databases back GraphQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do GraphQL servers often rely on databases?

GraphQL servers need to provide data to clients. Why is it common for GraphQL servers to use databases as their data source?

ABecause databases automatically convert GraphQL queries into REST API calls.
BBecause GraphQL can only work with SQL databases and no other data sources.
CBecause GraphQL servers do not need any data source to respond to queries.
DBecause databases store structured data that GraphQL queries can retrieve efficiently.
Attempts:
2 left
💡 Hint

Think about where data is usually stored in applications.

query_result
intermediate
2:00remaining
What data does this GraphQL query return from the database?

Given a GraphQL query requesting user { id, name } and a database table users with columns id, name, and email, what will the query return?

GraphQL
query {
  user {
    id
    name
  }
}
A[{ "id": 1, "name": "Alice", "email": "alice@example.com" }]
B[{ "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" }]
C[{ "email": "alice@example.com" }]
D[]
Attempts:
2 left
💡 Hint

The query only asks for id and name, not email.

📝 Syntax
advanced
2:00remaining
Identify the error in this GraphQL resolver accessing a database

Look at this resolver code snippet that fetches data from a database. What is the syntax error?

GraphQL
const resolvers = {
  Query: {
    user: (parent, args, context) => {
      return context.db.query('SELECT * FROM users WHERE id = $1', args.id)
    }
  }
}
AThe function should be async to await the database query.
BThe resolver is missing a return statement.
CThe query method should use an array for parameters, like [args.id] instead of args.id.
DThe context object cannot be used inside resolvers.
Attempts:
2 left
💡 Hint

Check how parameters are passed to parameterized queries.

optimization
advanced
2:00remaining
How to optimize GraphQL queries backed by databases to reduce load?

When a GraphQL query requests nested data (e.g., users and their posts), what is a common optimization to reduce database load?

AUse data loader techniques to batch and cache database requests.
BRun separate database queries for each nested field without caching.
CFetch all data in one big query without filtering.
DAvoid using databases and hardcode data in resolvers.
Attempts:
2 left
💡 Hint

Think about how to avoid many repeated database calls.

🔧 Debug
expert
2:00remaining
Why does this GraphQL query fail when backed by a database?

This GraphQL query requests a user by ID, but it returns null even though the user exists in the database. What is the likely cause?

GraphQL
query {
  user(id: 5) {
    id
    name
  }
}
AThe resolver does not correctly use the argument 'id' to query the database.
BThe user ID 5 does not exist in the database.
CGraphQL does not support arguments in queries.
DThe database table is empty.
Attempts:
2 left
💡 Hint

Check how the resolver uses the query arguments.