4Catalyzer / graphql-validation-complexity

Query complexity validation for GraphQL.js
MIT License
342 stars 24 forks source link

Could not find a declaration file for module 'graphql-validation-complexity' #263

Open svtbuica opened 4 years ago

svtbuica commented 4 years ago

It seems that there is no type declaration in the npm package to use with typescript.

npm i -D @types/graphql-validation-complexity
npm ERR! code E404 npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2fgraphql-validation-complexity - Not found npm ERR! 404 npm ERR! 404 '@types/graphql-validation-complexity@latest' is not in the npm registry. npm ERR! 404 You should bug the author to publish it (or use the name yourself!) npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, http url, or git url.

jquense commented 4 years ago

That is correct. No TS types for this but it has got a really simple interface

cmd-johnson commented 3 years ago

I stumbled upon the same issue and fixed it by adding this to my index.d.ts file:

declare module "graphql-validation-complexity" {
  import type { ASTNode, GraphQLError, ValidationContext } from "graphql";

  export interface ComplexityLimitRuleOptions {
    onCost?: (cost: number, context: ValidationContext) => void;
    createError?: (cost: number, node: ASTNode) => GraphQLError;
    formatErrorMessage?: (cost: number) => string;

    scalarCost?: number;
    objectCost?: number;
    listFactor?: number;
    introspectionListFactor?: number;
  }

  export function createComplexityLimitRule(
    maxCost: number,
    options?: ComplexityLimitRuleOptions,
  ): (ctx: ValidationContext) => any;
}

If you don't have an index.d.ts file, just create one at your project's root and tsc will pick it up automatically.

The declaration is not complete (the ComplexityVisitor class and complexityLimitExceededErrorMessage function is missing), but if you're only using createComplexityLimitRule, this should be enough.

kruschid commented 3 years ago

@cmd-johnson I used your declarations to open a PR (#284), hope you don't mind? :+1:

advaiyalad commented 3 years ago

@jquense Could this be published, so that people can use the definitions? Thanks!

jjm340 commented 2 years ago

@jquense Are you still maintaining this package?

krunalg commented 2 years ago

Added types in DefinitelyTyped PR #61569