0
0
GraphQLquery~10 mins

Context setup 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 GraphQL context parameter in the resolver function.

GraphQL
const resolver = (parent, args, [1]) => { return args.id; }
Drag options to blanks, or click blank then click option'
Acontext
Binfo
Croot
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'info' or 'root' instead of 'context' for shared data.
Confusing the order of resolver parameters.
2fill in blank
medium

Complete the code to access the user ID from the context inside a resolver.

GraphQL
const userId = [1].user.id;
Drag options to blanks, or click blank then click option'
Aargs
Binfo
Cparent
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to get user ID from args or parent instead of context.
Using 'info' which holds query info, not user data.
3fill in blank
hard

Fix the error in the resolver to correctly destructure context and get the database client.

GraphQL
const resolver = (parent, args, [1]) => { const { db } = context; return db.getUser(args.id); }
Drag options to blanks, or click blank then click option'
Acontext
Bctx
Cinfo
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the variable inside the function.
Not destructuring the context object properly.
4fill in blank
hard

Fill both blanks to correctly pass context to the Apollo Server constructor.

GraphQL
const server = new ApolloServer({ typeDefs, resolvers, [1]: () => ([2]) });
Drag options to blanks, or click blank then click option'
Acontext
B{ user: currentUser }
Cschema
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'schema' or 'args' instead of 'context' option.
Not returning an object from the context function.
5fill in blank
hard

Fill all three blanks to create a resolver that uses context to check user role and fetch data.

GraphQL
const resolver = (parent, args, [1]) => { if (![2].user || [3].user.role !== 'admin') { throw new Error('Unauthorized'); } return [2].db.getData(args.id); }
Drag options to blanks, or click blank then click option'
Acontext
Cctx
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing different names for context parameter.
Not checking if user exists before accessing role.