Add type definitions for graphql-errors

This commit is contained in:
Matías Olivera
2018-12-15 10:17:49 -03:00
parent 39b514a4bb
commit db9106038c
4 changed files with 81 additions and 0 deletions

View File

@@ -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");

17
types/graphql-errors/index.d.ts vendored Normal file
View File

@@ -0,0 +1,17 @@
// Type definitions for graphql-errors 2.1
// Project: https://github.com/kadirahq/graphql-errors
// Definitions by: Matías Olivera <https://github.com/MatiasOlivera>
// 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);
}

View File

@@ -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"]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }