-
Notifications
You must be signed in to change notification settings - Fork 38
/
getCombinedDecorator.ts
82 lines (71 loc) · 2.97 KB
/
getCombinedDecorator.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Field as TypeGraphQLField } from 'type-graphql';
import { Column as TypeORMColumn, ColumnType } from 'typeorm';
import { ColumnMetadata, FieldType } from '../metadata';
import { MethodDecoratorFactory } from '../utils';
import { WarthogField } from './WarthogField';
// Combine TypeORM, TypeGraphQL and Warthog decorators
export interface WarthogCombinedDecoratorOptions<T> {
fieldType: FieldType; // This is the warthog field type
warthogColumnMeta: T; // Warthog options like sort, filter, nullable
gqlFieldType?: any; // This is the Type that will land in the GraphQL schmea
dbType?: ColumnType;
dbColumnOptions?: any; // Passed to TypeORM `Column` decorator
}
//
export function getCombinedDecorator<T extends Partial<ColumnMetadata>>({
fieldType,
warthogColumnMeta,
gqlFieldType = String,
dbType = 'varchar',
dbColumnOptions: columnOptions = {}
}: WarthogCombinedDecoratorOptions<T>) {
const nullableOption = warthogColumnMeta.nullable === true ? { nullable: true } : {};
const defaultOption =
typeof warthogColumnMeta.default !== 'undefined' ? { default: warthogColumnMeta.default } : {};
const uniqueOption =
typeof warthogColumnMeta.unique !== 'undefined' ? { unique: warthogColumnMeta.unique } : {};
const tgqlDescriptionOption =
typeof warthogColumnMeta.description !== 'undefined'
? { description: warthogColumnMeta.description }
: {};
const arrayOption = (warthogColumnMeta as any).array ? { array: true } : {};
// TODO: Enable this when TypeORM is fixed: https://github.com/typeorm/typeorm/issues/5906
// const typeOrmColumnOption =
// typeof warthogColumnMeta.description !== 'undefined'
// ? { column: warthogColumnMeta.description }
// : {};
const exposeDB = !warthogColumnMeta.apiOnly;
const exposeAPI = !warthogColumnMeta.dbOnly;
// Warthog: start with the Warthog decorator that adds metadata for generating the GraphQL schema
// for sorting, filtering, args, where inputs, etc...
const decorators: any[] = [];
if (exposeAPI) {
decorators.push(WarthogField(fieldType, warthogColumnMeta));
}
// TypeGraphQL: next add the type-graphql decorator that generates the GraphQL type (or field within that type)
// If an object is only writeable, don't add the `Field` decorators that will add it to the GraphQL type
if (exposeAPI && !warthogColumnMeta.writeonly) {
decorators.push(
TypeGraphQLField(() => gqlFieldType, {
...nullableOption,
...defaultOption,
...tgqlDescriptionOption
})
);
}
// TypeORM: finally add the TypeORM decorator to describe the DB field
if (exposeDB) {
decorators.push(
TypeORMColumn({
type: dbType,
...nullableOption,
...defaultOption,
...columnOptions,
...uniqueOption,
...arrayOption
// ...typeOrmColumnOption: // TODO: Enable this when TypeORM is fixed: https://github.com/typeorm/typeorm/issues/5906
}) as MethodDecoratorFactory
);
}
return decorators;
}