mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 04:50:18 +00:00
Add type definitions for graphql-resolve-batch
This commit is contained in:
@@ -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<SomeTestSource, SomeTestResult>(
|
||||
(sources, _, __) => {
|
||||
return sources.map(source => {
|
||||
const res: SomeTestResult = {
|
||||
someTestResultProp: ""
|
||||
};
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
namespace WithSourceAndResultAsPromise {
|
||||
interface SomeTestSource {
|
||||
someSourceProp: string;
|
||||
}
|
||||
|
||||
interface SomeTestResult {
|
||||
someTestResultProp: string;
|
||||
}
|
||||
|
||||
const verify = createBatchResolver<SomeTestSource, SomeTestResult>(
|
||||
(sources, _, __) => {
|
||||
// $ExpectType ReadonlyArray<SomeTestSource>
|
||||
const verifySources = sources;
|
||||
|
||||
return sources.map(source => {
|
||||
return new Promise<SomeTestResult>(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<SomeTestSource>
|
||||
const verifySources = sources;
|
||||
// $ExpectType string
|
||||
const verifyArgs = args.someArg;
|
||||
|
||||
return sources.map(source => {
|
||||
return new Promise<SomeTestResult>(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<SomeTestSource>
|
||||
const verifySources = sources;
|
||||
// $ExpectType string
|
||||
const verifyArgs = args.someArg;
|
||||
// $ExpectType string
|
||||
const verifyContext = context.someContextProp;
|
||||
|
||||
return sources.map(source => {
|
||||
return new Promise<SomeTestResult>(resolve => {
|
||||
const res: SomeTestResult = {
|
||||
someTestResultProp: ""
|
||||
};
|
||||
|
||||
resolve(res);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
+26
@@ -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 <https://github.com/nayni>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
export function createBatchResolver<
|
||||
TSource,
|
||||
TReturn,
|
||||
TArgs = any,
|
||||
TContext = any
|
||||
>(
|
||||
batchResolveFn: BatchResolveFunction<TSource, TArgs, TContext, TReturn>
|
||||
): ResolverFunction<TSource, TArgs, TContext, TReturn>;
|
||||
|
||||
export type ResolverFunction<TSource, TArgs, TContext, TReturn> = (
|
||||
source: TSource,
|
||||
args: TArgs,
|
||||
context: TContext
|
||||
) => Promise<TReturn>;
|
||||
|
||||
export type BatchResolveFunction<TSource, TArgs, TContext, TReturn> = (
|
||||
sources: ReadonlyArray<TSource>,
|
||||
args: TArgs,
|
||||
context: TContext
|
||||
) => TReturn[] | Promise<TReturn>[];
|
||||
@@ -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"]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"no-namespace": false,
|
||||
"array-type": [true, "array"]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user