Challenge - 5 Problems
MongoDB with GraphQL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Query Result: Fetching Nested Documents
Given a MongoDB collection users with embedded posts documents, what will be the result of this GraphQL query?
GraphQL
query {
users {
name
posts {
title
commentsCount
}
}
}Attempts:
2 left
💡 Hint
Remember that embedded documents are returned as arrays and fields requested must exist in the schema.
✗ Incorrect
The query requests users with their posts including title and commentsCount. Alice has one post with 5 comments, Bob has no posts, so posts is an empty array.
🧠 Conceptual
intermediate1:30remaining
Understanding GraphQL Resolvers with MongoDB
In a GraphQL API backed by MongoDB, what is the main role of a resolver function?
Attempts:
2 left
💡 Hint
Think about what happens when you ask for data in GraphQL.
✗ Incorrect
Resolvers are functions that run when a GraphQL query or mutation is executed. They fetch data from MongoDB and return it in the shape requested.
📝 Syntax
advanced2:00remaining
Identify the Syntax Error in GraphQL Schema Definition
Which option contains a syntax error in this GraphQL type definition for a MongoDB document?
GraphQL
type User {
id: ID!
name: String!
posts: [Post]
}
type Post {
title: String!
content: String
createdAt: DateTime
}Attempts:
2 left
💡 Hint
Check if 'DateTime' is a valid scalar or custom scalar in GraphQL.
✗ Incorrect
The schema is valid assuming 'DateTime' is defined as a custom scalar. 'ID!' is the correct type for unique identifiers. '[Post]' allows null posts or empty arrays, which is acceptable.
❓ optimization
advanced2:30remaining
Optimizing MongoDB Queries in GraphQL Resolvers
Which option best optimizes a MongoDB query inside a GraphQL resolver to fetch only necessary fields for a user list?
GraphQL
const users = await db.collection('users').find({}).toArray();
Attempts:
2 left
💡 Hint
MongoDB uses 'projection' to specify fields to return.
✗ Incorrect
The 'projection' option in find() specifies which fields to include. Other options like 'fields', 'select', or 'include' are invalid in MongoDB native driver.
🔧 Debug
expert3:00remaining
Debugging a GraphQL Mutation with MongoDB Insert
A GraphQL mutation to add a new user to MongoDB is failing. Which option explains the cause of the error?
GraphQL
mutation {
addUser(name: "John", email: "john@example.com") {
id
name
}
}
// Resolver snippet:
async addUser(parent, args, context) {
const result = await context.db.collection('users').insertOne(args);
return result.ops[0];
}Attempts:
2 left
💡 Hint
Check the MongoDB driver version and its return value for insertOne.
✗ Incorrect
Modern MongoDB drivers do not return 'ops' from insertOne. Instead, use 'insertedId' to get the new document's ID and then query it if needed.