0
0
GraphQLquery~20 mins

Server middleware in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Middleware effect on query response

Consider a GraphQL server with middleware that adds a requestId field to the context for each request. Given the query below, what will be the output if the middleware correctly adds a unique requestId?

query GetUser {
  user(id: "1") {
    id
    name
    requestId
  }
}

Assume the user with id "1" has name "Alice".

A{"data": {"user": {"id": "1", "name": "Alice", "requestId": "req-123"}}}
B{"data": {"user": null}}
C{"data": {"user": {"id": "1", "name": "Alice"}}}
D{"errors": [{"message": "Field \"requestId\" not found on type User"}]}
Attempts:
2 left
💡 Hint

Think about the GraphQL schema and what fields are defined on the User type.

🧠 Conceptual
intermediate
1:30remaining
Purpose of server middleware in GraphQL

What is the primary purpose of server middleware in a GraphQL server?

ATo modify the GraphQL schema dynamically during query execution
BTo intercept and process requests before they reach the resolver functions
CTo generate client-side queries automatically
DTo store data permanently in the database
Attempts:
2 left
💡 Hint

Think about what middleware usually does in web servers.

📝 Syntax
advanced
2:30remaining
Identify the syntax error in middleware setup

Which option contains a syntax error in setting up Express middleware for a GraphQL server?

GraphQL
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const app = express();

app.use('/graphql', (req, res, next) => {
  req.context = { user: 'admin' };
  next();
});

app.use('/graphql', graphqlHTTP({
  schema: mySchema,
  graphiql: true,
  context: req.context
}));
AThe middleware function does not call next(), causing requests to hang
BThe app.use calls should be combined into one call
CThe context option in graphqlHTTP cannot access req.context directly like this
DThe schema variable mySchema is not imported
Attempts:
2 left
💡 Hint

Consider how the context option is set in express-graphql.

🔧 Debug
advanced
2:30remaining
Debugging middleware order issue

A developer notices that authentication middleware is not working as expected in their GraphQL server. The middleware is supposed to add a user object to the context, but resolvers receive undefined. What is the most likely cause?

GraphQL
app.use('/graphql', graphqlHTTP({
  schema: mySchema,
  graphiql: true,
  context: ({ req }) => req.user
}));

app.use('/graphql', (req, res, next) => {
  req.user = { id: '123', name: 'Alice' };
  next();
});
AThe authentication middleware is registered after the graphqlHTTP middleware, so it runs too late
BThe context function is not returning the user object correctly
CThe schema does not define a user type
DThe middleware does not call next()
Attempts:
2 left
💡 Hint

Think about the order in which middleware functions are executed in Express.

optimization
expert
3:00remaining
Optimizing middleware for performance

In a high-traffic GraphQL server, which middleware optimization will most effectively reduce latency?

ACache authentication results in middleware to avoid repeated checks per request
BMove authentication checks into resolvers instead of middleware
CRemove all middleware and handle everything in resolvers
DUse synchronous middleware functions to simplify code
Attempts:
2 left
💡 Hint

Consider how caching can reduce repeated work in middleware.