Skip to content

Commit

Permalink
feat(core/transform): adds toUnary
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 18, 2019
1 parent f697ba9 commit 3e4560f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
38 changes: 36 additions & 2 deletions packages/core/src/transform/to.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { CollectionTree, CollectionTreeImplementation, Service } from '~/types';
import {
CollectionTree,
CollectionTreeImplementation,
Service,
UnaryCollection
} from '~/types';
import { replace } from './replace';
import { isElementService, isServiceImplementation } from '~/inspect';
import {
isElementService,
isServiceImplementation,
isServiceSubscription
} from '~/inspect';

export function toImplementation<T extends CollectionTree>(
collection: T,
Expand All @@ -24,3 +33,28 @@ export function toDeclaration(collection: CollectionTree): CollectionTree {
return element;
});
}

export function toUnary<T extends CollectionTree>(
collection: T
): UnaryCollection<T> {
return replace(collection, (element, next) => {
element = next(element);

if (!isElementService(element) || !isServiceSubscription(element)) {
return element;
}

if (!isServiceImplementation(element)) {
return { ...element, kind: 'query' };
}

const resolve: any = element.resolve;
return {
...element,
kind: 'query',
resolve(...args: any): Promise<any> {
return resolve.apply(this, args).toPromise();
}
};
}) as UnaryCollection<T>;
}
26 changes: 19 additions & 7 deletions packages/core/src/types/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
import {
CollectionTreeImplementation,
ServiceImplementation,
ScopeTreeImplementation
ScopeTreeImplementation,
QueryServiceImplementation,
MutationServiceImplementation
} from './implementation';
import { GenericError } from './types';

Expand All @@ -20,6 +22,22 @@ export type GenericCollection<
? CollectionTreeImplementation
: CollectionTree;

export type UnaryCollection<
T extends CollectionTree
> = T extends CollectionTreeImplementation
? CollectionTree<
QueryServiceImplementation,
MutationServiceImplementation,
never
>
: CollectionTree<QueryService, MutationService, never>;

export type ApplicationCollection<
T extends CollectionTree = CollectionTree
> = T & {
types: { [P in GenericError]: ErrorType };
};

export type GenericService<
T extends CollectionTree
> = T extends CollectionTreeImplementation ? ServiceImplementation : Service;
Expand All @@ -33,12 +51,6 @@ export interface ServicesRoutes<T extends CollectionTree = CollectionTree>
[key: string]: GenericService<T>;
}

export type ApplicationCollection<
T extends CollectionTree = CollectionTree
> = T & {
types: { [P in GenericError]: ErrorType };
};

export type ScopeCollection<
T extends CollectionTree,
N extends string
Expand Down

0 comments on commit 3e4560f

Please sign in to comment.