JWT integration in GraphQL - Time & Space Complexity
When using JWT (JSON Web Tokens) in GraphQL, we want to understand how the time to verify and use tokens grows as more requests come in.
We ask: How does the work needed to check tokens change with the number of users or requests?
Analyze the time complexity of the following GraphQL resolver using JWT for authentication.
type Query {
userData(token: String!): User
}
const resolver = {
Query: {
userData: async (_, { token }) => {
const payload = verifyJWT(token); // verify token signature and decode
return getUserFromDB(payload.userId); // fetch user data
}
}
};
This code verifies a JWT token and then fetches user data from the database for each request.
Look at what repeats when many requests come in.
- Primary operation: Verifying the JWT token and fetching user data.
- How many times: Once per request, for each token received.
As the number of requests (n) grows, the server verifies and fetches data for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 token verifications + 10 database fetches |
| 100 | 100 token verifications + 100 database fetches |
| 1000 | 1000 token verifications + 1000 database fetches |
Pattern observation: The work grows directly with the number of requests; doubling requests doubles work.
Time Complexity: O(n)
This means the time to handle requests grows linearly with how many requests come in.
[X] Wrong: "Verifying a JWT token is a one-time cost regardless of requests."
[OK] Correct: Each request has its own token to verify, so the work repeats for every request.
Understanding how authentication steps like JWT verification scale helps you design systems that handle many users smoothly.
"What if we cached verified tokens to skip repeated verification? How would the time complexity change?"
Practice
Solution
Step 1: Understand JWT role in security
JWT (JSON Web Token) is used to confirm who the user is by verifying their identity.Step 2: Connect JWT to database access
This verification controls who can access or change data in the GraphQL database.Final Answer:
To verify the user's identity and secure database access -> Option CQuick Check:
JWT secures access [OK]
- Thinking JWT speeds up queries
- Confusing JWT with data storage
- Assuming JWT changes schema format
Solution
Step 1: Recall standard JWT header format
The JWT token is sent in the HTTP header named 'Authorization' with the prefix 'Bearer'.Step 2: Match the correct header syntax
The correct syntax is 'Authorization: Bearer <token>'. Other options use wrong header names or order.Final Answer:
Authorization: Bearer <token> -> Option DQuick Check:
JWT header uses Bearer prefix [OK]
- Using 'Token' instead of 'Bearer'
- Wrong header name like 'Auth'
- Mixing order of 'Token' and 'Authorization'
const user = verifyToken(context.headers.authorization); if (!user) return null; return db.getUserData(user.id);What happens if the token is invalid or missing?
Solution
Step 1: Analyze token verification
The function verifyToken returns null or false if the token is invalid or missing.Step 2: Check resolver behavior on invalid token
If user is falsy, the resolver returns null, meaning no data is sent back.Final Answer:
The resolver returns null and no data is fetched -> Option AQuick Check:
Invalid token leads to null return [OK]
- Expecting a syntax error on invalid token
- Assuming data is returned without user
- Thinking server crashes on bad token
const token = context.headers.Authorization;
const user = verifyToken(token);
if (!user) throw new Error('Unauthorized');
return db.getUserData(user.id);Solution
Step 1: Check header name case sensitivity
HTTP headers in Node.js are usually lowercase, so 'authorization' is correct, not 'Authorization'.Step 2: Confirm token extraction correctness
Using 'Authorization' may cause token to be undefined, leading to verification failure.Final Answer:
Header name should be lowercase 'authorization', not 'Authorization' -> Option BQuick Check:
Header keys are lowercase [OK]
- Using uppercase header keys in Node.js
- Calling db before verifying user
- Confusing error message types
Solution
Step 1: Understand role-based access control
To restrict access, verify the token and check the user's role before sending data.Step 2: Evaluate options for security
Returning data before verification or skipping roles risks exposing sensitive info.Final Answer:
Verify token, then check if user.role === 'admin' before returning data -> Option AQuick Check:
Check role after token verification [OK]
- Sending data before verifying user
- Ignoring roles in backend checks
- Trusting frontend for security
Ready to prove it?
Pass the test and this pattern moves unknown
