forked from goldcaddy77/warthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomField.ts
42 lines (35 loc) · 1.56 KB
/
CustomField.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Field } from 'type-graphql';
import { AdvancedOptions } from 'type-graphql/dist/decorators/types';
import { Column, ColumnType } from 'typeorm';
import { ColumnOptions } from 'typeorm/decorator/options/ColumnOptions';
import { decoratorDefaults, FieldType } from '../metadata';
import { columnTypeToGraphQLType } from '../schema';
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils';
import { WarthogField } from './WarthogField';
interface CustomColumnOptions extends ColumnOptions {
type: ColumnType; // we require this
}
interface ExtendedTypeGraphQLOptions extends AdvancedOptions {
type: FieldType;
filter?: boolean;
nullable?: boolean;
sort?: boolean;
}
interface CustomFieldOptions {
// nullable?: boolean; // need to decide if we should add this shortcut
api: ExtendedTypeGraphQLOptions;
db: CustomColumnOptions;
}
export function CustomField(args: CustomFieldOptions): any {
// const nullableOption = typeof args.nullable !== 'undefined' ? { nullable: args.nullable } : {};
// const dbOptions = { ...nullableOption, ...(args.db || {}) };
const { type, filter, sort, ...typeGraphQLOptions } = args.api;
const warthogOptions = { ...decoratorDefaults, nullable: args.api.nullable, type, filter, sort };
// These are the 2 required decorators to get type-graphql and typeorm working
const factories = [
WarthogField(args.api.type, warthogOptions),
Field(() => columnTypeToGraphQLType(args.api.type), typeGraphQLOptions),
Column(args.db) as MethodDecoratorFactory
];
return composeMethodDecorators(...factories);
}