Skip to content

Commit

Permalink
feat(core): allows service types to take schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 12, 2019
1 parent a8babb0 commit d8fe60a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
41 changes: 22 additions & 19 deletions packages/core/src/create/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import {
InputSubscriptionService,
TreeServicesImplementation,
CollectionTreeImplementation,
Observable
Observable,
InputServiceTypes,
ServiceTypesImplementation
} from '~/types';
import { from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { request, response } from './types';
import { isElement } from '~/utils';

export function services<T extends TreeServicesImplementation>(
services: T
Expand All @@ -31,12 +35,7 @@ export function query<I, O>(
return {
...query,
kind: 'query',
types: {
errors: {},
request: { kind: 'request', schema: {} },
response: { kind: 'response', schema: {} },
...(query.types || {})
},
types: parseTypes(query.types),
intercepts: query.intercepts || [],
async resolve(...args: any) {
return query.resolve.apply(this, args);
Expand All @@ -50,12 +49,7 @@ export function mutation<I, O>(
return {
...mutation,
kind: 'mutation',
types: {
errors: {},
request: { kind: 'request', schema: {} },
response: { kind: 'response', schema: {} },
...(mutation.types || {})
},
types: parseTypes(mutation.types),
intercepts: mutation.intercepts || [],
async resolve(...args: any) {
return mutation.resolve.apply(this, args);
Expand All @@ -69,12 +63,7 @@ export function subscription<I, O>(
return {
...subscription,
kind: 'subscription',
types: {
errors: {},
request: { kind: 'request', schema: {} },
response: { kind: 'response', schema: {} },
...(subscription.types || {})
},
types: parseTypes(subscription.types),
intercepts: subscription.intercepts || [],
resolve(...args: any) {
const get = async (): Promise<Observable<O>> => {
Expand All @@ -84,3 +73,17 @@ export function subscription<I, O>(
}
};
}

function parseTypes(types: InputServiceTypes = {}): ServiceTypesImplementation {
return {
errors: types.errors || {},
request:
isElement(types.request) || typeof types.request === 'string'
? types.request
: request({ schema: types.request || {} }),
response:
isElement(types.response) || typeof types.response === 'string'
? types.response
: response({ schema: types.response || {} })
};
}
4 changes: 2 additions & 2 deletions packages/core/src/types/collection/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export interface SubscriptionServiceImplementation<I = any, O = any>

export interface ServiceTypesImplementation extends ServiceTypes {
errors: ServiceErrorsImplementation;
request: RequestTypeImplementation | string;
response: ResponseTypeImplementation | string;
request: string | RequestTypeImplementation;
response: string | ResponseTypeImplementation;
}

export type ServiceErrorsImplementation = ServiceErrors;
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/types/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ServiceTypesImplementation,
ErrorTypeImplementation,
RequestTypeImplementation,
ResponseTypeImplementation,
Expand All @@ -8,24 +7,32 @@ import {
InterceptImplementation
} from './collection';
import { Observable } from './observable';
import { Schema } from './schema';

// Input
// Services
export interface InputQueryService<I = any, O = any> {
types?: ServiceTypesImplementation;
types?: InputServiceTypes;
intercepts?: Array<InterceptImplementation<I, O>>;
resolve: (data: I, context: any) => Promise<O> | O;
}
export interface InputMutationService<I = any, O = any> {
types?: ServiceTypesImplementation;
types?: InputServiceTypes;
intercepts?: Array<InterceptImplementation<I, O>>;
resolve: (data: I, context: any) => Promise<O> | O;
}
export interface InputSubscriptionService<I = any, O = any> {
types?: ServiceTypesImplementation;
types?: InputServiceTypes;
intercepts?: Array<InterceptImplementation<I, O>>;
resolve: (data: I, context: any) => Observable<O> | Promise<Observable<O>>;
}

export interface InputServiceTypes {
errors?: ServiceErrorsImplementation;
request?: string | Schema | RequestTypeImplementation;
response?: string | Schema | ResponseTypeImplementation;
}

// Types
export type InputErrorType = Omit<ErrorTypeImplementation, 'kind'>;
export type InputRequestType = Omit<RequestTypeImplementation, 'kind'>;
export type InputResponseType = Omit<ResponseTypeImplementation, 'kind'>;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export function isTreeCollection(tree: Tree): tree is CollectionTree {
return Object.hasOwnProperty.call(tree, 'types');
}

export function isElement(item: any): item is Element {
return (
typeof item === 'object' &&
item !== null &&
Object.hasOwnProperty.call(item, 'kind')
);
}

export function isElementService(element: Element): element is Service {
return ['query', 'mutation', 'subscription'].includes(element.kind);
}
Expand Down

0 comments on commit d8fe60a

Please sign in to comment.