@kollors/deep-json-server 0.8.1 → 1.0.0-alpha.1
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 +286 -410
- package/README.ru.md +278 -404
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/dist/src/cli.js +50 -63
- package/dist/src/cli.js.map +1 -1
- package/dist/src/config.d.ts +16 -2
- package/dist/src/config.js +24 -8
- package/dist/src/config.js.map +1 -1
- package/dist/src/constants.d.ts +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/database.d.ts +4 -3
- package/dist/src/database.js +32 -19
- package/dist/src/database.js.map +1 -1
- package/dist/src/engine.d.ts +47 -0
- package/dist/src/engine.js +438 -0
- package/dist/src/engine.js.map +1 -0
- package/dist/src/files/disk-store.js +13 -2
- package/dist/src/files/disk-store.js.map +1 -1
- package/dist/src/files/openapi.d.ts +3 -0
- package/dist/src/files/openapi.js +86 -0
- package/dist/src/files/openapi.js.map +1 -0
- package/dist/src/graphql.d.ts +4 -0
- package/dist/src/graphql.js +223 -0
- package/dist/src/graphql.js.map +1 -0
- package/dist/src/model.d.ts +67 -0
- package/dist/src/model.js +319 -0
- package/dist/src/model.js.map +1 -0
- package/dist/src/openapi/document.d.ts +7 -7
- package/dist/src/openapi/document.js +234 -320
- package/dist/src/openapi/document.js.map +1 -1
- package/dist/src/openapi/index.d.ts +3 -6
- package/dist/src/openapi/index.js +2 -3
- package/dist/src/openapi/index.js.map +1 -1
- package/dist/src/query/filter.d.ts +0 -5
- package/dist/src/query/filter.js +6 -170
- package/dist/src/query/filter.js.map +1 -1
- package/dist/src/query/options.d.ts +31 -0
- package/dist/src/query/options.js +135 -0
- package/dist/src/query/options.js.map +1 -0
- package/dist/src/server.d.ts +4 -3
- package/dist/src/server.js +113 -135
- package/dist/src/server.js.map +1 -1
- package/dist/src/types.d.ts +1 -3
- package/dist/src/utils.d.ts +0 -1
- package/dist/src/utils.js +0 -1
- package/dist/src/utils.js.map +1 -1
- package/package.json +13 -8
- package/dist/src/openapi/config.d.ts +0 -11
- package/dist/src/openapi/config.js +0 -204
- package/dist/src/openapi/config.js.map +0 -1
- package/dist/src/openapi/inference.d.ts +0 -14
- package/dist/src/openapi/inference.js +0 -145
- package/dist/src/openapi/inference.js.map +0 -1
- package/dist/src/query/index.d.ts +0 -3
- package/dist/src/query/index.js +0 -4
- package/dist/src/query/index.js.map +0 -1
- package/dist/src/query/pagination.d.ts +0 -11
- package/dist/src/query/pagination.js +0 -28
- package/dist/src/query/pagination.js.map +0 -1
- package/dist/src/query/sort.d.ts +0 -1
- package/dist/src/query/sort.js +0 -54
- package/dist/src/query/sort.js.map +0 -1
- package/dist/src/relations.d.ts +0 -12
- package/dist/src/relations.js +0 -155
- package/dist/src/relations.js.map +0 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { FILE_ROUTES } from './contract.js';
|
|
2
|
+
const createSchemaReference = (name) => ({ $ref: `#/components/schemas/${name}` });
|
|
3
|
+
const createJsonContent = (schema) => ({ content: { 'application/json': { schema } } });
|
|
4
|
+
const createResponse = (description, schema) => ({ description, ...(schema != null && createJsonContent(schema)) });
|
|
5
|
+
const createErrorResponse = (description) => createResponse(description, createSchemaReference('Error'));
|
|
6
|
+
const createParameterReference = (name) => ({ $ref: `#/components/parameters/${name}` });
|
|
7
|
+
const createRequestBody = (name) => ({ required: true, ...createJsonContent(createSchemaReference(name)) });
|
|
8
|
+
export const createFilePaths = () => ({
|
|
9
|
+
[`${FILE_ROUTES.download}/{path}`]: {
|
|
10
|
+
get: {
|
|
11
|
+
operationId: 'downloadFile',
|
|
12
|
+
parameters: [createParameterReference('FilePath')],
|
|
13
|
+
responses: {
|
|
14
|
+
200: { content: { '*/*': { schema: { format: 'binary', type: 'string' } } }, description: 'File download' },
|
|
15
|
+
400: createErrorResponse('Invalid path'),
|
|
16
|
+
404: createErrorResponse('Not found'),
|
|
17
|
+
},
|
|
18
|
+
tags: ['files'],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
[`${FILE_ROUTES.metadata}/{path}`]: {
|
|
22
|
+
get: {
|
|
23
|
+
operationId: 'getFileMetadata',
|
|
24
|
+
parameters: [createParameterReference('FilePath')],
|
|
25
|
+
responses: {
|
|
26
|
+
200: createResponse('File metadata', createSchemaReference('FileMetadata')),
|
|
27
|
+
400: createErrorResponse('Invalid path'),
|
|
28
|
+
404: createErrorResponse('Not found'),
|
|
29
|
+
},
|
|
30
|
+
tags: ['files'],
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
[FILE_ROUTES.storage]: {
|
|
34
|
+
post: {
|
|
35
|
+
operationId: 'uploadFile',
|
|
36
|
+
parameters: ['ContentName', 'ContentDirectory', 'ContentOverride'].map(createParameterReference),
|
|
37
|
+
requestBody: { content: { '*/*': { schema: { format: 'binary', type: 'string' } } }, required: true },
|
|
38
|
+
responses: {
|
|
39
|
+
200: createResponse('Overwritten', createSchemaReference('FileMetadata')),
|
|
40
|
+
201: createResponse('Created', createSchemaReference('FileMetadata')),
|
|
41
|
+
400: createErrorResponse('Invalid request'),
|
|
42
|
+
409: createErrorResponse('Already exists'),
|
|
43
|
+
413: createErrorResponse('File is too large'),
|
|
44
|
+
415: createErrorResponse('Unsupported media type'),
|
|
45
|
+
},
|
|
46
|
+
tags: ['files'],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
[`${FILE_ROUTES.storage}/{path}`]: {
|
|
50
|
+
delete: {
|
|
51
|
+
operationId: 'deleteFile',
|
|
52
|
+
parameters: [createParameterReference('FilePath')],
|
|
53
|
+
responses: {
|
|
54
|
+
204: { description: 'Deleted' },
|
|
55
|
+
400: createErrorResponse('Invalid path'),
|
|
56
|
+
404: createErrorResponse('Not found'),
|
|
57
|
+
},
|
|
58
|
+
tags: ['files'],
|
|
59
|
+
},
|
|
60
|
+
get: {
|
|
61
|
+
operationId: 'getFileContent',
|
|
62
|
+
parameters: [createParameterReference('FilePath')],
|
|
63
|
+
responses: {
|
|
64
|
+
200: { content: { '*/*': { schema: { format: 'binary', type: 'string' } } }, description: 'File contents' },
|
|
65
|
+
400: createErrorResponse('Invalid path'),
|
|
66
|
+
404: createErrorResponse('Not found'),
|
|
67
|
+
},
|
|
68
|
+
tags: ['files'],
|
|
69
|
+
},
|
|
70
|
+
patch: {
|
|
71
|
+
operationId: 'updateFile',
|
|
72
|
+
parameters: [createParameterReference('FilePath')],
|
|
73
|
+
requestBody: createRequestBody('FileUpdate'),
|
|
74
|
+
responses: {
|
|
75
|
+
200: createResponse('Updated', createSchemaReference('FileMetadata')),
|
|
76
|
+
400: createErrorResponse('Invalid request'),
|
|
77
|
+
404: createErrorResponse('Not found'),
|
|
78
|
+
409: createErrorResponse('Already exists'),
|
|
79
|
+
413: createErrorResponse('Request is too large'),
|
|
80
|
+
415: createErrorResponse('Unsupported media type'),
|
|
81
|
+
},
|
|
82
|
+
tags: ['files'],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
//# sourceMappingURL=openapi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../../../src/files/openapi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,MAAM,qBAAqB,GAAG,CAAC,IAAY,EAAiB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,wBAAwB,IAAI,EAAE,EAAE,CAAC,CAAC;AAC1G,MAAM,iBAAiB,GAAG,CAAC,MAAqB,EAAiB,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,kBAAkB,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;AACtH,MAAM,cAAc,GAAG,CAAC,WAAmB,EAAE,MAAsB,EAAiB,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,IAAI,IAAI,IAAI,iBAAi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AW,CAAC;aACtC;YACD,IAAI,EAAE,CAAC,OAAO,CAAC;SAChB;QACD,GAAG,EAAE;YACH,WAAW,EAAE,gBAAgB;YAC7B,UAAU,EAAE,CAAC,wBAAwB,CAAC,UAAU,CAAC,CAAC;YAClD,SAAS,EAAE;gBACT,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,EAAE,WAAW,EAAE,eAAe,EAAE;gBAC3G,GAAG,EAAE,mBAAmB,CAAC,cAAc,CAAC;gBACxC,GAAG,EAAE,mBAAmB,CAAC,WAAW,CAAC;aACtC;YACD,IAAI,EAAE,CAAC,OAAO,CAAC;SAChB;QACD,KAAK,EAAE;YACL,WAAW,EAAE,YAAY;YACzB,UAAU,EAAE,CAAC,wBAAwB,CAAC,UAAU,CAAC,CAAC;YAClD,WAAW,EAAE,iBAAiB,CAAC,YAAY,CAAC;YAC5C,SAAS,EAAE;gBACT,GAAG,EAAE,cAAc,CAAC,SAAS,EAAE,qBAAqB,CAAC,cAAc,CAAC,CAAC;gBACrE,GAAG,EAAE,mBAAmB,CAAC,iBAAiB,CAAC;gBAC3C,GAAG,EAAE,mBAAmB,CAAC,WAAW,CAAC;gBACrC,GAAG,EAAE,mBAAmB,CAAC,gBAAgB,CAAC;gBAC1C,GAAG,EAAE,mBAAmB,CAAC,sBAAsB,CAAC;gBAChD,GAAG,EAAE,mBAAmB,CAAC,wBAAwB,CAAC;aACnD;YACD,IAAI,EAAE,CAAC,OAAO,CAAC;SAChB;KACF;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { assertValidSchema, GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, } from 'graphql';
|
|
2
|
+
import { resolveField } from './engine.js';
|
|
3
|
+
import { assertApi, nodeName, operationName } from './model.js';
|
|
4
|
+
import { sortableFields } from './query/options.js';
|
|
5
|
+
export function buildGraphql(model, engine) {
|
|
6
|
+
assertApi(model, 'graphql');
|
|
7
|
+
const names = new Set(['String', 'Float', 'Int', 'Boolean', 'ID', 'Query', 'Mutation', 'Pager', 'OrderDirection']);
|
|
8
|
+
const reserve = (name) => {
|
|
9
|
+
if (names.has(name))
|
|
10
|
+
throw new Error(`GraphQL type name collision: ${name}`);
|
|
11
|
+
names.add(name);
|
|
12
|
+
return name;
|
|
13
|
+
};
|
|
14
|
+
const objects = new Map();
|
|
15
|
+
const pages = new Map();
|
|
16
|
+
const inputs = new Map();
|
|
17
|
+
const wheres = new Map();
|
|
18
|
+
const filters = new Map();
|
|
19
|
+
const orders = new Map();
|
|
20
|
+
const enums = new Map();
|
|
21
|
+
const pager = new GraphQLInputObjectType({ name: 'Pager', fields: { page: { type: GraphQLInt }, pageSize: { type: GraphQLInt } } });
|
|
22
|
+
const direction = new GraphQLEnumType({ name: 'OrderDirection', values: { ASC: { value: 'ASC' }, DESC: { value: 'DESC' } } });
|
|
23
|
+
const canonical = (entity, node) => (node.relation ? [node.relation, node.relation.root] : [entity, node]);
|
|
24
|
+
function scalar(entity, node, constrained = true) {
|
|
25
|
+
if (constrained && node.enum) {
|
|
26
|
+
let type = enums.get(node);
|
|
27
|
+
if (!type) {
|
|
28
|
+
const values = Object.fromEntries(node.enum.map((value, index) => [typeof value === 'string' && /^[A-Za-z][A-Za-z0-9_]*$/.test(value) && !['true', 'false', 'null'].includes(value) ? value : `VALUE_${index}`, { value }]));
|
|
29
|
+
if (Object.keys(values).length !== node.enum.length)
|
|
30
|
+
throw new Error(`Enum name collision: ${entity.name}.${node.path}`);
|
|
31
|
+
type = new GraphQLEnumType({ name: reserve(`${nodeName(entity, node)}Enum`), values });
|
|
32
|
+
enums.set(node, type);
|
|
33
|
+
}
|
|
34
|
+
return type;
|
|
35
|
+
}
|
|
36
|
+
return node.base === 'number' ? GraphQLFloat : node.base === 'boolean' ? GraphQLBoolean : node.primary ? GraphQLID : GraphQLString;
|
|
37
|
+
}
|
|
38
|
+
function output(entity, node) {
|
|
39
|
+
[entity, node] = canonical(entity, node);
|
|
40
|
+
let type = objects.get(node);
|
|
41
|
+
if (type)
|
|
42
|
+
return type;
|
|
43
|
+
type = new GraphQLObjectType({
|
|
44
|
+
name: reserve(nodeName(entity, node)),
|
|
45
|
+
fields: () => Object.fromEntries(Object.entries(node.children)
|
|
46
|
+
.filter(([, child]) => !child.writeOnly)
|
|
47
|
+
.map(([key, child]) => {
|
|
48
|
+
const object = child.relation || child.base === 'object';
|
|
49
|
+
let childType = object ? (child.many ? page(entity, child) : output(entity, child)) : scalar(entity, child);
|
|
50
|
+
if (child.many && !object)
|
|
51
|
+
childType = new GraphQLList(new GraphQLNonNull(childType));
|
|
52
|
+
if (child.required && !child.nullable)
|
|
53
|
+
childType = new GraphQLNonNull(childType);
|
|
54
|
+
return [
|
|
55
|
+
key,
|
|
56
|
+
{
|
|
57
|
+
type: childType,
|
|
58
|
+
description: child.description,
|
|
59
|
+
args: object && child.many ? listArgs(entity, child) : undefined,
|
|
60
|
+
resolve: (ref, args) => {
|
|
61
|
+
const value = resolveField(ref, child);
|
|
62
|
+
return object && child.many && value != null ? engine.list(value, child, args) : value;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
})),
|
|
67
|
+
});
|
|
68
|
+
objects.set(node, type);
|
|
69
|
+
return type;
|
|
70
|
+
}
|
|
71
|
+
function page(entity, node) {
|
|
72
|
+
[entity, node] = canonical(entity, node);
|
|
73
|
+
let type = pages.get(node);
|
|
74
|
+
if (type)
|
|
75
|
+
return type;
|
|
76
|
+
type = new GraphQLObjectType({
|
|
77
|
+
name: reserve(`${nodeName(entity, node)}Page`),
|
|
78
|
+
fields: () => ({ data: { type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(output(entity, node)))) }, total: { type: new GraphQLNonNull(GraphQLInt) } }),
|
|
79
|
+
});
|
|
80
|
+
pages.set(node, type);
|
|
81
|
+
return type;
|
|
82
|
+
}
|
|
83
|
+
function input(entity, node, mode, root = false) {
|
|
84
|
+
const name = `${nodeName(entity, node)}${mode[0].toUpperCase() + mode.slice(1)}`;
|
|
85
|
+
let type = inputs.get(name);
|
|
86
|
+
if (type)
|
|
87
|
+
return type;
|
|
88
|
+
type = new GraphQLInputObjectType({
|
|
89
|
+
name: reserve(name),
|
|
90
|
+
fields: () => {
|
|
91
|
+
const fields = {};
|
|
92
|
+
for (const [key, child] of Object.entries(node.children)) {
|
|
93
|
+
if (child.relation || child.generated || child.readOnly || (child.primary && mode !== 'create'))
|
|
94
|
+
continue;
|
|
95
|
+
let childType = child.base === 'object' ? input(entity, child, mode) : scalar(entity, child);
|
|
96
|
+
if (child.many)
|
|
97
|
+
childType = new GraphQLList(new GraphQLNonNull(childType));
|
|
98
|
+
if (child.required && !child.nullable && !(root && mode === 'update') && child.default === undefined)
|
|
99
|
+
childType = new GraphQLNonNull(childType);
|
|
100
|
+
fields[key] = { type: childType, description: child.description };
|
|
101
|
+
}
|
|
102
|
+
return fields;
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
inputs.set(name, type);
|
|
106
|
+
return type;
|
|
107
|
+
}
|
|
108
|
+
function where(entity, node) {
|
|
109
|
+
[entity, node] = canonical(entity, node);
|
|
110
|
+
let type = wheres.get(node);
|
|
111
|
+
if (type)
|
|
112
|
+
return type;
|
|
113
|
+
const name = reserve(`${nodeName(entity, node)}Where`);
|
|
114
|
+
type = new GraphQLInputObjectType({
|
|
115
|
+
name,
|
|
116
|
+
fields: () => {
|
|
117
|
+
const fields = {
|
|
118
|
+
and: { type: new GraphQLList(new GraphQLNonNull(type)) },
|
|
119
|
+
or: { type: new GraphQLList(new GraphQLNonNull(type)) },
|
|
120
|
+
not: { type: type },
|
|
121
|
+
};
|
|
122
|
+
for (const [key, child] of Object.entries(node.children))
|
|
123
|
+
if (!child.writeOnly) {
|
|
124
|
+
if (Object.hasOwn(fields, key))
|
|
125
|
+
throw new Error(`Reserved filter field: ${entity.name}.${child.path}`);
|
|
126
|
+
fields[key] = { type: child.many ? filter(entity, child) : child.relation || child.base === 'object' ? where(entity, child) : filter(entity, child) };
|
|
127
|
+
}
|
|
128
|
+
return fields;
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
wheres.set(node, type);
|
|
132
|
+
return type;
|
|
133
|
+
}
|
|
134
|
+
function filter(entity, node) {
|
|
135
|
+
let type = filters.get(node);
|
|
136
|
+
if (type)
|
|
137
|
+
return type;
|
|
138
|
+
type = new GraphQLInputObjectType({
|
|
139
|
+
name: reserve(`${nodeName(entity, node)}Filter`),
|
|
140
|
+
fields: () => {
|
|
141
|
+
const fields = { not: { type: type } };
|
|
142
|
+
const object = node.relation || node.base === 'object';
|
|
143
|
+
if (node.many) {
|
|
144
|
+
const element = object ? where(entity, node) : filter(entity, { ...node, many: false, path: `${node.path}_element` });
|
|
145
|
+
for (const key of ['some', 'every', 'none'])
|
|
146
|
+
fields[key] = { type: element };
|
|
147
|
+
if (!object) {
|
|
148
|
+
const value = scalar(entity, node);
|
|
149
|
+
fields.contains = { type: value };
|
|
150
|
+
fields.in = { type: new GraphQLList(value) };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const value = scalar(entity, node);
|
|
155
|
+
for (const key of ['eq', 'ne'])
|
|
156
|
+
fields[key] = { type: value };
|
|
157
|
+
fields.in = { type: new GraphQLList(value) };
|
|
158
|
+
if (node.base !== 'boolean')
|
|
159
|
+
for (const key of ['gt', 'gte', 'lt', 'lte'])
|
|
160
|
+
fields[key] = { type: scalar(entity, node, false) };
|
|
161
|
+
if (node.base === 'string')
|
|
162
|
+
for (const key of ['contains', 'startsWith', 'endsWith'])
|
|
163
|
+
fields[key] = { type: GraphQLString };
|
|
164
|
+
}
|
|
165
|
+
return fields;
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
filters.set(node, type);
|
|
169
|
+
return type;
|
|
170
|
+
}
|
|
171
|
+
function order(entity, node) {
|
|
172
|
+
[entity, node] = canonical(entity, node);
|
|
173
|
+
let type = orders.get(node);
|
|
174
|
+
if (type)
|
|
175
|
+
return type;
|
|
176
|
+
const paths = sortableFields(node);
|
|
177
|
+
if (!paths.length)
|
|
178
|
+
return undefined;
|
|
179
|
+
const values = Object.fromEntries(paths.map((path) => [path.replaceAll('.', '_'), { value: path }]));
|
|
180
|
+
if (Object.keys(values).length !== paths.length)
|
|
181
|
+
throw new Error(`Sort enum collision: ${nodeName(entity, node)}`);
|
|
182
|
+
const field = new GraphQLEnumType({ name: reserve(`${nodeName(entity, node)}OrderField`), values });
|
|
183
|
+
type = new GraphQLInputObjectType({ name: reserve(`${nodeName(entity, node)}Order`), fields: { field: { type: new GraphQLNonNull(field) }, direction: { type: new GraphQLNonNull(direction) } } });
|
|
184
|
+
orders.set(node, type);
|
|
185
|
+
return type;
|
|
186
|
+
}
|
|
187
|
+
function listArgs(entity, node) {
|
|
188
|
+
const ordering = order(entity, node);
|
|
189
|
+
return { where: { type: where(entity, node) }, pager: { type: pager }, ...(ordering ? { order: { type: new GraphQLList(new GraphQLNonNull(ordering)) } } : {}) };
|
|
190
|
+
}
|
|
191
|
+
const queries = {};
|
|
192
|
+
const mutations = {};
|
|
193
|
+
for (const entity of model.entities.filter((e) => e.api.includes('graphql'))) {
|
|
194
|
+
const name = operationName(entity);
|
|
195
|
+
for (const operation of [name, `${name}List`])
|
|
196
|
+
if (queries[operation])
|
|
197
|
+
throw new Error(`GraphQL operation collision: ${operation}`);
|
|
198
|
+
const keyArg = { [entity.primary]: { type: new GraphQLNonNull(scalar(entity, entity.fields[entity.primary], false)) } };
|
|
199
|
+
queries[name] = { type: output(entity, entity.root), args: keyArg, resolve: (_root, args, ctx) => engine.find(ctx.snapshot, entity, args[entity.primary]) ?? null };
|
|
200
|
+
queries[`${name}List`] = {
|
|
201
|
+
type: new GraphQLNonNull(page(entity, entity.root)),
|
|
202
|
+
args: listArgs(entity, entity.root),
|
|
203
|
+
resolve: (_root, args, ctx) => engine.list(engine.records(ctx.snapshot, entity), entity.root, args),
|
|
204
|
+
};
|
|
205
|
+
for (const mode of ['create', 'replace', 'update', 'delete']) {
|
|
206
|
+
const operation = `${name}${mode[0].toUpperCase() + mode.slice(1)}`;
|
|
207
|
+
if (mutations[operation])
|
|
208
|
+
throw new Error(`GraphQL operation collision: ${operation}`);
|
|
209
|
+
const fields = Object.values(entity.root.children).filter((child) => !child.relation && !child.generated && !child.readOnly && (!child.primary || mode === 'create'));
|
|
210
|
+
mutations[operation] = {
|
|
211
|
+
type: output(entity, entity.root),
|
|
212
|
+
args: { ...(mode !== 'create' ? keyArg : {}), ...(mode !== 'delete' && fields.length ? { data: { type: new GraphQLNonNull(input(entity, entity.root, mode, true)) } } : {}) },
|
|
213
|
+
resolve: (_root, args) => engine.mutate(entity, mode, args[entity.primary], args.data ?? {}),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!Object.keys(queries).length)
|
|
218
|
+
throw new Error('No models enable graphql');
|
|
219
|
+
const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: queries }), mutation: new GraphQLObjectType({ name: 'Mutation', fields: mutations }) });
|
|
220
|
+
assertValidSchema(schema);
|
|
221
|
+
return schema;
|
|
222
|
+
}
|
|
223
|
+
//# sourceMappingURL=graphql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,eAAe,EAEf,YAAY,EACZ,SAAS,EAET,sBAAsB,EAEtB,UAAU,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EAEjB,aAAa,EACb,aAAa,GACd,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,YAAY,EAAE,M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,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,cAAc,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE;gBAC7K,OAAO,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;aAC7F,CAAC;QACJ,CAAC;IACH,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC9E,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,EAAE,KAAK,EAAE,IAAI,iBAAiB,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,iBAAiB,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC;IACjL,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC1B,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Ajv, type ValidateFunction } from 'ajv';
|
|
2
|
+
import type { DatabaseData, JsonValue } from './types.js';
|
|
3
|
+
export interface Field {
|
|
4
|
+
type: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
example?: JsonValue;
|
|
7
|
+
required?: boolean;
|
|
8
|
+
nullable?: boolean;
|
|
9
|
+
default?: JsonValue;
|
|
10
|
+
enum?: JsonValue[];
|
|
11
|
+
primary?: boolean;
|
|
12
|
+
generated?: 'uuid' | 'increment';
|
|
13
|
+
readOnly?: boolean;
|
|
14
|
+
writeOnly?: boolean;
|
|
15
|
+
minLength?: number;
|
|
16
|
+
maxLength?: number;
|
|
17
|
+
pattern?: string;
|
|
18
|
+
format?: 'date' | 'date-time' | 'email' | 'uri' | 'uuid';
|
|
19
|
+
minimum?: number;
|
|
20
|
+
maximum?: number;
|
|
21
|
+
source?: string;
|
|
22
|
+
target?: string;
|
|
23
|
+
onDelete?: 'restrict' | 'cascade';
|
|
24
|
+
}
|
|
25
|
+
export interface EntityDefinition {
|
|
26
|
+
collection: string;
|
|
27
|
+
api?: ('openapi' | 'graphql')[];
|
|
28
|
+
fields: Record<string, Field>;
|
|
29
|
+
}
|
|
30
|
+
export type ModelSchema = Record<string, EntityDefinition>;
|
|
31
|
+
export interface Node extends Field {
|
|
32
|
+
path: string;
|
|
33
|
+
base: string;
|
|
34
|
+
many: boolean;
|
|
35
|
+
children: Record<string, Node>;
|
|
36
|
+
relation?: Entity;
|
|
37
|
+
implicit?: boolean;
|
|
38
|
+
}
|
|
39
|
+
export interface Entity {
|
|
40
|
+
name: string;
|
|
41
|
+
collection: string;
|
|
42
|
+
api: ('openapi' | 'graphql')[];
|
|
43
|
+
primary: string;
|
|
44
|
+
fields: Record<string, Node>;
|
|
45
|
+
root: Node;
|
|
46
|
+
}
|
|
47
|
+
export interface Model {
|
|
48
|
+
entities: Entity[];
|
|
49
|
+
byName: Map<string, Entity>;
|
|
50
|
+
byCollection: Map<string, Entity>;
|
|
51
|
+
explicit: boolean;
|
|
52
|
+
}
|
|
53
|
+
export type ValidationSchema = Record<string, unknown>;
|
|
54
|
+
export declare const ajv: Ajv;
|
|
55
|
+
export declare const pathParts: (path: string) => string[];
|
|
56
|
+
export declare const readPath: (value: unknown, path: string | string[]) => unknown[];
|
|
57
|
+
export declare const childName: (node: Node) => string;
|
|
58
|
+
export declare const nodeName: (entity: Entity, node: Node) => string;
|
|
59
|
+
export declare const operationName: (entity: Entity) => string;
|
|
60
|
+
export declare function loadModel(source: unknown): Promise<Model | undefined>;
|
|
61
|
+
export declare function assertApi(model: Model | undefined, api: 'graphql' | 'openapi'): asserts model is Model;
|
|
62
|
+
export declare function valueSchema(node: Node): ValidationSchema;
|
|
63
|
+
export type InputMode = 'stored' | 'create' | 'replace' | 'update';
|
|
64
|
+
export declare function objectSchema(node: Node, mode: InputMode, root?: boolean): ValidationSchema;
|
|
65
|
+
export declare const validators: WeakMap<Entity, Map<InputMode, ValidateFunction<unknown>>>;
|
|
66
|
+
export declare function validateRecord(entity: Entity, value: unknown, mode: InputMode): void;
|
|
67
|
+
export declare function inferModel(database: DatabaseData): Model;
|