-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgraphql-meta-field.ts
More file actions
260 lines (237 loc) · 8.41 KB
/
graphql-meta-field.ts
File metadata and controls
260 lines (237 loc) · 8.41 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import {
GraphQLBoolean,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
type GraphQLFieldConfig,
type GraphQLOutputType,
} from 'graphql';
import type { TableMeta } from './types';
type FieldMap = Record<string, unknown>;
function nn<T extends GraphQLOutputType>(type: T): GraphQLNonNull<T> {
return new GraphQLNonNull(type);
}
function nnList<T extends GraphQLOutputType>(
type: T,
): GraphQLNonNull<GraphQLList<GraphQLNonNull<T>>> {
return nn(new GraphQLList(nn(type)));
}
function createMetaSchemaType(): GraphQLObjectType {
const MetaTypeType = new GraphQLObjectType({
name: 'MetaType',
description: 'Information about a PostgreSQL type',
fields: () => ({
pgType: { type: nn(GraphQLString) },
gqlType: { type: nn(GraphQLString) },
isArray: { type: nn(GraphQLBoolean) },
isNotNull: { type: GraphQLBoolean },
hasDefault: { type: GraphQLBoolean },
}),
});
const MetaFieldType = new GraphQLObjectType({
name: 'MetaField',
description: 'Information about a table field/column',
fields: () => ({
name: { type: nn(GraphQLString) },
type: { type: nn(MetaTypeType) },
isNotNull: { type: nn(GraphQLBoolean) },
hasDefault: { type: nn(GraphQLBoolean) },
}),
});
const MetaIndexType = new GraphQLObjectType({
name: 'MetaIndex',
description: 'Information about a database index',
fields: () => ({
name: { type: nn(GraphQLString) },
isUnique: { type: nn(GraphQLBoolean) },
isPrimary: { type: nn(GraphQLBoolean) },
columns: { type: nnList(GraphQLString) },
fields: { type: new GraphQLList(nn(MetaFieldType)) },
}),
});
const MetaPrimaryKeyConstraintType = new GraphQLObjectType({
name: 'MetaPrimaryKeyConstraint',
description: 'Information about a primary key constraint',
fields: () => ({
name: { type: nn(GraphQLString) },
fields: { type: nnList(MetaFieldType) },
}),
});
const MetaUniqueConstraintType = new GraphQLObjectType({
name: 'MetaUniqueConstraint',
description: 'Information about a unique constraint',
fields: () => ({
name: { type: nn(GraphQLString) },
fields: { type: nnList(MetaFieldType) },
}),
});
const MetaRefTableType = new GraphQLObjectType({
name: 'MetaRefTable',
description: 'Reference to a related table',
fields: () => ({
name: { type: nn(GraphQLString) },
}),
});
const MetaForeignKeyConstraintType = new GraphQLObjectType({
name: 'MetaForeignKeyConstraint',
description: 'Information about a foreign key constraint',
fields: () => ({
name: { type: nn(GraphQLString) },
fields: { type: nnList(MetaFieldType) },
referencedTable: { type: nn(GraphQLString) },
referencedFields: { type: nnList(GraphQLString) },
refFields: { type: new GraphQLList(nn(MetaFieldType)) },
refTable: { type: MetaRefTableType },
}),
});
const MetaConstraintsType = new GraphQLObjectType({
name: 'MetaConstraints',
description: 'Table constraints',
fields: () => ({
primaryKey: { type: MetaPrimaryKeyConstraintType },
unique: { type: nnList(MetaUniqueConstraintType) },
foreignKey: { type: nnList(MetaForeignKeyConstraintType) },
}),
});
const MetaInflectionType = new GraphQLObjectType({
name: 'MetaInflection',
description: 'Table inflection names',
fields: () => ({
tableType: { type: nn(GraphQLString) },
allRows: { type: nn(GraphQLString) },
connection: { type: nn(GraphQLString) },
edge: { type: nn(GraphQLString) },
filterType: { type: GraphQLString },
orderByType: { type: nn(GraphQLString) },
conditionType: { type: nn(GraphQLString) },
patchType: { type: GraphQLString },
createInputType: { type: nn(GraphQLString) },
createPayloadType: { type: nn(GraphQLString) },
updatePayloadType: { type: GraphQLString },
deletePayloadType: { type: nn(GraphQLString) },
}),
});
const MetaQueryType = new GraphQLObjectType({
name: 'MetaQuery',
description: 'Table query/mutation names',
fields: () => ({
all: { type: nn(GraphQLString) },
one: { type: GraphQLString },
create: { type: GraphQLString },
update: { type: GraphQLString },
delete: { type: GraphQLString },
}),
});
const MetaBelongsToRelationType = new GraphQLObjectType({
name: 'MetaBelongsToRelation',
description: 'A belongs-to (forward FK) relation',
fields: () => ({
fieldName: { type: GraphQLString },
isUnique: { type: nn(GraphQLBoolean) },
type: { type: GraphQLString },
keys: { type: nnList(MetaFieldType) },
references: { type: nn(MetaRefTableType) },
}),
});
const MetaHasRelationType = new GraphQLObjectType({
name: 'MetaHasRelation',
description: 'A has-one or has-many (reverse FK) relation',
fields: () => ({
fieldName: { type: GraphQLString },
isUnique: { type: nn(GraphQLBoolean) },
type: { type: GraphQLString },
keys: { type: nnList(MetaFieldType) },
referencedBy: { type: nn(MetaRefTableType) },
}),
});
const MetaManyToManyRelationType = new GraphQLObjectType({
name: 'MetaManyToManyRelation',
description: 'A many-to-many relation via junction table',
fields: () => ({
fieldName: { type: GraphQLString },
type: { type: GraphQLString },
junctionTable: { type: nn(MetaRefTableType) },
junctionLeftConstraint: { type: nn(MetaForeignKeyConstraintType) },
junctionLeftKeyAttributes: { type: nnList(MetaFieldType) },
junctionRightConstraint: { type: nn(MetaForeignKeyConstraintType) },
junctionRightKeyAttributes: { type: nnList(MetaFieldType) },
leftKeyAttributes: { type: nnList(MetaFieldType) },
rightKeyAttributes: { type: nnList(MetaFieldType) },
rightTable: { type: nn(MetaRefTableType) },
}),
});
const MetaRelationsType = new GraphQLObjectType({
name: 'MetaRelations',
description: 'Table relations',
fields: () => ({
belongsTo: { type: nnList(MetaBelongsToRelationType) },
has: { type: nnList(MetaHasRelationType) },
hasOne: { type: nnList(MetaHasRelationType) },
hasMany: { type: nnList(MetaHasRelationType) },
manyToMany: { type: nnList(MetaManyToManyRelationType) },
}),
});
const MetaAuthzGrantType = new GraphQLObjectType({
name: 'MetaAuthzGrant',
description: 'A privilege/role grant pair for an authorization policy',
fields: () => ({
privilege: { type: nn(GraphQLString) },
role: { type: nn(GraphQLString) },
}),
});
const MetaAuthzPolicyType = new GraphQLObjectType({
name: 'MetaAuthzPolicy',
description: 'Authorization policy applied to a table',
fields: () => ({
policyType: { type: nn(GraphQLString) },
description: { type: nn(GraphQLString) },
grants: { type: nnList(MetaAuthzGrantType) },
permissive: { type: nn(GraphQLBoolean) },
name: { type: GraphQLString },
}),
});
const MetaTableType = new GraphQLObjectType({
name: 'MetaTable',
description: 'Information about a database table',
fields: () => ({
name: { type: nn(GraphQLString) },
schemaName: { type: nn(GraphQLString) },
fields: { type: nnList(MetaFieldType) },
indexes: { type: nnList(MetaIndexType) },
constraints: { type: nn(MetaConstraintsType) },
foreignKeyConstraints: { type: nnList(MetaForeignKeyConstraintType) },
primaryKeyConstraints: { type: nnList(MetaPrimaryKeyConstraintType) },
uniqueConstraints: { type: nnList(MetaUniqueConstraintType) },
relations: { type: nn(MetaRelationsType) },
inflection: { type: nn(MetaInflectionType) },
query: { type: nn(MetaQueryType) },
authz: { type: new GraphQLList(nn(MetaAuthzPolicyType)) },
}),
});
return new GraphQLObjectType({
name: 'MetaSchema',
description: 'Root meta schema type',
fields: () => ({
tables: { type: nnList(MetaTableType) },
}),
});
}
export function extendQueryWithMetaField(
fields: FieldMap,
tablesMeta: TableMeta[],
): FieldMap {
const metaSchemaType = createMetaSchemaType();
const metaField: GraphQLFieldConfig<unknown, unknown> = {
type: metaSchemaType,
description:
'Metadata about the database schema, including tables, fields, indexes, and constraints. Useful for code generation tools.',
resolve() {
return { tables: tablesMeta };
},
};
return {
...fields,
_meta: metaField,
};
}