0
0
GraphQLquery~10 mins

MongoDB with GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a GraphQL query that fetches all users from MongoDB.

GraphQL
type Query { users: [User] [1] }
Drag options to blanks, or click blank then click option'
A!
C?
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '!' which makes the list non-nullable and may cause errors if no users exist.
2fill in blank
medium

Complete the resolver function to fetch all users from MongoDB collection.

GraphQL
const resolvers = { Query: { users: async () => await db.collection('[1]').find().toArray() } }
Drag options to blanks, or click blank then click option'
Acustomers
Busers
Corders
Dproducts
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong collection name like 'orders' or 'products' which returns wrong data.
3fill in blank
hard

Fix the error in the GraphQL schema for a User type with an ID field.

GraphQL
type User { id: [1] name: String }
Drag options to blanks, or click blank then click option'
AInt
BID!
CString!
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Int' or 'Boolean' for ID which is not correct for unique identifiers.
4fill in blank
hard

Fill both blanks to define a mutation that adds a new user with a name argument.

GraphQL
type Mutation { addUser(name: [1]): User [2] }
Drag options to blanks, or click blank then click option'
AString!
BBoolean
C!
DID
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving out '!' which allows null values and may cause errors.
5fill in blank
hard

Fill all three blanks to write a resolver for addUser mutation that inserts a user and returns it.

GraphQL
const resolvers = { Mutation: { addUser: async (_, { [1] }) => { const result = await db.collection('users').insertOne({ [2] }); return result.[3]; } } }
Drag options to blanks, or click blank then click option'
Aname
Bname: name
Cops[0]
DinsertedId
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'insertedId' instead of the full inserted document.
Not matching argument name and inserted document field.