MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Home Page:https://typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

directive graphql-rate-limit does not work

gyl89 opened this issue · comments

Describe the Bug
@ratelimit directive does not seem to work (coming from the package graphql-rate-limit-directive)

To Reproduce
Define resolverClasses (including one mutation with @ratelimit), then to build the schema, use the code

import {rateLimitDirective} from 'graphql-rate-limit-directive';
  ...
private async buildSchema(resolverClasses: any): Promise<GraphQLSchema> {
    const { rateLimitDirectiveTypeDefs, rateLimitDirectiveTransformer } = rateLimitDirective();


    const basicSchema = await buildSchema({
      authChecker: ({ context }, perms) => accessCheck(context, perms),
      resolvers: resolverClasses,
      container,
    });
    const schemaMergedWithTypes = mergeSchemas({
      schemas: [basicSchema],
      typeDefs: rateLimitDirectiveTypeDefs,
    });
    const schemaWithRateLimitDirective = rateLimitDirectiveTransformer(schemaMergedWithTypes);
    return schemaWithRateLimitDirective;
  }

...

Expected Behavior
When using "@ratelimit({ limit: 3, duration: 60 })" before a mutation, it should limit the rate at which the mutation can be called.

Logs
When building the schema through buildSchema:
Error parsing directive definition "@rateLimit({ limit: 3, duration: 60 })"

Environment (please complete the following information):

  • OS: Mac os, docker environment
  • Node 16-alpine.
  • Package version : 2.0.0.beta3,
    "graphql-rate-limit-directive": "2.0.4"
  • TypeScript version : 4.9.5

Directives should be passed to the buildSchema function. TypeGraphQL performs schema check after build, so it does not know the definition of your directive and yells.

Thanks for your answers.

I feel thought that the documentation is not very clear :
https://typegraphql.com/docs/directives.html
Part on the 'fake-rename-directives" does not mention that. Besides, the package I mentioned does not export the directive but only the transformer and the typedefs.

Fair point. Maybe @carlocorradini can tell you more as he wrote this chapter (based on git blame).

For now, try to use skipCheck: true option of buildSchema.

Sorry for the late reply, I'll check it ASAP 🥳

@gyl89
Strange... I'm using the same approach with graphql-auth-directive (see https://github.com/carlocorradini/graphql-auth-directive/blob/main/examples/typegraphql/index.ts)
Can you create an example repo with the code so we can examine and test it? Thanks 😅🥳

@gyl89 Can you provide a repository with a minimal reproducible code example, in order to debug the issue?