5.
You want to allow queries up to depth 4 but block deeper ones. You also want to log a warning when a query exceeds the limit. Which approach correctly combines depth limiting with custom logging in a GraphQL server?
const depthLimit = require('graphql-depth-limit');
const { ApolloServer } = require('apollo-server');
const loggingDepthLimit = (maxDepth) => {
return (context) => {
const validationRule = depthLimit(maxDepth);
return (validationContext) => {
const errors = validationRule(validationContext);
if (errors && errors.length > 0) {
console.warn('Query depth exceeded:', errors);
}
return errors;
};
};
};
const server = new ApolloServer({
schema,
validationRules: [loggingDepthLimit(4)]
});