0
0
GraphQLquery~10 mins

SQL database resolvers in 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 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'
Aselect
BgetAll
Cfetch
DselectAll
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.
2fill in blank
medium

Complete 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'
Auid
Buser_id
CuserId
Did
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.
3fill in blank
hard

Fix 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'
Ainsert
Buser
Cusers
DaddUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using the method name instead of the table name.
Using singular form instead of plural for the table.
4fill in blank
hard

Fill 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'
Ausers
Bupdate
Cinsert
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of update.
Using wrong table name.
5fill in blank
hard

Fill 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'
Ausers
Bid
Cdel
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of del.
Using wrong column or table names.