From 252159b951a9e7c69cfac135247d34d7fde56fb1 Mon Sep 17 00:00:00 2001 From: Abhik Mitra Date: Wed, 5 Sep 2018 22:23:48 +0530 Subject: [PATCH 1/4] Added Types for Graphql Query Complexity --- .../dist/QueryComplexity.d.ts | 26 +++++++++++++++++++ .../graphql-query-complexity/dist/index.d.ts | 2 ++ .../graphql-query-complexity-tests.ts | 4 +++ types/graphql-query-complexity/index.d.ts | 7 +++++ types/graphql-query-complexity/tsconfig.json | 24 +++++++++++++++++ types/graphql-query-complexity/tslint.json | 1 + 6 files changed, 64 insertions(+) create mode 100644 types/graphql-query-complexity/dist/QueryComplexity.d.ts create mode 100644 types/graphql-query-complexity/dist/index.d.ts create mode 100644 types/graphql-query-complexity/graphql-query-complexity-tests.ts create mode 100644 types/graphql-query-complexity/index.d.ts create mode 100644 types/graphql-query-complexity/tsconfig.json create mode 100644 types/graphql-query-complexity/tslint.json diff --git a/types/graphql-query-complexity/dist/QueryComplexity.d.ts b/types/graphql-query-complexity/dist/QueryComplexity.d.ts new file mode 100644 index 0000000000..898e2023bb --- /dev/null +++ b/types/graphql-query-complexity/dist/QueryComplexity.d.ts @@ -0,0 +1,26 @@ +/** + * Created by Ivo Meißner on 28.07.17. + */ +import { ValidationContext, FragmentDefinitionNode, OperationDefinitionNode, FieldNode, InlineFragmentNode, GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType, GraphQLError } from 'graphql'; +export interface QueryComplexityOptions { + maximumComplexity: number; + variables?: object; + onComplete?: (complexity: number) => void; + createError?: (max: number, actual: number) => GraphQLError; +} +export default class QueryComplexity { + context: ValidationContext; + complexity: number; + options: QueryComplexityOptions; + fragments: { + [name: string]: FragmentDefinitionNode; + }; + OperationDefinition: object; + constructor(context: ValidationContext, options: QueryComplexityOptions); + onOperationDefinitionEnter(operation: OperationDefinitionNode): void; + onOperationDefinitionLeave(): GraphQLError | undefined; + nodeComplexity(node: FieldNode | FragmentDefinitionNode | InlineFragmentNode | + OperationDefinitionNode, typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType, complexity?: number): number; + createError(): GraphQLError; + getDefaultComplexity(args: object, childScore: number): number; +} diff --git a/types/graphql-query-complexity/dist/index.d.ts b/types/graphql-query-complexity/dist/index.d.ts new file mode 100644 index 0000000000..5ceb3a70ea --- /dev/null +++ b/types/graphql-query-complexity/dist/index.d.ts @@ -0,0 +1,2 @@ +import { QueryComplexityOptions } from './QueryComplexity'; +export default function createQueryComplexityValidator(options: QueryComplexityOptions): () => void; diff --git a/types/graphql-query-complexity/graphql-query-complexity-tests.ts b/types/graphql-query-complexity/graphql-query-complexity-tests.ts new file mode 100644 index 0000000000..7fc5696726 --- /dev/null +++ b/types/graphql-query-complexity/graphql-query-complexity-tests.ts @@ -0,0 +1,4 @@ +import QueryComplexity from "graphql-query-complexity/dist/QueryComplexity"; + +const complexity = new QueryComplexity(null as any, null as any); +complexity.createError().message; // $ExpectType string diff --git a/types/graphql-query-complexity/index.d.ts b/types/graphql-query-complexity/index.d.ts new file mode 100644 index 0000000000..e7d723884d --- /dev/null +++ b/types/graphql-query-complexity/index.d.ts @@ -0,0 +1,7 @@ +// Type definitions for graphql-query-complexity 0.1 +// Project: https://github.com/ivome/graphql-query-complexity +// Definitions by: Abhik Mitra +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 +import createQueryComplexityValidator from "./dist"; +export default createQueryComplexityValidator; diff --git a/types/graphql-query-complexity/tsconfig.json b/types/graphql-query-complexity/tsconfig.json new file mode 100644 index 0000000000..9853a69621 --- /dev/null +++ b/types/graphql-query-complexity/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "dist/index.d.ts", + "dist/QueryComplexity.d.ts", + "graphql-query-complexity-tests.ts" + ] +} \ No newline at end of file diff --git a/types/graphql-query-complexity/tslint.json b/types/graphql-query-complexity/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/graphql-query-complexity/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 5a3b76f927428fa90d73e6d949e07b8d9953fd8c Mon Sep 17 00:00:00 2001 From: Abhik Mitra Date: Wed, 5 Sep 2018 22:57:04 +0530 Subject: [PATCH 2/4] Fixed tests --- .../graphql-query-complexity/graphql-query-complexity-tests.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/graphql-query-complexity/graphql-query-complexity-tests.ts b/types/graphql-query-complexity/graphql-query-complexity-tests.ts index 7fc5696726..29a3913dbf 100644 --- a/types/graphql-query-complexity/graphql-query-complexity-tests.ts +++ b/types/graphql-query-complexity/graphql-query-complexity-tests.ts @@ -1,4 +1,5 @@ import QueryComplexity from "graphql-query-complexity/dist/QueryComplexity"; - +import complexityFunction from "graphql-query-complexity"; const complexity = new QueryComplexity(null as any, null as any); complexity.createError().message; // $ExpectType string +complexityFunction({maximumComplexity: 10}); From 0b4bd9a02ca9dd5c656abfc660401630744a2a58 Mon Sep 17 00:00:00 2001 From: Abhik Mitra Date: Wed, 5 Sep 2018 23:12:02 +0530 Subject: [PATCH 3/4] Fixed test --- types/graphql-query-complexity/index.d.ts | 2 +- types/graphql-query-complexity/tsconfig.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/graphql-query-complexity/index.d.ts b/types/graphql-query-complexity/index.d.ts index e7d723884d..4c5954fb4f 100644 --- a/types/graphql-query-complexity/index.d.ts +++ b/types/graphql-query-complexity/index.d.ts @@ -2,6 +2,6 @@ // Project: https://github.com/ivome/graphql-query-complexity // Definitions by: Abhik Mitra // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 2.3 import createQueryComplexityValidator from "./dist"; export default createQueryComplexityValidator; diff --git a/types/graphql-query-complexity/tsconfig.json b/types/graphql-query-complexity/tsconfig.json index 9853a69621..d706752c48 100644 --- a/types/graphql-query-complexity/tsconfig.json +++ b/types/graphql-query-complexity/tsconfig.json @@ -17,6 +17,7 @@ "forceConsistentCasingInFileNames": true }, "files": [ + "index.d.ts", "dist/index.d.ts", "dist/QueryComplexity.d.ts", "graphql-query-complexity-tests.ts" From 02cf6477bf2556b623ed7507fd4be989266e1f47 Mon Sep 17 00:00:00 2001 From: Abhik Mitra Date: Wed, 5 Sep 2018 23:25:24 +0530 Subject: [PATCH 4/4] Added more types --- types/graphql-query-complexity/dist/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/graphql-query-complexity/dist/index.d.ts b/types/graphql-query-complexity/dist/index.d.ts index 5ceb3a70ea..082f70cc11 100644 --- a/types/graphql-query-complexity/dist/index.d.ts +++ b/types/graphql-query-complexity/dist/index.d.ts @@ -1,2 +1,4 @@ import { QueryComplexityOptions } from './QueryComplexity'; export default function createQueryComplexityValidator(options: QueryComplexityOptions): () => void; +export type ComplexityResolver = (args: any, complexity: number) => number; +export type Complexity = number | ComplexityResolver;