Complete the code to define a resolver that fetches all users from the database.
const resolvers = {
Query: {
users: async () => {
return await db('users').[1]();
}
}
};The select method is used to query all rows from the 'users' table in SQL databases.
Complete the resolver code to fetch a user by their ID.
const resolvers = {
Query: {
user: async (_, args) => {
return await db('users').where('[1]', args.id).first();
}
}
};The column name for the user ID is typically id in SQL tables.
Fix the error in the resolver code to correctly insert a new user.
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();
}
}
};The table name to insert into is users. The method insert is called on the table name.
Fill both blanks to update a user's email by ID and return the updated user.
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();
}
}
};insert instead of update.The table to update is users and the method to change data is update.
Fill all three blanks to delete a user by ID and return the number of deleted rows.
const resolvers = {
Mutation: {
deleteUser: async (_, args) => {
const count = await db('[1]').where('[2]', args.id).[3]();
return count;
}
}
};remove instead of del.The table is users, the column is id, and the method to delete rows is del.
