0
0
GraphQLquery~20 mins

MongoDB with GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB with GraphQL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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
    }
  }
}
A[{"name":"Alice","posts":[{"title":"Hello World","commentsCount":0}]},{"name":"Bob","posts":[]}]
B[{"name":"Alice","posts":[{"title":"Hello World"}]},{"name":"Bob","posts":null}]
C[{"name":"Alice","posts":[{"title":"Hello World","commentsCount":5}]},{"name":"Bob","posts":[]}]
D[{"name":"Alice","posts":null},{"name":"Bob","posts":[]}]
Attempts:
2 left
💡 Hint
Remember that embedded documents are returned as arrays and fields requested must exist in the schema.
🧠 Conceptual
intermediate
1:30remaining
Understanding GraphQL Resolvers with MongoDB
In a GraphQL API backed by MongoDB, what is the main role of a resolver function?
ATo define the database schema and indexes in MongoDB
BTo validate user input before sending it to the client
CTo convert GraphQL queries into SQL commands
DTo fetch and return data from MongoDB when a query or mutation is executed
Attempts:
2 left
💡 Hint
Think about what happens when you ask for data in GraphQL.
📝 Syntax
advanced
2: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
}
AThe field 'createdAt' should be of type 'String' instead of 'DateTime'.
BThe type definition is correct and valid.
CThe list type for 'posts' should be '[Post!]' instead of '[Post]'.
DThe 'id' field should be of type 'String!' instead of 'ID!'.
Attempts:
2 left
💡 Hint
Check if 'DateTime' is a valid scalar or custom scalar in GraphQL.
optimization
advanced
2: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();
Aconst users = await db.collection('users').find({}, { projection: { name: 1, email: 1 } }).toArray();
Bconst users = await db.collection('users').find({}, { fields: { name: 1, email: 1 } }).toArray();
Cconst users = await db.collection('users').find({}, { select: ['name', 'email'] }).toArray();
Dconst users = await db.collection('users').find({}, { include: ['name', 'email'] }).toArray();
Attempts:
2 left
💡 Hint
MongoDB uses 'projection' to specify fields to return.
🔧 Debug
expert
3: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];
}
AThe 'insertOne' method does not return 'ops' in recent MongoDB drivers; use 'result.insertedId' and fetch the document separately.
BThe mutation is missing required fields in the input arguments.
CThe resolver should use 'insertMany' instead of 'insertOne' for single inserts.
DThe 'args' object cannot be passed directly to 'insertOne' due to type mismatch.
Attempts:
2 left
💡 Hint
Check the MongoDB driver version and its return value for insertOne.