chore: added gql decorator and complexity plugin
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||||
|
import { User } from '../user/user.model';
|
||||||
|
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||||
|
|
||||||
|
export const GqlUser = createParamDecorator<any, any, User>(
|
||||||
|
(_data: any, context: ExecutionContext) => {
|
||||||
|
const { user } = GqlExecutionContext.create(context).getContext<{
|
||||||
|
user: User;
|
||||||
|
}>();
|
||||||
|
if (!user)
|
||||||
|
throw new Error(
|
||||||
|
'@GqlUser decorator use with null user. Make sure the resolve has the @GqlAuthGuard present.',
|
||||||
|
);
|
||||||
|
|
||||||
|
return user;
|
||||||
|
},
|
||||||
|
);
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { GraphQLSchemaHost } from '@nestjs/graphql';
|
||||||
|
import {
|
||||||
|
ApolloServerPlugin,
|
||||||
|
GraphQLRequestListener,
|
||||||
|
} from 'apollo-server-plugin-base';
|
||||||
|
import { Plugin } from '@nestjs/apollo';
|
||||||
|
import { GraphQLError } from 'graphql';
|
||||||
|
import {
|
||||||
|
fieldExtensionsEstimator,
|
||||||
|
getComplexity,
|
||||||
|
simpleEstimator,
|
||||||
|
} from 'graphql-query-complexity';
|
||||||
|
|
||||||
|
const COMPLEXITY_LIMIT = 50;
|
||||||
|
|
||||||
|
@Plugin()
|
||||||
|
export class GQLComplexityPlugin implements ApolloServerPlugin {
|
||||||
|
constructor(private gqlSchemaHost: GraphQLSchemaHost) {}
|
||||||
|
|
||||||
|
async requestDidStart(): Promise<GraphQLRequestListener> {
|
||||||
|
const { schema } = this.gqlSchemaHost;
|
||||||
|
|
||||||
|
return {
|
||||||
|
async didResolveOperation({ request, document }) {
|
||||||
|
const complexity = getComplexity({
|
||||||
|
schema,
|
||||||
|
operationName: request.operationName,
|
||||||
|
query: document,
|
||||||
|
variables: request.variables,
|
||||||
|
estimators: [
|
||||||
|
fieldExtensionsEstimator(),
|
||||||
|
simpleEstimator({ defaultComplexity: 1 }),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
if (complexity > COMPLEXITY_LIMIT) {
|
||||||
|
throw new GraphQLError(
|
||||||
|
`Query is too complex: ${complexity}. Maximum allowed complexity: ${COMPLEXITY_LIMIT}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.log('Query Complexity:', complexity);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user