Complete the code to limit the query depth to 3.
const depthLimit = require('graphql-depth-limit'); const server = new ApolloServer({ schema, validationRules: [depthLimit({ maxDepth: [1] })], });
Setting maxDepth to 3 limits the query depth to 3 levels, preventing overly deep queries.
Complete the code to add a complexity limit of 100 to the GraphQL server.
const { createComplexityRule } = require('graphql-validation-complexity');
const server = new ApolloServer({
schema,
validationRules: [createComplexityRule({ [1]: 100 })],
});The maxComplexity option sets the maximum allowed complexity for a query.
Fix the error in the validation rules array to correctly apply both depth and complexity limits.
const server = new ApolloServer({
schema,
validationRules: [depthLimit({ maxDepth: 4 }), [1]({ maxComplexity: 150 })],
});The function to create a complexity validation rule is createComplexityRule.
Fill both blanks to set a maximum query depth of 5 and maximum complexity of 200.
const server = new ApolloServer({
schema,
validationRules: [[1]({ maxDepth: 5 }), [2]({ maxComplexity: 200 })],
});depthLimit sets the max depth, and createComplexityRule sets the max complexity.
Fill all three blanks to create a GraphQL server with depth limit 4, complexity limit 120, and a custom error message 'Query too complex'.
const server = new ApolloServer({
schema,
validationRules: [
[1]({ maxDepth: 4 }),
[2]({ maxComplexity: 120, [3]: () => new Error('Query too complex') }),
],
});depthLimit sets the depth limit, createComplexityRule sets complexity, and onComplete defines a callback for when the complexity check fails.