From db9106038ce984e4970d49706bafa27d2fd77d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Olivera?= Date: Sat, 15 Dec 2018 10:17:49 -0300 Subject: [PATCH] Add type definitions for graphql-errors --- types/graphql-errors/graphql-errors-tests.ts | 47 ++++++++++++++++++++ types/graphql-errors/index.d.ts | 17 +++++++ types/graphql-errors/tsconfig.json | 16 +++++++ types/graphql-errors/tslint.json | 1 + 4 files changed, 81 insertions(+) create mode 100644 types/graphql-errors/graphql-errors-tests.ts create mode 100644 types/graphql-errors/index.d.ts create mode 100644 types/graphql-errors/tsconfig.json create mode 100644 types/graphql-errors/tslint.json diff --git a/types/graphql-errors/graphql-errors-tests.ts b/types/graphql-errors/graphql-errors-tests.ts new file mode 100644 index 0000000000..0820fe2afa --- /dev/null +++ b/types/graphql-errors/graphql-errors-tests.ts @@ -0,0 +1,47 @@ +import { GraphQLSchema, buildSchema } from 'graphql'; +import { + UserError, + maskErrors, + handlerFunction, + setDefaultHandler +} from 'graphql-errors'; + +// $ExpectType GraphQLSchema +const schema: GraphQLSchema = buildSchema(` + # graphql schema definition +`); + +const customHandler: handlerFunction = err => { + return { ...err, message: 'Internal error' }; +}; + +/** + * Mask GraphQL error messages + */ + +// $ExpectType void +maskErrors(schema); + +// $ExpectType void +maskErrors(schema, customHandler); + +// $ExpectError +maskErrors('schema'); + +/** + * Set callback function that modifies the errors + */ + +// $ExpectType void +setDefaultHandler(customHandler); + +// $ExpectError +setDefaultHandler(); + +// $ExpectError +setDefaultHandler(err => {}); + +/** + * Thrown an user error + */ +new UserError("Uh, Houston, we've had a problem"); diff --git a/types/graphql-errors/index.d.ts b/types/graphql-errors/index.d.ts new file mode 100644 index 0000000000..5dc452ab20 --- /dev/null +++ b/types/graphql-errors/index.d.ts @@ -0,0 +1,17 @@ +// Type definitions for graphql-errors 2.1 +// Project: https://github.com/kadirahq/graphql-errors +// Definitions by: Matías Olivera +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 + +import { GraphQLSchema } from 'graphql'; + +export type handlerFunction = (err: Error) => Error; + +export function setDefaultHandler(fn: handlerFunction): void; + +export function maskErrors(schema: GraphQLSchema, fn?: handlerFunction): void; + +export class UserError extends Error { + constructor(message: string); +} diff --git a/types/graphql-errors/tsconfig.json b/types/graphql-errors/tsconfig.json new file mode 100644 index 0000000000..159013d442 --- /dev/null +++ b/types/graphql-errors/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6", "esnext.asynciterable"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": ["index.d.ts", "graphql-errors-tests.ts"] +} diff --git a/types/graphql-errors/tslint.json b/types/graphql-errors/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/graphql-errors/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }