-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
135 lines (124 loc) · 4.33 KB
/
schema.js
File metadata and controls
135 lines (124 loc) · 4.33 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
/**
* @import {DefinitionNode} from "graphql"
* @import {Logger, FileREEntityResponseData, FieldsOrder, MappedRESchema, InstanceREEntityResponseData} from "../../shared/types/types"
*/
const { mapRootSchemaToContainer } = require('./rootSchema');
const { findNodesByKind } = require('../helpers/findNodesByKind');
const { getTypeDefinitions } = require('./typeDefinitions/typeDefinitions');
const { mapRootTypesToEntities } = require('./rootTypes');
const { astNodeKind } = require('../constants/graphqlAST');
const { getDefinitionCategoryByNameMap } = require('../helpers/getDefinitionCategoryByNameMap');
/**
* Maps a GraphQL schema to a general RE schema
*
* @param {object} params
* @param {DefinitionNode[]} params.schemaItems
* @param {string} params.graphName
* @param {FieldsOrder} [params.fieldsOrder]
* @returns {MappedRESchema}
*/
function getMappedSchema({ schemaItems, graphName, fieldsOrder }) {
if (!schemaItems) {
throw new Error('Schema items are empty');
}
const container = mapRootSchemaToContainer({
rootSchemaNode: findNodesByKind({ nodes: schemaItems, kind: astNodeKind.SCHEMA_DEFINITION })[0],
graphName,
});
const rootTypeNames = [
container.schemaRootTypes?.rootQuery || 'Query',
container.schemaRootTypes?.rootMutation || 'Mutation',
container.schemaRootTypes?.rootSubscription || 'Subscription',
];
const definitionCategoryByNameMap = getDefinitionCategoryByNameMap({ nodes: schemaItems, rootTypeNames });
const rootTypeNodes = findNodesByKind({
nodes: schemaItems,
kind: astNodeKind.OBJECT_TYPE_DEFINITION,
}).filter(node => rootTypeNames.includes(node.name.value));
const entities = mapRootTypesToEntities({
rootTypeNodes,
definitionCategoryByNameMap,
fieldsOrder,
schemaRootTypesMap: rootTypeNames,
});
const typeDefinitions = getTypeDefinitions({
typeDefinitions: schemaItems,
fieldsOrder,
rootTypeNames,
definitionCategoryByNameMap,
});
return { container, entities, typeDefinitions };
}
/**
* Maps a GraphQL schema to a RE from file response
*
* @param {object} params
* @param {DefinitionNode[]} params.schemaItems - The schema items
* @param {string} params.graphName - The name of the graph to be mapped as the container name
* @param {Logger} params.logger - The logger
* @param {FieldsOrder} params.fieldsOrder - The fields order
* @returns {FileREEntityResponseData[]} The mapped entities
*/
function getMappedSchemaFromFile({ schemaItems, graphName, logger, fieldsOrder }) {
try {
const { container, entities, typeDefinitions } = getMappedSchema({ schemaItems, graphName, fieldsOrder });
return entities.map(entity => ({
jsonSchema: JSON.stringify(entity.data),
objectNames: {
collectionName: entity.name,
},
doc: {
bucketInfo: container,
collectionName: entity.name,
dbName: container.name,
modelDefinitions: JSON.stringify(typeDefinitions),
},
}));
} catch (error) {
logger.log('error', error, 'Failed to map GraphQL schema');
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to map GraphQL schema: ${errorMessage}`);
}
}
/**
* Maps a GraphQL schema to a RE from instance response
*
* @param {object} params
* @param {DefinitionNode[]} params.schemaItems - The schema items
* @param {string} params.graphName - The name of the graph to be mapped as the container name
* @param {Logger} params.logger - The logger
* @returns {InstanceREEntityResponseData[]}
*/
function getMappedSchemaFromInstance({ schemaItems, graphName, logger }) {
try {
const { container, entities, typeDefinitions } = getMappedSchema({ schemaItems, graphName });
return entities.map(entity => {
const { properties, required, ...entityLevelData } = entity.data;
return {
dbName: graphName,
collectionName: entity.name,
entityLevel: entityLevelData,
validation: {
jsonSchema: {
properties,
required,
type: 'object',
},
},
emptyBucket: false,
bucketInfo: container,
modelDefinitions: {
properties: typeDefinitions.definitions,
},
};
});
} catch (error) {
logger.log('error', error, 'Failed to map GraphQL schema');
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to map GraphQL schema: ${errorMessage}`);
}
}
module.exports = {
getMappedSchemaFromFile,
getMappedSchemaFromInstance,
};