0
0
GraphQLquery~10 mins

Context-based authentication 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 query that fetches the current user's ID.

GraphQL
query { currentUser { [1] } }
Drag options to blanks, or click blank then click option'
Aemail
Bpassword
Cid
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using sensitive fields like password or token in the query.
Selecting fields that do not identify the user uniquely.
2fill in blank
medium

Complete the code to add a context argument to the resolver function signature.

GraphQL
const resolver = (parent, args, [1]) => { /* authentication logic */ }
Drag options to blanks, or click blank then click option'
Ainfo
Bcontext
Cparams
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using info instead of context for authentication data.
Omitting the context argument entirely.
3fill in blank
hard

Fix the error in the authentication check inside the resolver.

GraphQL
if (![1].user) { throw new Error('Not authenticated'); }
Drag options to blanks, or click blank then click option'
Acontext
Bparent
Cargs
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Checking args.user instead of context.user.
Using parent or info for authentication data.
4fill in blank
hard

Fill both blanks to create a context object that includes the authenticated user and request headers.

GraphQL
const context = ({ [1] }) => { return { user: getUser([2].headers.authorization) }; };
Drag options to blanks, or click blank then click option'
Areq
Bres
Crequest
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using res or response instead of req.
Not accessing the authorization header correctly.
5fill in blank
hard

Fill all three blanks to define a GraphQL directive for requiring authentication.

GraphQL
directive @[1] on [2] | [3]
Drag options to blanks, or click blank then click option'
Aauth
BFIELD_DEFINITION
COBJECT
DQUERY
Attempts:
3 left
💡 Hint
Common Mistakes
Using QUERY as a directive location, which is invalid.
Naming the directive something other than auth.