From be81617ff7c423a69426537a88575c563b234d03 Mon Sep 17 00:00:00 2001 From: nayni Date: Sat, 10 Mar 2018 14:38:09 +0100 Subject: [PATCH 1/5] Add jsdocs + remove unnessecary union return type --- types/graphql-resolve-batch/index.d.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/types/graphql-resolve-batch/index.d.ts b/types/graphql-resolve-batch/index.d.ts index e78ddafb37..af0fd725f7 100644 --- a/types/graphql-resolve-batch/index.d.ts +++ b/types/graphql-resolve-batch/index.d.ts @@ -4,6 +4,21 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 +/** + * Creates a GraphQL.js field resolver that batches together multiple resolves + * together that share the *exact* same GraphQL field selection. + * + * Note: + * The batchResolveFunction you supply always expects you to always return an array of TReturn (or a promise with this array). + * This is because you will have to return a TReturn for every source given to you by the batch function. + * For an example of this please see the examples in the original project repository: + * https://github.com/calebmer/graphql-resolve-batch/tree/master/examples + * + * @template TSource The original type of a single source. + * @template TReturn The return type of the field resolver. + * @template TArgs The type of supplied arguments. + * @template TContext The type of the current resolver context. + */ export function createBatchResolver< TSource, TReturn, @@ -13,12 +28,19 @@ export function createBatchResolver< batchResolveFn: BatchResolveFunction ): ResolverFunction; +/** + * The resulting field resolver that batches together multiple resolves. + * Graphql-resolve-batch will always return a promise for this resulting resolver. + */ export type ResolverFunction = ( source: TSource, args: TArgs, context: TContext -) => Promise | Promise; +) => Promise; +/** + * A batch function to resolve all fields for the given sources in a single batch. + */ export type BatchResolveFunction = ( sources: ReadonlyArray, args: TArgs, From 71347423382b8babdfca1ea9e17cc98f6953d459 Mon Sep 17 00:00:00 2001 From: nayni Date: Sat, 10 Mar 2018 14:40:39 +0100 Subject: [PATCH 2/5] Add batchfn jsdocs --- types/graphql-resolve-batch/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/graphql-resolve-batch/index.d.ts b/types/graphql-resolve-batch/index.d.ts index af0fd725f7..e0118fc026 100644 --- a/types/graphql-resolve-batch/index.d.ts +++ b/types/graphql-resolve-batch/index.d.ts @@ -18,6 +18,7 @@ * @template TReturn The return type of the field resolver. * @template TArgs The type of supplied arguments. * @template TContext The type of the current resolver context. + * @returns A batch function to resolve all fields for the given sources in a single batch. */ export function createBatchResolver< TSource, From 64ca985e2a751f2ba5d778fad7dda0305e6ea90d Mon Sep 17 00:00:00 2001 From: nayni Date: Sat, 10 Mar 2018 14:50:26 +0100 Subject: [PATCH 3/5] improve test coverage --- .../graphql-resolve-batch-tests.ts | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts b/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts index 519a3a4088..9043cdaf0e 100644 --- a/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts +++ b/types/graphql-resolve-batch/graphql-resolve-batch-tests.ts @@ -16,6 +16,14 @@ interface SomeTestResult { someTestResultProp: string; } +const batchFunction = (sources: ReadonlyArray) => { + const someTestResult: SomeTestResult = { + someTestResultProp: "Hello" + }; + + return sources.map(source => someTestResult); +}; + const asyncBatchFunction = async (sources: ReadonlyArray) => { return new Promise(resolve => { const res = [ @@ -27,19 +35,35 @@ const asyncBatchFunction = async (sources: ReadonlyArray) => { }); }; +const asyncBatchFunctionWhenTReturnIsArray = async ( + sources: ReadonlyArray +) => { + const sourceBatches = sources.map(() => { + return new Promise(resolve => { + const res = [ + { + someTestResultProp: "" + } + ]; + resolve(res); + }); + }); + + return Promise.all(sourceBatches); +}; + +// $ExpectType ResolverFunction const withSourceAndResultTyped = createBatchResolver< SomeTestSource, SomeTestResult >((sources, _, __) => { - return sources.map(source => { - const res: SomeTestResult = { - someTestResultProp: "" - }; + // $ExpectType ReadonlyArray + const verifySources = sources; - return res; - }); + return batchFunction(sources); }); +// $ExpectType ResolverFunction const withSourceAndResultTypedAsPromise = createBatchResolver< SomeTestSource, SomeTestResult @@ -50,6 +74,7 @@ const withSourceAndResultTypedAsPromise = createBatchResolver< return result; }); +// $ExpectType ResolverFunction const withSourceAndArgsAndResultTyped = createBatchResolver< SomeTestSource, SomeTestResult, @@ -64,6 +89,7 @@ const withSourceAndArgsAndResultTyped = createBatchResolver< return result; }); +// $ExpectType ResolverFunction const withSourceAndArgsAndContextTyped = createBatchResolver< SomeTestSource, SomeTestResult, @@ -80,3 +106,13 @@ const withSourceAndArgsAndContextTyped = createBatchResolver< const result = await asyncBatchFunction(sources); return result; }); + +// $ExpectType ResolverFunction +const withResultIsArray = createBatchResolver( + (sources, _, __) => { + // $ExpectType ReadonlyArray + const verifySources = sources; + + return asyncBatchFunctionWhenTReturnIsArray(sources); + } +); From 3bd0059542296baefdf531fc24c0a37c8ccce713 Mon Sep 17 00:00:00 2001 From: nayni Date: Sat, 10 Mar 2018 14:57:02 +0100 Subject: [PATCH 4/5] clean up jsdocs --- types/graphql-resolve-batch/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/graphql-resolve-batch/index.d.ts b/types/graphql-resolve-batch/index.d.ts index e0118fc026..3818304537 100644 --- a/types/graphql-resolve-batch/index.d.ts +++ b/types/graphql-resolve-batch/index.d.ts @@ -9,7 +9,7 @@ * together that share the *exact* same GraphQL field selection. * * Note: - * The batchResolveFunction you supply always expects you to always return an array of TReturn (or a promise with this array). + * The batchResolveFunction you supply always expects you to return an array of TReturn (or a promise with this array). * This is because you will have to return a TReturn for every source given to you by the batch function. * For an example of this please see the examples in the original project repository: * https://github.com/calebmer/graphql-resolve-batch/tree/master/examples From d7d3e0712cf35e2bba5932c2b56d8fbde878f9bf Mon Sep 17 00:00:00 2001 From: nayni Date: Sat, 10 Mar 2018 15:03:13 +0100 Subject: [PATCH 5/5] jsdocs: @return should be @param --- types/graphql-resolve-batch/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/graphql-resolve-batch/index.d.ts b/types/graphql-resolve-batch/index.d.ts index 3818304537..ea90a83555 100644 --- a/types/graphql-resolve-batch/index.d.ts +++ b/types/graphql-resolve-batch/index.d.ts @@ -18,7 +18,7 @@ * @template TReturn The return type of the field resolver. * @template TArgs The type of supplied arguments. * @template TContext The type of the current resolver context. - * @returns A batch function to resolve all fields for the given sources in a single batch. + * @param batchResolveFn A batch function to resolve all fields for the given sources in a single batch. */ export function createBatchResolver< TSource,