Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tg feature resolvers types #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions dist/context.d.ts

This file was deleted.

33 changes: 0 additions & 33 deletions dist/context.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/context.js.map

This file was deleted.

3 changes: 0 additions & 3 deletions dist/helper.d.ts

This file was deleted.

30 changes: 0 additions & 30 deletions dist/helper.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/helper.js.map

This file was deleted.

5 changes: 0 additions & 5 deletions dist/index.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions dist/index.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.js.map

This file was deleted.

2 changes: 0 additions & 2 deletions dist/promise.d.ts

This file was deleted.

18 changes: 0 additions & 18 deletions dist/promise.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/promise.js.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/resolver.d.ts

This file was deleted.

60 changes: 0 additions & 60 deletions dist/resolver.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/resolver.js.map

This file was deleted.

3 changes: 0 additions & 3 deletions dist/util.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions dist/util.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/util.js.map

This file was deleted.

24 changes: 21 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { getPromise } from './promise';
import { isFunction, Promisify, isNotNullOrUndefined } from './util';

const Promise = getPromise();

export const createResolver = (resFn, errFn) => {
export interface ResultFunction<ResulType> {
(root, args, context, info): Promise<ResulType> | ResulType | void
}

export interface ErrorFunction<ErrorType> {
(root, args, context, err): ErrorType | void
}

export interface CreateResolverFunction {
<R, E>(resFn: ResultFunction<R>, errFn?: ErrorFunction<E>): Resolver<R>
}

export interface Resolver<ResulType> {
(root, args: {}, context: {}, info: {}): Promise<ResulType>
createResolver?: CreateResolverFunction
}

export const createResolver: CreateResolverFunction = <R, E>(resFn: ResultFunction<R>, errFn: ErrorFunction<E>) => {
const Promise = getPromise();
const baseResolver = (root, args = {}, context = {}, info = {}) => {
const baseResolver: Resolver<R> = (root, args = {}, context = {}, info = {}) => {
// Return resolving promise with `null` if the resolver function param is not a function
if (!isFunction(resFn)) return Promise.resolve(null);
return Promisify(resFn)(root, args, context, info).catch(e => {
Expand All @@ -20,7 +38,7 @@ export const createResolver = (resFn, errFn) => {
});
});
};
baseResolver['createResolver'] = (cResFn, cErrFn) => {
baseResolver.createResolver = (cResFn, cErrFn) => {
const Promise = getPromise();

const childResFn = (root, args, context, info = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getPromise } from './promise';

export const isFunction = fn => typeof fn === 'function';
export const isFunction = fn => typeof fn === 'function' || fn instanceof Function;

export const Promisify = fn => {
const Promise = getPromise();
Expand Down