diff --git a/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts b/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts new file mode 100644 index 0000000000..1e8a8c3b18 --- /dev/null +++ b/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts @@ -0,0 +1,129 @@ +import { createBatchResolver, ResolverFunction } from "graphql-resolve-batch"; + +// Test cases split up in namespaces to test different type inference scenarios. + +namespace WithSourceAndResult { + interface SomeTestSource { + someSourceProp: string; + } + + interface SomeTestResult { + someTestResultProp: string; + } + + const verify = createBatchResolver( + (sources, _, __) => { + return sources.map(source => { + const res: SomeTestResult = { + someTestResultProp: "" + }; + + return res; + }); + } + ); +} + +namespace WithSourceAndResultAsPromise { + interface SomeTestSource { + someSourceProp: string; + } + + interface SomeTestResult { + someTestResultProp: string; + } + + const verify = createBatchResolver( + (sources, _, __) => { + // $ExpectType ReadonlyArray + const verifySources = sources; + + return sources.map(source => { + return new Promise(resolve => { + const res: SomeTestResult = { + someTestResultProp: "" + }; + + resolve(res); + }); + }); + } + ); +} + +namespace WithSourceAndArgsAndResult { + interface SomeTestSource { + someSourceProp: string; + } + + interface SomeTestArgs { + someArg: string; + } + + interface SomeTestResult { + someTestResultProp: string; + } + + const verify = createBatchResolver< + SomeTestSource, + SomeTestResult, + SomeTestArgs + >((sources, args, _) => { + // $ExpectType ReadonlyArray + const verifySources = sources; + // $ExpectType string + const verifyArgs = args.someArg; + + return sources.map(source => { + return new Promise(resolve => { + const res: SomeTestResult = { + someTestResultProp: "" + }; + + resolve(res); + }); + }); + }); +} + +namespace WithSourceAndArgsAndContextAndResult { + interface SomeTestSource { + someSourceProp: string; + } + + interface SomeTestArgs { + someArg: string; + } + + interface SomeTestContext { + someContextProp: string; + } + + interface SomeTestResult { + someTestResultProp: string; + } + + const verify = createBatchResolver< + SomeTestSource, + SomeTestResult, + SomeTestArgs, + SomeTestContext + >((sources, args, context) => { + // $ExpectType ReadonlyArray + const verifySources = sources; + // $ExpectType string + const verifyArgs = args.someArg; + // $ExpectType string + const verifyContext = context.someContextProp; + + return sources.map(source => { + return new Promise(resolve => { + const res: SomeTestResult = { + someTestResultProp: "" + }; + + resolve(res); + }); + }); + }); +} diff --git a/types/graphql-resolve-batch/index.d.ts b/types/graphql-resolve-batch/index.d.ts new file mode 100644 index 0000000000..3fd9e1242b --- /dev/null +++ b/types/graphql-resolve-batch/index.d.ts @@ -0,0 +1,26 @@ +// Type definitions for graphql-resolve-batch 1.0 +// Project: https://github.com/calebmer/graphql-resolve-batch#readme +// Definitions by: Rutger Hendrickx +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +export function createBatchResolver< + TSource, + TReturn, + TArgs = any, + TContext = any +>( + batchResolveFn: BatchResolveFunction +): ResolverFunction; + +export type ResolverFunction = ( + source: TSource, + args: TArgs, + context: TContext +) => Promise; + +export type BatchResolveFunction = ( + sources: ReadonlyArray, + args: TArgs, + context: TContext +) => TReturn[] | Promise[]; diff --git a/types/graphql-resolve-batch/tsconfig.json b/types/graphql-resolve-batch/tsconfig.json new file mode 100644 index 0000000000..aa4ad6661d --- /dev/null +++ b/types/graphql-resolve-batch/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "strictFunctionTypes": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "graphql-resolve-batch-tests.ts"] +} diff --git a/types/graphql-resolve-batch/tslint.json b/types/graphql-resolve-batch/tslint.json new file mode 100644 index 0000000000..fa856c9e87 --- /dev/null +++ b/types/graphql-resolve-batch/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-namespace": false, + "array-type": [true, "array"] + } +}