Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a resolver that fetches all users from the database.
GraphQL
const resolvers = {
Query: {
users: async () => {
return await db('users').[1]();
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist on the database object.
Confusing fetch or getAll with the correct SQL method.
✗ Incorrect
The select method is used to query all rows from the 'users' table in SQL databases.
2fill in blank
mediumComplete the resolver code to fetch a user by their ID.
GraphQL
const resolvers = {
Query: {
user: async (_, args) => {
return await db('users').where('[1]', args.id).first();
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in the table.
Using camelCase instead of snake_case or vice versa.
✗ Incorrect
The column name for the user ID is typically id in SQL tables.
3fill in blank
hardFix the error in the resolver code to correctly insert a new user.
GraphQL
const resolvers = {
Mutation: {
addUser: async (_, args) => {
const [id] = await db('[1]').insert({ name: args.name, email: args.email });
return db.select('*').from('users').where('id', id).first();
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the method name instead of the table name.
Using singular form instead of plural for the table.
✗ Incorrect
The table name to insert into is users. The method insert is called on the table name.
4fill in blank
hardFill both blanks to update a user's email by ID and return the updated user.
GraphQL
const resolvers = {
Mutation: {
updateUserEmail: async (_, args) => {
await db('[1]').where('id', args.id).[2]({ email: args.email });
return db.select('*').from('users').where('id', args.id).first();
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
insert instead of update.Using wrong table name.
✗ Incorrect
The table to update is users and the method to change data is update.
5fill in blank
hardFill all three blanks to delete a user by ID and return the number of deleted rows.
GraphQL
const resolvers = {
Mutation: {
deleteUser: async (_, args) => {
const count = await db('[1]').where('[2]', args.id).[3]();
return count;
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
remove instead of del.Using wrong column or table names.
✗ Incorrect
The table is users, the column is id, and the method to delete rows is del.