-
Notifications
You must be signed in to change notification settings - Fork 46
/
Field.ts
84 lines (70 loc) · 1.82 KB
/
Field.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
83
84
import { Relation } from '.'
import { availableTypes } from './ScalarTypes'
import { ModelType } from './WarthogModel'
interface DerivedFrom {
argument: string
}
/**
* Reperenst GraphQL object type field
* @constructor(name: string, type: string, nullable: boolean = true, isBuildinType: boolean = true, isList = false)
*/
export class Field {
// GraphQL field name
name: string
// GraphQL field type
type: string
// type in the model (SCALAR, ENUM, INTERFACE, ENTITY, VARIANT)
modelType!: ModelType
// Is field type built-in or not
isBuildinType: boolean
// Is field nullable or not
nullable: boolean
// Is field a list. eg: post: [Post]
isList: boolean
// Description of the field will be shown in GrapqQL API
description?: string
// Make field as a unique column on database
unique?: boolean
// Relation
relation?: Relation
derivedFrom?: DerivedFrom
apiOnly?: boolean
constructor(
name: string,
type: string,
nullable = true,
isBuildinType = true,
isList = false
) {
this.name = name
this.type = type
this.nullable = nullable
this.isBuildinType = isBuildinType
this.isList = isList
}
// get isBuiltInType(): boolean {
// return this.modelType == ModelType.SCALAR
// }
columnType(): string {
if (this.relation) return this.relation?.type
return this.isBuildinType ? availableTypes[this.type] : this.type
}
isArray(): boolean {
return this.isBuildinType && this.isList
}
isScalar(): boolean {
return this.isBuildinType && !this.isList
}
isRelationType(): boolean {
return !!this.relation
}
isEnum(): boolean {
return this.modelType === ModelType.ENUM
}
isUnion(): boolean {
return this.modelType === ModelType.UNION
}
isEntity(): boolean {
return this.modelType === ModelType.ENTITY
}
}