Skip to content

Commit

Permalink
feat(core): adds input types for error, request, response
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Sep 30, 2019
1 parent 95d5a1a commit 734e70c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/create/service/input-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ServiceTypes, TreeTypesImplementation, InputTypes } from '~/types';
import {
ServiceTypes,
TreeTypesImplementation,
InputServiceTypes
} from '~/types';
import { isEnvelope, mergeTypes } from '~/utils';
import camelcase from 'camelcase';

export default function handleInputTypes(
prefix: string,
input: InputTypes = {}
input: InputServiceTypes = {}
): {
names: ServiceTypes;
types?: TreeTypesImplementation;
Expand Down
28 changes: 12 additions & 16 deletions packages/core/src/create/type.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
import {
ErrorCode,
Schema,
QueryServiceImplementation,
SubscriptionServiceImplementation,
ResponseTypeImplementation,
ErrorTypeImplementation,
RequestTypeImplementation,
TreeTypesImplementation,
Envelope
Envelope,
InputErrorType,
InputResponseType,
InputRequestType
} from '~/types';
import { mergeTypes, prefixInlineTypes } from '~/utils';

export function error<N extends string>(
name: N,
code: ErrorCode
error: InputErrorType
): Envelope<ErrorTypeImplementation, N> {
return {
name,
item: {
kind: 'error',
code
code: error.code
}
};
}

export function request<N extends string>(
name: N,
schema: Schema
request: InputRequestType
): Envelope<RequestTypeImplementation, N> {
return {
name,
item: {
kind: 'request',
schema
schema: request.schema
}
};
}

export function response<N extends string>(
name: N,
schema: Schema,
children?: Array<
Envelope<QueryServiceImplementation | SubscriptionServiceImplementation>
>
response: InputResponseType
): Envelope<ResponseTypeImplementation, N> {
const envelope: Envelope<ResponseTypeImplementation, N> = {
name,
item: {
kind: 'response',
schema
schema: response.schema
}
};

if (children) {
if (response.children) {
let inline: TreeTypesImplementation = {};
let types: TreeTypesImplementation = {};
envelope.item.children = {};
for (const child of children) {
for (const child of response.children) {
if (child.inline) inline = mergeTypes(inline, child.inline);
if (child.types) types = mergeTypes(types, child.types);
}
Expand Down
27 changes: 21 additions & 6 deletions packages/core/src/types/create/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
RequestTypeImplementation,
QueryImplementationResolve,
MutationImplementationResolve,
SubscriptionImplementationResolve
SubscriptionImplementationResolve,
QueryServiceImplementation,
SubscriptionServiceImplementation
} from '../collection';
import { Envelope } from './envelope';

Expand All @@ -17,20 +19,19 @@ export type InputService<I = any, O = any> =
| InputSubscriptionService<I, O>;

export interface InputQueryService<I = any, O = any> {
types?: InputTypes;
types?: InputServiceTypes;
resolve: QueryImplementationResolve<I, O>;
}
export interface InputMutationService<I = any, O = any> {
types?: InputTypes;
types?: InputServiceTypes;
resolve: MutationImplementationResolve<I, O>;
}
export interface InputSubscriptionService<I = any, O = any> {
types?: InputTypes;
types?: InputServiceTypes;
resolve: SubscriptionImplementationResolve<I, O>;
}

// Types
export interface InputTypes {
export interface InputServiceTypes {
errors?: Array<
InputInlineError | Envelope<ErrorTypeImplementation, string> | string
>;
Expand All @@ -48,6 +49,20 @@ export type InputInlineError = { name: string; code: ErrorCode };
export type InputInlineRequest = Schema;
export type InputInlineResponse = Schema;

// Types
export interface InputErrorType {
code: ErrorCode;
}
export interface InputRequestType {
schema: Schema;
}
export interface InputResponseType {
schema: Schema;
children?: Array<
Envelope<QueryServiceImplementation | SubscriptionServiceImplementation>
>;
}

// Hooks
export type InputHook = {
errors?: Array<
Expand Down

0 comments on commit 734e70c

Please sign in to comment.