@constructive-io/graphql-codegen 2.19.0 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1818 -113
- package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/__tests__/codegen/input-types-generator.test.js +635 -0
- package/cli/codegen/barrel.d.ts +27 -0
- package/cli/codegen/barrel.js +163 -0
- package/cli/codegen/client.d.ts +4 -0
- package/cli/codegen/client.js +170 -0
- package/cli/codegen/custom-mutations.d.ts +38 -0
- package/cli/codegen/custom-mutations.js +149 -0
- package/cli/codegen/custom-queries.d.ts +38 -0
- package/cli/codegen/custom-queries.js +358 -0
- package/cli/codegen/filters.d.ts +27 -0
- package/cli/codegen/filters.js +357 -0
- package/cli/codegen/gql-ast.d.ts +41 -0
- package/cli/codegen/gql-ast.js +329 -0
- package/cli/codegen/index.d.ts +71 -0
- package/cli/codegen/index.js +147 -0
- package/cli/codegen/mutations.d.ts +30 -0
- package/cli/codegen/mutations.js +410 -0
- package/cli/codegen/orm/barrel.d.ts +18 -0
- package/cli/codegen/orm/barrel.js +48 -0
- package/cli/codegen/orm/client-generator.d.ts +45 -0
- package/cli/codegen/orm/client-generator.js +646 -0
- package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/cli/codegen/orm/custom-ops-generator.js +350 -0
- package/cli/codegen/orm/index.d.ts +38 -0
- package/cli/codegen/orm/index.js +88 -0
- package/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/cli/codegen/orm/input-types-generator.js +705 -0
- package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/cli/codegen/orm/input-types-generator.test.js +75 -0
- package/cli/codegen/orm/model-generator.d.ts +32 -0
- package/cli/codegen/orm/model-generator.js +264 -0
- package/cli/codegen/orm/query-builder.d.ts +161 -0
- package/cli/codegen/orm/query-builder.js +366 -0
- package/cli/codegen/orm/select-types.d.ts +169 -0
- package/cli/codegen/orm/select-types.js +16 -0
- package/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/cli/codegen/orm/select-types.test.js +22 -0
- package/cli/codegen/queries.d.ts +25 -0
- package/cli/codegen/queries.js +438 -0
- package/cli/codegen/scalars.d.ts +12 -0
- package/cli/codegen/scalars.js +71 -0
- package/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/cli/codegen/schema-gql-ast.js +385 -0
- package/cli/codegen/ts-ast.d.ts +122 -0
- package/cli/codegen/ts-ast.js +280 -0
- package/cli/codegen/type-resolver.d.ts +96 -0
- package/cli/codegen/type-resolver.js +246 -0
- package/cli/codegen/types.d.ts +12 -0
- package/cli/codegen/types.js +69 -0
- package/cli/codegen/utils.d.ts +163 -0
- package/cli/codegen/utils.js +326 -0
- package/cli/commands/generate-orm.d.ts +37 -0
- package/cli/commands/generate-orm.js +195 -0
- package/cli/commands/generate.d.ts +39 -0
- package/cli/commands/generate.js +299 -0
- package/cli/commands/index.d.ts +7 -0
- package/cli/commands/index.js +12 -0
- package/cli/commands/init.d.ts +35 -0
- package/cli/commands/init.js +176 -0
- package/cli/index.d.ts +4 -0
- package/cli/index.js +291 -0
- package/cli/introspect/fetch-meta.d.ts +31 -0
- package/cli/introspect/fetch-meta.js +108 -0
- package/cli/introspect/fetch-schema.d.ts +21 -0
- package/cli/introspect/fetch-schema.js +86 -0
- package/cli/introspect/index.d.ts +8 -0
- package/cli/introspect/index.js +16 -0
- package/cli/introspect/meta-query.d.ts +111 -0
- package/cli/introspect/meta-query.js +191 -0
- package/cli/introspect/schema-query.d.ts +20 -0
- package/cli/introspect/schema-query.js +123 -0
- package/cli/introspect/transform-schema.d.ts +74 -0
- package/cli/introspect/transform-schema.js +269 -0
- package/cli/introspect/transform-schema.test.d.ts +1 -0
- package/cli/introspect/transform-schema.test.js +67 -0
- package/cli/introspect/transform.d.ts +21 -0
- package/cli/introspect/transform.js +216 -0
- package/cli/watch/cache.d.ts +45 -0
- package/cli/watch/cache.js +111 -0
- package/cli/watch/debounce.d.ts +19 -0
- package/cli/watch/debounce.js +89 -0
- package/cli/watch/hash.d.ts +17 -0
- package/cli/watch/hash.js +48 -0
- package/cli/watch/index.d.ts +10 -0
- package/cli/watch/index.js +22 -0
- package/cli/watch/orchestrator.d.ts +63 -0
- package/cli/watch/orchestrator.js +228 -0
- package/cli/watch/poller.d.ts +65 -0
- package/cli/watch/poller.js +203 -0
- package/cli/watch/types.d.ts +67 -0
- package/cli/watch/types.js +5 -0
- package/client/error.d.ts +95 -0
- package/client/error.js +255 -0
- package/client/execute.d.ts +57 -0
- package/client/execute.js +124 -0
- package/client/index.d.ts +6 -0
- package/client/index.js +18 -0
- package/client/typed-document.d.ts +31 -0
- package/client/typed-document.js +44 -0
- package/core/ast.d.ts +10 -0
- package/core/ast.js +593 -0
- package/core/custom-ast.d.ts +35 -0
- package/core/custom-ast.js +204 -0
- package/core/index.d.ts +8 -0
- package/core/index.js +33 -0
- package/core/meta-object/convert.d.ts +65 -0
- package/core/meta-object/convert.js +63 -0
- package/core/meta-object/format.json +93 -0
- package/core/meta-object/index.d.ts +2 -0
- package/core/meta-object/index.js +18 -0
- package/core/meta-object/validate.d.ts +9 -0
- package/core/meta-object/validate.js +34 -0
- package/core/query-builder.d.ts +46 -0
- package/core/query-builder.js +412 -0
- package/core/types.d.ts +139 -0
- package/core/types.js +28 -0
- package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
- package/esm/cli/codegen/barrel.d.ts +27 -0
- package/esm/cli/codegen/barrel.js +156 -0
- package/esm/cli/codegen/client.d.ts +4 -0
- package/esm/cli/codegen/client.js +167 -0
- package/esm/cli/codegen/custom-mutations.d.ts +38 -0
- package/esm/cli/codegen/custom-mutations.js +145 -0
- package/esm/cli/codegen/custom-queries.d.ts +38 -0
- package/esm/cli/codegen/custom-queries.js +354 -0
- package/esm/cli/codegen/filters.d.ts +27 -0
- package/esm/cli/codegen/filters.js +351 -0
- package/esm/cli/codegen/gql-ast.d.ts +41 -0
- package/esm/cli/codegen/gql-ast.js +288 -0
- package/esm/cli/codegen/index.d.ts +71 -0
- package/esm/cli/codegen/index.js +124 -0
- package/esm/cli/codegen/mutations.d.ts +30 -0
- package/esm/cli/codegen/mutations.js +404 -0
- package/esm/cli/codegen/orm/barrel.d.ts +18 -0
- package/esm/cli/codegen/orm/barrel.js +44 -0
- package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
- package/esm/cli/codegen/orm/client-generator.js +640 -0
- package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
- package/esm/cli/codegen/orm/index.d.ts +38 -0
- package/esm/cli/codegen/orm/index.js +75 -0
- package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/esm/cli/codegen/orm/input-types-generator.js +700 -0
- package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
- package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
- package/esm/cli/codegen/orm/model-generator.js +260 -0
- package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
- package/esm/cli/codegen/orm/query-builder.js +353 -0
- package/esm/cli/codegen/orm/select-types.d.ts +169 -0
- package/esm/cli/codegen/orm/select-types.js +15 -0
- package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/esm/cli/codegen/orm/select-types.test.js +21 -0
- package/esm/cli/codegen/queries.d.ts +25 -0
- package/esm/cli/codegen/queries.js +433 -0
- package/esm/cli/codegen/scalars.d.ts +12 -0
- package/esm/cli/codegen/scalars.js +66 -0
- package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/esm/cli/codegen/schema-gql-ast.js +343 -0
- package/esm/cli/codegen/ts-ast.d.ts +122 -0
- package/esm/cli/codegen/ts-ast.js +260 -0
- package/esm/cli/codegen/type-resolver.d.ts +96 -0
- package/esm/cli/codegen/type-resolver.js +224 -0
- package/esm/cli/codegen/types.d.ts +12 -0
- package/esm/cli/codegen/types.js +65 -0
- package/esm/cli/codegen/utils.d.ts +163 -0
- package/esm/cli/codegen/utils.js +288 -0
- package/esm/cli/commands/generate-orm.d.ts +37 -0
- package/esm/cli/commands/generate-orm.js +192 -0
- package/esm/cli/commands/generate.d.ts +39 -0
- package/esm/cli/commands/generate.js +262 -0
- package/esm/cli/commands/index.d.ts +7 -0
- package/esm/cli/commands/index.js +5 -0
- package/esm/cli/commands/init.d.ts +35 -0
- package/esm/cli/commands/init.js +138 -0
- package/esm/cli/index.d.ts +4 -0
- package/esm/cli/index.js +256 -0
- package/esm/cli/introspect/fetch-meta.d.ts +31 -0
- package/esm/cli/introspect/fetch-meta.js +104 -0
- package/esm/cli/introspect/fetch-schema.d.ts +21 -0
- package/esm/cli/introspect/fetch-schema.js +83 -0
- package/esm/cli/introspect/index.d.ts +8 -0
- package/esm/cli/introspect/index.js +6 -0
- package/esm/cli/introspect/meta-query.d.ts +111 -0
- package/esm/cli/introspect/meta-query.js +188 -0
- package/esm/cli/introspect/schema-query.d.ts +20 -0
- package/esm/cli/introspect/schema-query.js +120 -0
- package/esm/cli/introspect/transform-schema.d.ts +74 -0
- package/esm/cli/introspect/transform-schema.js +259 -0
- package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
- package/esm/cli/introspect/transform-schema.test.js +65 -0
- package/esm/cli/introspect/transform.d.ts +21 -0
- package/esm/cli/introspect/transform.js +210 -0
- package/esm/cli/watch/cache.d.ts +45 -0
- package/esm/cli/watch/cache.js +73 -0
- package/esm/cli/watch/debounce.d.ts +19 -0
- package/esm/cli/watch/debounce.js +85 -0
- package/esm/cli/watch/hash.d.ts +17 -0
- package/esm/cli/watch/hash.js +43 -0
- package/esm/cli/watch/index.d.ts +10 -0
- package/esm/cli/watch/index.js +8 -0
- package/esm/cli/watch/orchestrator.d.ts +63 -0
- package/esm/cli/watch/orchestrator.js +223 -0
- package/esm/cli/watch/poller.d.ts +65 -0
- package/esm/cli/watch/poller.js +198 -0
- package/esm/cli/watch/types.d.ts +67 -0
- package/esm/cli/watch/types.js +4 -0
- package/esm/client/error.d.ts +95 -0
- package/esm/client/error.js +249 -0
- package/esm/client/execute.d.ts +57 -0
- package/esm/client/execute.js +120 -0
- package/esm/client/index.d.ts +6 -0
- package/esm/client/index.js +6 -0
- package/esm/client/typed-document.d.ts +31 -0
- package/esm/client/typed-document.js +40 -0
- package/esm/core/ast.d.ts +10 -0
- package/esm/core/ast.js +549 -0
- package/esm/core/custom-ast.d.ts +35 -0
- package/esm/core/custom-ast.js +161 -0
- package/esm/core/index.d.ts +8 -0
- package/esm/core/index.js +12 -0
- package/esm/core/meta-object/convert.d.ts +65 -0
- package/esm/core/meta-object/convert.js +60 -0
- package/esm/core/meta-object/format.json +93 -0
- package/esm/core/meta-object/index.d.ts +2 -0
- package/esm/core/meta-object/index.js +2 -0
- package/esm/core/meta-object/validate.d.ts +9 -0
- package/esm/core/meta-object/validate.js +28 -0
- package/esm/core/query-builder.d.ts +46 -0
- package/esm/core/query-builder.js +375 -0
- package/esm/core/types.d.ts +139 -0
- package/esm/core/types.js +24 -0
- package/esm/generators/field-selector.d.ts +30 -0
- package/esm/generators/field-selector.js +355 -0
- package/esm/generators/index.d.ts +6 -0
- package/esm/generators/index.js +9 -0
- package/esm/generators/mutations.d.ts +31 -0
- package/esm/generators/mutations.js +197 -0
- package/esm/generators/select.d.ts +50 -0
- package/esm/generators/select.js +636 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +17 -3
- package/esm/react/index.d.ts +5 -0
- package/esm/react/index.js +6 -0
- package/esm/types/config.d.ts +199 -0
- package/esm/types/config.js +106 -0
- package/esm/types/index.d.ts +9 -0
- package/esm/types/index.js +4 -0
- package/esm/types/introspection.d.ts +121 -0
- package/esm/types/introspection.js +54 -0
- package/esm/types/mutation.d.ts +45 -0
- package/esm/types/mutation.js +4 -0
- package/esm/types/query.d.ts +82 -0
- package/esm/types/query.js +4 -0
- package/esm/types/schema.d.ts +253 -0
- package/esm/types/schema.js +5 -0
- package/esm/types/selection.d.ts +43 -0
- package/esm/types/selection.js +4 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/generators/field-selector.d.ts +30 -0
- package/generators/field-selector.js +361 -0
- package/generators/index.d.ts +6 -0
- package/generators/index.js +27 -0
- package/generators/mutations.d.ts +31 -0
- package/generators/mutations.js +235 -0
- package/generators/select.d.ts +50 -0
- package/generators/select.js +679 -0
- package/index.d.ts +12 -3
- package/index.js +19 -3
- package/package.json +59 -38
- package/react/index.d.ts +5 -0
- package/react/index.js +9 -0
- package/types/config.d.ts +199 -0
- package/types/config.js +111 -0
- package/types/index.d.ts +9 -0
- package/types/index.js +10 -0
- package/types/introspection.d.ts +121 -0
- package/types/introspection.js +62 -0
- package/types/mutation.d.ts +45 -0
- package/types/mutation.js +5 -0
- package/types/query.d.ts +82 -0
- package/types/query.js +5 -0
- package/types/schema.d.ts +253 -0
- package/types/schema.js +6 -0
- package/types/selection.d.ts +43 -0
- package/types/selection.js +5 -0
- package/utils/index.d.ts +4 -0
- package/utils/index.js +7 -0
- package/codegen.d.ts +0 -13
- package/codegen.js +0 -293
- package/esm/codegen.js +0 -253
- package/esm/gql.js +0 -939
- package/esm/options.js +0 -27
- package/gql.d.ts +0 -188
- package/gql.js +0 -992
- package/options.d.ts +0 -45
- package/options.js +0 -31
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import * as t from 'gql-ast';
|
|
2
|
+
/**
|
|
3
|
+
* Get custom AST for MetaField type - handles PostgreSQL types that need subfield selections
|
|
4
|
+
*/
|
|
5
|
+
export function getCustomAst(fieldDefn) {
|
|
6
|
+
if (!fieldDefn) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const { pgType } = fieldDefn.type;
|
|
10
|
+
if (pgType === 'geometry') {
|
|
11
|
+
return geometryAst(fieldDefn.name);
|
|
12
|
+
}
|
|
13
|
+
if (pgType === 'interval') {
|
|
14
|
+
return intervalAst(fieldDefn.name);
|
|
15
|
+
}
|
|
16
|
+
return t.field({
|
|
17
|
+
name: fieldDefn.name,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Generate custom AST for CleanField type - handles GraphQL types that need subfield selections
|
|
22
|
+
*/
|
|
23
|
+
export function getCustomAstForCleanField(field) {
|
|
24
|
+
const { name, type } = field;
|
|
25
|
+
const { gqlType, pgType } = type;
|
|
26
|
+
// Handle by GraphQL type first (this is what the error messages reference)
|
|
27
|
+
if (gqlType === 'GeometryPoint') {
|
|
28
|
+
return geometryPointAst(name);
|
|
29
|
+
}
|
|
30
|
+
if (gqlType === 'Interval') {
|
|
31
|
+
return intervalAst(name);
|
|
32
|
+
}
|
|
33
|
+
if (gqlType === 'GeometryGeometryCollection') {
|
|
34
|
+
return geometryCollectionAst(name);
|
|
35
|
+
}
|
|
36
|
+
// Handle by pgType as fallback
|
|
37
|
+
if (pgType === 'geometry') {
|
|
38
|
+
return geometryAst(name);
|
|
39
|
+
}
|
|
40
|
+
if (pgType === 'interval') {
|
|
41
|
+
return intervalAst(name);
|
|
42
|
+
}
|
|
43
|
+
// Return simple field for scalar types
|
|
44
|
+
return t.field({
|
|
45
|
+
name,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Check if a CleanField requires subfield selection based on its GraphQL type
|
|
50
|
+
*/
|
|
51
|
+
export function requiresSubfieldSelection(field) {
|
|
52
|
+
const { gqlType } = field.type;
|
|
53
|
+
// Complex GraphQL types that require subfield selection
|
|
54
|
+
const complexTypes = [
|
|
55
|
+
'GeometryPoint',
|
|
56
|
+
'Interval',
|
|
57
|
+
'GeometryGeometryCollection',
|
|
58
|
+
'GeoJSON',
|
|
59
|
+
];
|
|
60
|
+
return complexTypes.includes(gqlType);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Generate AST for GeometryPoint type
|
|
64
|
+
*/
|
|
65
|
+
export function geometryPointAst(name) {
|
|
66
|
+
return t.field({
|
|
67
|
+
name,
|
|
68
|
+
selectionSet: t.selectionSet({
|
|
69
|
+
selections: toFieldArray(['x', 'y']),
|
|
70
|
+
}),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Generate AST for GeometryGeometryCollection type
|
|
75
|
+
*/
|
|
76
|
+
export function geometryCollectionAst(name) {
|
|
77
|
+
// Manually create inline fragment since gql-ast doesn't support it
|
|
78
|
+
const inlineFragment = {
|
|
79
|
+
kind: 'InlineFragment',
|
|
80
|
+
typeCondition: {
|
|
81
|
+
kind: 'NamedType',
|
|
82
|
+
name: {
|
|
83
|
+
kind: 'Name',
|
|
84
|
+
value: 'GeometryPoint',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
selectionSet: {
|
|
88
|
+
kind: 'SelectionSet',
|
|
89
|
+
selections: [
|
|
90
|
+
{
|
|
91
|
+
kind: 'Field',
|
|
92
|
+
name: {
|
|
93
|
+
kind: 'Name',
|
|
94
|
+
value: 'x',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
kind: 'Field',
|
|
99
|
+
name: {
|
|
100
|
+
kind: 'Name',
|
|
101
|
+
value: 'y',
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
return t.field({
|
|
108
|
+
name,
|
|
109
|
+
selectionSet: t.selectionSet({
|
|
110
|
+
selections: [
|
|
111
|
+
t.field({
|
|
112
|
+
name: 'geometries',
|
|
113
|
+
selectionSet: t.selectionSet({
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
115
|
+
selections: [inlineFragment], // gql-ast limitation with inline fragments
|
|
116
|
+
}),
|
|
117
|
+
}),
|
|
118
|
+
],
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Generate AST for generic geometry type (returns geojson)
|
|
124
|
+
*/
|
|
125
|
+
export function geometryAst(name) {
|
|
126
|
+
return t.field({
|
|
127
|
+
name,
|
|
128
|
+
selectionSet: t.selectionSet({
|
|
129
|
+
selections: toFieldArray(['geojson']),
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Generate AST for interval type
|
|
135
|
+
*/
|
|
136
|
+
export function intervalAst(name) {
|
|
137
|
+
return t.field({
|
|
138
|
+
name,
|
|
139
|
+
selectionSet: t.selectionSet({
|
|
140
|
+
selections: toFieldArray([
|
|
141
|
+
'days',
|
|
142
|
+
'hours',
|
|
143
|
+
'minutes',
|
|
144
|
+
'months',
|
|
145
|
+
'seconds',
|
|
146
|
+
'years',
|
|
147
|
+
]),
|
|
148
|
+
}),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function toFieldArray(strArr) {
|
|
152
|
+
return strArr.map((fieldName) => t.field({ name: fieldName }));
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check if an object has interval type shape
|
|
156
|
+
*/
|
|
157
|
+
export function isIntervalType(obj) {
|
|
158
|
+
if (!obj || typeof obj !== 'object')
|
|
159
|
+
return false;
|
|
160
|
+
return ['days', 'hours', 'minutes', 'months', 'seconds', 'years'].every((key) => Object.prototype.hasOwnProperty.call(obj, key));
|
|
161
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core query building exports
|
|
3
|
+
*/
|
|
4
|
+
// Types
|
|
5
|
+
export * from './types';
|
|
6
|
+
// AST generation
|
|
7
|
+
export * from './ast';
|
|
8
|
+
export * from './custom-ast';
|
|
9
|
+
// Query builder
|
|
10
|
+
export { QueryBuilder, MetaObject } from './query-builder';
|
|
11
|
+
// Meta object utilities
|
|
12
|
+
export { validateMetaObject, convertFromMetaSchema } from './meta-object';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { MetaFieldType } from '../types';
|
|
2
|
+
interface MetaSchemaField {
|
|
3
|
+
name: string;
|
|
4
|
+
type: MetaFieldType;
|
|
5
|
+
}
|
|
6
|
+
interface MetaSchemaConstraint {
|
|
7
|
+
fields: MetaSchemaField[];
|
|
8
|
+
}
|
|
9
|
+
interface MetaSchemaForeignConstraint {
|
|
10
|
+
fields: MetaSchemaField[];
|
|
11
|
+
refFields: MetaSchemaField[];
|
|
12
|
+
refTable: {
|
|
13
|
+
name: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface MetaSchemaBelongsTo {
|
|
17
|
+
keys: MetaSchemaField[];
|
|
18
|
+
fieldName: string;
|
|
19
|
+
}
|
|
20
|
+
interface MetaSchemaRelations {
|
|
21
|
+
belongsTo: MetaSchemaBelongsTo[];
|
|
22
|
+
}
|
|
23
|
+
interface MetaSchemaTable {
|
|
24
|
+
name: string;
|
|
25
|
+
fields: MetaSchemaField[];
|
|
26
|
+
primaryKeyConstraints: MetaSchemaConstraint[];
|
|
27
|
+
uniqueConstraints: MetaSchemaConstraint[];
|
|
28
|
+
foreignKeyConstraints: MetaSchemaForeignConstraint[];
|
|
29
|
+
relations: MetaSchemaRelations;
|
|
30
|
+
}
|
|
31
|
+
interface MetaSchemaInput {
|
|
32
|
+
_meta: {
|
|
33
|
+
tables: MetaSchemaTable[];
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface ConvertedField {
|
|
37
|
+
name: string;
|
|
38
|
+
type: MetaFieldType;
|
|
39
|
+
alias?: string;
|
|
40
|
+
}
|
|
41
|
+
interface ConvertedConstraint {
|
|
42
|
+
name: string;
|
|
43
|
+
type: MetaFieldType;
|
|
44
|
+
alias?: string;
|
|
45
|
+
}
|
|
46
|
+
interface ConvertedForeignConstraint {
|
|
47
|
+
refTable: string;
|
|
48
|
+
fromKey: ConvertedField;
|
|
49
|
+
toKey: ConvertedField;
|
|
50
|
+
}
|
|
51
|
+
interface ConvertedTable {
|
|
52
|
+
name: string;
|
|
53
|
+
fields: ConvertedField[];
|
|
54
|
+
primaryConstraints: ConvertedConstraint[];
|
|
55
|
+
uniqueConstraints: ConvertedConstraint[];
|
|
56
|
+
foreignConstraints: ConvertedForeignConstraint[];
|
|
57
|
+
}
|
|
58
|
+
interface ConvertedMetaObject {
|
|
59
|
+
tables: ConvertedTable[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert from raw _meta schema response to internal MetaObject format
|
|
63
|
+
*/
|
|
64
|
+
export declare function convertFromMetaSchema(metaSchema: MetaSchemaInput): ConvertedMetaObject;
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert from raw _meta schema response to internal MetaObject format
|
|
3
|
+
*/
|
|
4
|
+
export function convertFromMetaSchema(metaSchema) {
|
|
5
|
+
const { _meta: { tables }, } = metaSchema;
|
|
6
|
+
const result = {
|
|
7
|
+
tables: [],
|
|
8
|
+
};
|
|
9
|
+
for (const table of tables) {
|
|
10
|
+
result.tables.push({
|
|
11
|
+
name: table.name,
|
|
12
|
+
fields: table.fields.map((f) => pickField(f)),
|
|
13
|
+
primaryConstraints: pickArrayConstraint(table.primaryKeyConstraints),
|
|
14
|
+
uniqueConstraints: pickArrayConstraint(table.uniqueConstraints),
|
|
15
|
+
foreignConstraints: pickForeignConstraint(table.foreignKeyConstraints, table.relations),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
function pickArrayConstraint(constraints) {
|
|
21
|
+
if (constraints.length === 0)
|
|
22
|
+
return [];
|
|
23
|
+
const c = constraints[0];
|
|
24
|
+
return c.fields.map((field) => pickConstraintField(field));
|
|
25
|
+
}
|
|
26
|
+
function pickForeignConstraint(constraints, relations) {
|
|
27
|
+
if (constraints.length === 0)
|
|
28
|
+
return [];
|
|
29
|
+
const { belongsTo } = relations;
|
|
30
|
+
return constraints.map((c) => {
|
|
31
|
+
const { fields, refFields, refTable } = c;
|
|
32
|
+
const fromKey = pickField(fields[0]);
|
|
33
|
+
const toKey = pickField(refFields[0]);
|
|
34
|
+
const matchingBelongsTo = belongsTo.find((belongsToItem) => {
|
|
35
|
+
const field = pickField(belongsToItem.keys[0]);
|
|
36
|
+
return field.name === fromKey.name;
|
|
37
|
+
});
|
|
38
|
+
// Ex: 'ownerId' will have an alias of 'owner', which has further selection of 'User' type
|
|
39
|
+
if (matchingBelongsTo) {
|
|
40
|
+
fromKey.alias = matchingBelongsTo.fieldName;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
refTable: refTable.name,
|
|
44
|
+
fromKey,
|
|
45
|
+
toKey,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function pickField(field) {
|
|
50
|
+
return {
|
|
51
|
+
name: field.name,
|
|
52
|
+
type: field.type,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function pickConstraintField(field) {
|
|
56
|
+
return {
|
|
57
|
+
name: field.name,
|
|
58
|
+
type: field.type,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$id": "root",
|
|
3
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"definitions": {
|
|
6
|
+
"field": {
|
|
7
|
+
"type": "object",
|
|
8
|
+
"required": ["name", "type"],
|
|
9
|
+
"additionalProperties": true,
|
|
10
|
+
"properties": {
|
|
11
|
+
"name": {
|
|
12
|
+
"type": "string",
|
|
13
|
+
"default": "id",
|
|
14
|
+
"examples": ["id"]
|
|
15
|
+
},
|
|
16
|
+
"type": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"required": ["pgType", "gqlType"],
|
|
19
|
+
"additionalProperties": true,
|
|
20
|
+
"properties": {
|
|
21
|
+
"gqlType": {
|
|
22
|
+
"type": ["string", "null"]
|
|
23
|
+
},
|
|
24
|
+
"pgType": {
|
|
25
|
+
"type": ["string", "null"]
|
|
26
|
+
},
|
|
27
|
+
"subtype": {
|
|
28
|
+
"type": ["string", "null"]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"properties": {
|
|
36
|
+
"tables": {
|
|
37
|
+
"type": "array",
|
|
38
|
+
"items": {
|
|
39
|
+
"type": "object",
|
|
40
|
+
"properties": {
|
|
41
|
+
"name": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"default": "User",
|
|
44
|
+
"examples": ["User"]
|
|
45
|
+
},
|
|
46
|
+
"fields": {
|
|
47
|
+
"type": "array",
|
|
48
|
+
"items": {
|
|
49
|
+
"$ref": "#/definitions/field"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"primaryConstraints": {
|
|
53
|
+
"type": "array",
|
|
54
|
+
"items": {
|
|
55
|
+
"$ref": "#/definitions/field"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"uniqueConstraints": {
|
|
59
|
+
"type": "array",
|
|
60
|
+
"items": {
|
|
61
|
+
"$ref": "#/definitions/field"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"foreignConstraints": {
|
|
65
|
+
"type": "array",
|
|
66
|
+
"items": {
|
|
67
|
+
"type": "object",
|
|
68
|
+
"properties": {
|
|
69
|
+
"refTable": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"default": "User",
|
|
72
|
+
"examples": ["User"]
|
|
73
|
+
},
|
|
74
|
+
"fromKey": {
|
|
75
|
+
"$ref": "#/definitions/field"
|
|
76
|
+
},
|
|
77
|
+
"toKey": {
|
|
78
|
+
"$ref": "#/definitions/field"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"required": ["refTable", "fromKey", "toKey"],
|
|
82
|
+
"additionalProperties": false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"required": ["name", "fields"],
|
|
87
|
+
"additionalProperties": false
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"required": ["tables"],
|
|
92
|
+
"additionalProperties": false
|
|
93
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface ValidationResult {
|
|
2
|
+
errors: unknown[] | null | undefined;
|
|
3
|
+
message: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validate a MetaObject against the JSON schema
|
|
7
|
+
* @returns true if valid, or an object with errors and message if invalid
|
|
8
|
+
*/
|
|
9
|
+
export declare function validateMetaObject(obj: unknown): true | ValidationResult;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Ajv from 'ajv';
|
|
2
|
+
import format from './format.json';
|
|
3
|
+
let cachedAjv = null;
|
|
4
|
+
let cachedValidator = null;
|
|
5
|
+
function getValidator() {
|
|
6
|
+
if (!cachedAjv) {
|
|
7
|
+
cachedAjv = new Ajv({ allErrors: true });
|
|
8
|
+
cachedValidator = cachedAjv.compile(format);
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
ajv: cachedAjv,
|
|
12
|
+
validator: cachedValidator,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Validate a MetaObject against the JSON schema
|
|
17
|
+
* @returns true if valid, or an object with errors and message if invalid
|
|
18
|
+
*/
|
|
19
|
+
export function validateMetaObject(obj) {
|
|
20
|
+
const { ajv, validator } = getValidator();
|
|
21
|
+
const valid = validator(obj);
|
|
22
|
+
if (valid)
|
|
23
|
+
return true;
|
|
24
|
+
return {
|
|
25
|
+
errors: validator.errors,
|
|
26
|
+
message: ajv.errorsText(validator.errors, { separator: '\n' }),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { QueryFieldSelection, IntrospectionSchema, MetaObject, QueryBuilderOptions, QueryBuilderResult, QueryDefinition, QuerySelectionOptions } from './types';
|
|
2
|
+
export * as MetaObject from './meta-object';
|
|
3
|
+
export declare class QueryBuilder {
|
|
4
|
+
_introspection: IntrospectionSchema;
|
|
5
|
+
_meta: MetaObject;
|
|
6
|
+
private _models;
|
|
7
|
+
private _model;
|
|
8
|
+
private _key;
|
|
9
|
+
private _queryName;
|
|
10
|
+
private _ast;
|
|
11
|
+
_edges: boolean;
|
|
12
|
+
private _op;
|
|
13
|
+
private _mutation;
|
|
14
|
+
private _select;
|
|
15
|
+
constructor({ meta, introspection, }: QueryBuilderOptions);
|
|
16
|
+
initModelMap(): void;
|
|
17
|
+
clear(): void;
|
|
18
|
+
query(model: string): QueryBuilder;
|
|
19
|
+
_findQuery(): string;
|
|
20
|
+
_findMutation(): string;
|
|
21
|
+
select(selection?: QuerySelectionOptions | null): QueryBuilder;
|
|
22
|
+
edges(useEdges: boolean): QueryBuilder;
|
|
23
|
+
getMany({ select }?: {
|
|
24
|
+
select?: QuerySelectionOptions;
|
|
25
|
+
}): QueryBuilder;
|
|
26
|
+
all({ select }?: {
|
|
27
|
+
select?: QuerySelectionOptions;
|
|
28
|
+
}): QueryBuilder;
|
|
29
|
+
count(): QueryBuilder;
|
|
30
|
+
getOne({ select }?: {
|
|
31
|
+
select?: QuerySelectionOptions;
|
|
32
|
+
}): QueryBuilder;
|
|
33
|
+
create({ select }?: {
|
|
34
|
+
select?: QuerySelectionOptions;
|
|
35
|
+
}): QueryBuilder;
|
|
36
|
+
delete({ select }?: {
|
|
37
|
+
select?: QuerySelectionOptions;
|
|
38
|
+
}): QueryBuilder;
|
|
39
|
+
update({ select }?: {
|
|
40
|
+
select?: QuerySelectionOptions;
|
|
41
|
+
}): QueryBuilder;
|
|
42
|
+
queryName(name: string): QueryBuilder;
|
|
43
|
+
print(): QueryBuilderResult;
|
|
44
|
+
pickScalarFields: (selection: QuerySelectionOptions | null, defn: QueryDefinition) => QueryFieldSelection[];
|
|
45
|
+
pickAllFields: (selection: QuerySelectionOptions, defn: QueryDefinition) => QueryFieldSelection[];
|
|
46
|
+
}
|