diff --git a/src/NeptuneSchema.js b/src/NeptuneSchema.js index 1baccf9..1e79dbd 100644 --- a/src/NeptuneSchema.js +++ b/src/NeptuneSchema.js @@ -55,9 +55,7 @@ const schema = { }; function sanitize(text) { - // TODO implement sanitization logic - // placeholder for sanitization of query text that cannot be parameterized - return text; + return `\`${text}\``; } /** @@ -446,7 +444,7 @@ async function getNeptuneSchema() { await getEdgesDirections(); await getEdgesDirectionsCardinality(); - + return JSON.stringify(schema, null, 2); } diff --git a/src/graphdb.js b/src/graphdb.js index 19838fe..36a2c92 100644 --- a/src/graphdb.js +++ b/src/graphdb.js @@ -38,7 +38,7 @@ function checkForDuplicateNames(schema) { } -// Write a function that takes an input a string and return the string lowercase except the first charachter uppercase +// Write a function that takes an input a string and return the string lowercase except the first character uppercase function toPascalCase (str) { let r = ''; if (changeCase) { @@ -52,6 +52,25 @@ function toPascalCase (str) { return r.trim(); } +// Changes every instance of invalid characters in the given label with the following abbreviations +function cleanseLabel(label) { + return label + .replaceAll("!", "_ex_") + .replaceAll("$", "_dol_") + .replaceAll("&", "_amp_") + .replaceAll("(", "_op_") + .replaceAll(")", "_cp_") + .replaceAll(".", "_dot_") + .replaceAll(":", "_cn_") + .replaceAll("=", "_eq_") + .replaceAll("@", "_at_") + .replaceAll("[", "_os_") + .replaceAll("]", "_cs_") + .replaceAll("{", "_oc_") + .replaceAll("|", "_vb_") + .replaceAll("}", "_cc_") + .replaceAll("-", "_hy_"); +} function graphDBInferenceSchema (graphbSchema, addMutations) { let r = ''; @@ -61,35 +80,47 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { gdbs.nodeStructures.forEach(node => { // node type - let nodeCase = node.label; + let nodeCase = cleanseLabel(node.label); if (changeCase) { - nodeCase = toPascalCase(node.label); + nodeCase = toPascalCase(nodeCase); + } + + if (node.label !== nodeCase) { r += `type ${nodeCase} @alias(property:"${node.label}") {\n`; - } else { + } + else { r += `type ${nodeCase} {\n`; } r += '\t_id: ID! @id\n'; node.properties.forEach(property => { - if (property.name == 'id') + if (property.name === 'id') { r+= `\tid: ID\n`; - else - r+= `\t${property.name}: ${property.type}\n`; + } + else { + let propertyCase = cleanseLabel(property.name); + let alias = ''; + if (property.name !== propertyCase) { + alias = ` @alias(property: "${property.name}")`; + } + r+= `\t${propertyCase}: ${property.type}${alias}\n`; + } }); let edgeTypes = []; gdbs.edgeStructures.forEach(edge => { edge.directions.forEach(direction => { - let fromCase = toPascalCase(direction.from); - let toCase = toPascalCase(direction.to); - let edgeCase = toPascalCase(edge.label); - if (direction.from == node.label && direction.to == node.label){ - if (direction.relationship == 'MANY-MANY') { + let fromCase = toPascalCase(cleanseLabel(direction.from)); + let toCase = toPascalCase(cleanseLabel(direction.to)); + let edgeCase = toPascalCase(cleanseLabel(edge.label)); + + if (direction.from === node.label && direction.to === node.label){ + if (direction.relationship === 'MANY-MANY') { r += `\t${nodeCase.toLocaleLowerCase() + edgeCase}sOut(filter: ${nodeCase}Input, options: Options): [${nodeCase}] @relationship(edgeType:"${edge.label}", direction:OUT)\n`; - r += `\t${nodeCase.toLocaleLowerCase() + edgeCase}sIn(filter: ${nodeCase}Input, options: Options): [${nodeCase}] @relationship(edgeType:"${edge.label}", direction:IN)\n`; + r += `\t${nodeCase.toLocaleLowerCase() + edgeCase}sIn(filter: ${nodeCase}Input, options: Options): [${nodeCase}] @relationship(edgeType:"${edge.label}", direction:IN)\n`; } - if (direction.relationship == 'ONE-ONE') { + if (direction.relationship === 'ONE-ONE') { r += `\t${nodeCase.toLocaleLowerCase() + edgeCase}Out: ${nodeCase} @relationship(edgeType:"${edge.label}", direction:OUT)\n`; r += `\t${nodeCase.toLocaleLowerCase() + edgeCase}In: ${nodeCase} @relationship(edgeType:"${edge.label}", direction:IN)\n`; } @@ -97,29 +128,29 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { edgeTypes.push(edge.label); } - if (direction.from == node.label && direction.to != node.label){ - if (direction.relationship == 'MANY-MANY') { - r += `\t${toCase.toLocaleLowerCase() + edgeCase}sOut(filter: ${toCase}Input, options: Options): [${toCase}] @relationship(edgeType:"${edge.label}", direction:OUT)\n` + if (direction.from === node.label && direction.to !== node.label){ + if (direction.relationship === 'MANY-MANY') { + r += `\t${toCase.toLocaleLowerCase() + edgeCase}sOut(filter: ${toCase}Input, options: Options): [${toCase}] @relationship(edgeType:"${edge.label}", direction:OUT)\n`; } - if (direction.relationship == 'ONE-MANY') { - r += `\t${toCase.toLocaleLowerCase() + edgeCase}sOut(filter: ${toCase}Input, options: Options): [${toCase}] @relationship(edgeType:"${edge.label}", direction:OUT)\n` + if (direction.relationship === 'ONE-MANY') { + r += `\t${toCase.toLocaleLowerCase() + edgeCase}sOut(filter: ${toCase}Input, options: Options): [${toCase}] @relationship(edgeType:"${edge.label}", direction:OUT)\n`; } - if (direction.relationship == 'MANY-ONE') { - r += `\t${toCase.toLocaleLowerCase() + edgeCase}Out: ${toCase} @relationship(edgeType:"${edge.label}", direction:OUT)\n` + if (direction.relationship === 'MANY-ONE') { + r += `\t${toCase.toLocaleLowerCase() + edgeCase}Out: ${toCase} @relationship(edgeType:"${edge.label}", direction:OUT)\n`; } if (!edgeTypes.includes(edge.label)) edgeTypes.push(edge.label); } - if (direction.from != node.label && direction.to == node.label){ - if (direction.relationship == 'MANY-MANY') { + if (direction.from !== node.label && direction.to === node.label){ + if (direction.relationship === 'MANY-MANY') { r += `\t${fromCase.toLocaleLowerCase() + edgeCase}sIn(filter: ${fromCase}Input, options: Options): [${fromCase}] @relationship(edgeType:"${edge.label}", direction:IN)\n` } - if (direction.relationship == 'ONE-MANY') { - r += `\t${fromCase.toLocaleLowerCase() + edgeCase}In: ${fromCase} @relationship(edgeType:"${edge.label}", direction:IN)\n` + if (direction.relationship === 'ONE-MANY') { + r += `\t${fromCase.toLocaleLowerCase() + edgeCase}In: ${fromCase} @relationship(edgeType:"${edge.label}", direction:IN)\n`; } - if (direction.relationship == 'MANY-ONE') { - r += `\t${fromCase.toLocaleLowerCase() + edgeCase}sIn(filter: ${fromCase}Input, options: Options): [${fromCase}] @relationship(edgeType:"${edge.label}", direction:IN)\n` + if (direction.relationship === 'MANY-ONE') { + r += `\t${fromCase.toLocaleLowerCase() + edgeCase}sIn(filter: ${fromCase}Input, options: Options): [${fromCase}] @relationship(edgeType:"${edge.label}", direction:IN)\n`; } if (!edgeTypes.includes(edge.label)) edgeTypes.push(edge.label); @@ -141,7 +172,7 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { ? toPascalCase(edgeType) : edgeType; - r += `\t${aliasedEdgeType}:${caseAdjustedEdgeType}`; + r += `\t${cleanseLabel(aliasedEdgeType)}:${cleanseLabel(caseAdjustedEdgeType)}\n`; }); r += '}\n\n'; @@ -150,7 +181,13 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { r += `input ${nodeCase}Input {\n`; r += '\t_id: ID @id\n'; node.properties.forEach(property => { - r+= `\t${property.name}: ${property.type}\n`; + let propertyCase = cleanseLabel(property.name); + if (property.name !== propertyCase) { + r+= `\t${propertyCase}: ${property.type} @alias(property: "${property.name}")\n`; + } + else { + r+= `\t${property.name}: ${property.type}\n`; + } }); r += '}\n\n'; }) @@ -158,28 +195,45 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { // edge types gdbs.edgeStructures.forEach(edge => { // edge type - let edgeCase = edge.label; + let edgeCase = cleanseLabel(edge.label); if (changeCase) { - edgeCase = toPascalCase(edge.label); - r += `type ${edgeCase} @alias(property:"${edge.label}") {\n`; - } else { + edgeCase = toPascalCase(edgeCase); + } + if (edge.label !== edgeCase) { + r += `type ${edgeCase} @alias(property:"${edge.label}") {\n`; + } + else { r += `type ${edgeCase} {\n`; } r += '\t_id: ID! @id\n'; edge.properties.forEach(property => { - if (property.name == 'id') - r+= `\tid: ID\n`; - else - r+= `\t${property.name}: ${property.type}\n`; + if (property.name === 'id') { + r += `\tid: ID\n`; + } + else { + let propertyCase = cleanseLabel(property.name); + let alias = ''; + if (property.name !== propertyCase) { + alias = ` @alias(property: "${property.name}")`; + } + r+= `\t${propertyCase}: ${property.type}${alias}\n`; + } }); r += '}\n\n'; // input for the edge type - if (edge.properties.length > 0) { - r += `input ${edgeCase}Input {\n`; + if (edge.properties.length > 0) { + r += `input ${edgeCase}Input {\n`; + edge.properties.forEach(property => { - r+= `\t${property.name}: ${property.type}\n`; + let propertyCase = cleanseLabel(property.name); + if (property.name !== propertyCase) { + r += `\t${propertyCase}: ${property.type} @alias(property: "${property.name}")\n`; + } + else { + r+= `\t${property.name}: ${property.type}\n`; + } }); r += '}\n\n'; } @@ -193,17 +247,17 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { // query r += `type Query {\n`; gdbs.nodeStructures.forEach(node => { - let nodeCase = toPascalCase(node.label); + let nodeCase = toPascalCase(cleanseLabel(node.label)); r += `\tgetNode${nodeCase}(filter: ${nodeCase}Input): ${nodeCase}\n`; r += `\tgetNode${nodeCase}s(filter: ${nodeCase}Input, options: Options): [${nodeCase}]\n`; }); r += '}\n\n'; // mutation - if (addMutations) { + if (addMutations) { r += `type Mutation {\n`; gdbs.nodeStructures.forEach(node => { - let nodeCase = toPascalCase(node.label); + let nodeCase = toPascalCase(cleanseLabel(node.label)); r += `\tcreateNode${nodeCase}(input: ${nodeCase}Input!): ${nodeCase}\n`; r += `\tupdateNode${nodeCase}(input: ${nodeCase}Input!): ${nodeCase}\n`; r += `\tdeleteNode${nodeCase}(_id: ID!): Boolean\n`; @@ -211,16 +265,17 @@ function graphDBInferenceSchema (graphbSchema, addMutations) { gdbs.edgeStructures.forEach(edge => { edge.directions.forEach(direction => { - let edgeCase = toPascalCase(edge.label); - let fromCase = toPascalCase(direction.from); - let toCase = toPascalCase(direction.to); - if (edge.properties.length > 0) { - r += `\tconnectNode${fromCase}ToNode${toCase}Edge${edgeCase}(from_id: ID!, to_id: ID!, edge: ${edgeCase}Input!): ${edgeCase}\n`; - r += `\tupdateEdge${edgeCase}From${fromCase}To${toCase}(from_id: ID!, to_id: ID!, edge: ${edgeCase}Input!): ${edgeCase}\n`; - } else { - r += `\tconnectNode${fromCase}ToNode${toCase}Edge${edgeCase}(from_id: ID!, to_id: ID!): ${edgeCase}\n`; - } - r += `\tdeleteEdge${edgeCase}From${fromCase}To${toCase}(from_id: ID!, to_id: ID!): Boolean\n`; + let fromCase = toPascalCase(cleanseLabel(direction.from)); + let toCase = toPascalCase(cleanseLabel(direction.to)); + let edgeCase = toPascalCase(cleanseLabel(edge.label)); + + if (edge.properties.length > 0) { + r += `\tconnectNode${fromCase}ToNode${toCase}Edge${edgeCase}(from_id: ID!, to_id: ID!, edge: ${edgeCase}Input!): ${edgeCase}\n`; + r += `\tupdateEdge${edgeCase}From${fromCase}To${toCase}(from_id: ID!, to_id: ID!, edge: ${edgeCase}Input!): ${edgeCase}\n`; + } else { + r += `\tconnectNode${fromCase}ToNode${toCase}Edge${edgeCase}(from_id: ID!, to_id: ID!): ${edgeCase}\n`; + } + r += `\tdeleteEdge${edgeCase}From${fromCase}To${toCase}(from_id: ID!, to_id: ID!): Boolean\n`; }); }); r += '}\n\n'; diff --git a/src/test/graphdb.test.js b/src/test/graphdb.test.js index 3e4a1d3..4a96ec0 100644 --- a/src/test/graphdb.test.js +++ b/src/test/graphdb.test.js @@ -1,4 +1,4 @@ -import {graphDBInferenceSchema} from '../graphdb.js'; +import { graphDBInferenceSchema } from '../graphdb.js'; const SCHEMA_WITH_PROPERTY_AND_EDGE_SAME_NAME = { "nodeStructures": [ @@ -77,6 +77,198 @@ const SCHEMA_WITH_PROPERTY_AND_EDGE_SAME_NAME = { ] }; +const SCHEMA_WITH_SPECIAL_CHARS = { + "nodeStructures": [ + { + "label": "abc!$123&efg", + "properties": [ + { + "name": "instance_type", + "type": "String" + }, + { + "name": "state", + "type": "String" + }, + { + "name": "arn", + "type": "String" + } + ] + }, + { + "label": "abc(123).efg:456", + "properties": [ + { + "name": "name", + "type": "String" + }, + { + "name": "ip_range.first_ip", + "type": "String" + }, + { + "name": "ip_range.last_ip", + "type": "String" + } + ] + }, + { + "label": "abc=123@[efg]456", + "properties": [ + { + "name": "instance_type", + "type": "String" + }, + { + "name": "state", + "type": "String" + }, + { + "name": "arn", + "type": "String" + } + ] + }, + { + "label": "abc{123}|efg-456", + "properties": [ + { + "name": "name", + "type": "String" + }, + { + "name": "ip_range.first_ip", + "type": "String" + }, + { + "name": "ip_range.last_ip", + "type": "String" + } + ] + }, + ], + "edgeStructures": [ + { + "label": "resource_link", + "properties": [], + "directions": [ + { + "from": "abc!$123&efg", + "to": "abc(123).efg:456", + "relationship": "MANY-ONE" + }, + { + "from": "abc!$123&efg", + "to": "abc!$123&efg", + "relationship": "ONE-ONE" + } + ] + } + ] +}; + +const SCHEMA_WITH_SPECIAL_CHARS_RESULT = ` + type Abc_ex__dol_123_amp_efg @alias(property:\"abc!$123&efg\") { + _id: ID! @id + instance_type: String + state: String + arn: String + abc_op_123_cp__dot_efg_cn_456Resource_linkOut: Abc_op_123_cp__dot_efg_cn_456 @relationship(edgeType:\"resource_link\", direction:OUT) + abc_ex__dol_123_amp_efgResource_linkOut: Abc_ex__dol_123_amp_efg @relationship(edgeType:\"resource_link\", direction:OUT) + abc_ex__dol_123_amp_efgResource_linkIn: Abc_ex__dol_123_amp_efg @relationship(edgeType:\"resource_link\", direction:IN) + resource_link:Resource_link + } + + input Abc_ex__dol_123_amp_efgInput { + _id: ID @id + instance_type: String + state: String + arn: String + } + + type Abc_op_123_cp__dot_efg_cn_456 @alias(property:\"abc(123).efg:456\") { + _id: ID! @id + name: String + ip_range_dot_first_ip: String @alias(property: \"ip_range.first_ip\") + ip_range_dot_last_ip: String @alias(property: \"ip_range.last_ip\") + abc_ex__dol_123_amp_efgResource_linksIn(filter: Abc_ex__dol_123_amp_efgInput, options: Options): [Abc_ex__dol_123_amp_efg] @relationship(edgeType:\"resource_link\", direction:IN) + resource_link:Resource_link + } + + input Abc_op_123_cp__dot_efg_cn_456Input { + _id: ID @id + name: String + ip_range_dot_first_ip: String @alias(property: \"ip_range.first_ip\") + ip_range_dot_last_ip: String @alias(property: \"ip_range.last_ip\") + } + + type Abc_eq_123_at__os_efg_cs_456 @alias(property:\"abc=123@[efg]456\") { + _id: ID! @id + instance_type: String + state: String + arn: String + } + + input Abc_eq_123_at__os_efg_cs_456Input { + _id: ID @id + instance_type: String + state: String + arn: String + } + + type Abc_oc_123_cc__vb_efg_hy_456 @alias(property:\"abc{123}|efg-456\") { + _id: ID! @id + name: String + ip_range_dot_first_ip: String @alias(property: \"ip_range.first_ip\") + ip_range_dot_last_ip: String @alias(property: \"ip_range.last_ip\") + } + + input Abc_oc_123_cc__vb_efg_hy_456Input { + _id: ID @id + name: String + ip_range_dot_first_ip: String @alias(property: \"ip_range.first_ip\") + ip_range_dot_last_ip: String @alias(property: \"ip_range.last_ip\") + } + + type Resource_link @alias(property:\"resource_link\") { + _id: ID! @id + } + + input Options { + limit:Int + } + + type Query { + getNodeAbc_ex__dol_123_amp_efg(filter: Abc_ex__dol_123_amp_efgInput): Abc_ex__dol_123_amp_efg + getNodeAbc_ex__dol_123_amp_efgs(filter: Abc_ex__dol_123_amp_efgInput, options: Options): [Abc_ex__dol_123_amp_efg] + getNodeAbc_op_123_cp__dot_efg_cn_456(filter: Abc_op_123_cp__dot_efg_cn_456Input): Abc_op_123_cp__dot_efg_cn_456 + getNodeAbc_op_123_cp__dot_efg_cn_456s(filter: Abc_op_123_cp__dot_efg_cn_456Input, options: Options): [Abc_op_123_cp__dot_efg_cn_456] + getNodeAbc_eq_123_at__os_efg_cs_456(filter: Abc_eq_123_at__os_efg_cs_456Input): Abc_eq_123_at__os_efg_cs_456 + getNodeAbc_eq_123_at__os_efg_cs_456s(filter: Abc_eq_123_at__os_efg_cs_456Input, options: Options): [Abc_eq_123_at__os_efg_cs_456] + getNodeAbc_oc_123_cc__vb_efg_hy_456(filter: Abc_oc_123_cc__vb_efg_hy_456Input): Abc_oc_123_cc__vb_efg_hy_456 + getNodeAbc_oc_123_cc__vb_efg_hy_456s(filter: Abc_oc_123_cc__vb_efg_hy_456Input, options: Options): [Abc_oc_123_cc__vb_efg_hy_456] + } + + schema { + query: Query + } +`; + test('node with same property and edge label should add underscore prefix', () => { expect(graphDBInferenceSchema(JSON.stringify(SCHEMA_WITH_PROPERTY_AND_EDGE_SAME_NAME), false)).toContain('_commonName:Commonname'); }); + +function fixWhitespace(str) { + return str + .replace(/\s+/g, ' ') // replace whitespace with a space + .trim(); // trim leading and trailing whitespace +} + +test('should properly replace special chars in schema', function() { + let input = graphDBInferenceSchema(JSON.stringify(SCHEMA_WITH_SPECIAL_CHARS)); + let result = SCHEMA_WITH_SPECIAL_CHARS_RESULT; + input = fixWhitespace(input); + result = fixWhitespace(result); + expect(input).toBe(result); +}); \ No newline at end of file diff --git a/templates/JSResolverOCHTTPSTemplate.js b/templates/JSResolverOCHTTPSTemplate.js index f7865e3..683893f 100644 --- a/templates/JSResolverOCHTTPSTemplate.js +++ b/templates/JSResolverOCHTTPSTemplate.js @@ -49,7 +49,7 @@ function resolveGraphDBQueryFromAppSyncEvent(event) { query += event.selectionSetGraphQL; query += '\n}'; - let graphQuery = resolveGraphDBQuery(query); + const graphQuery = resolveGraphDBQuery(query); return graphQuery; } @@ -86,7 +86,6 @@ function getTypeAlias(typeName) { else return alias; } - function getSchemaInputTypeArgs (inputType, schemaInfo) { @@ -394,16 +393,16 @@ function createQueryFunctionMatchStatement(obj, matchStatements, querySchemaInfo gq = gq.replace('$' + arg.name.value, arg.value.value); }); - matchStatements.push(gq); + matchStatements.push(gq); } else { let { queryArguments, where } = getQueryArguments(obj.definitions[0].selectionSet.selections[0].arguments, querySchemaInfo); if (queryArguments.length > 0) { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias}{${queryArguments}})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`{${queryArguments}})${where}`); } else { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`)${where}`); } if (querySchemaInfo.argOptionsLimit != null) @@ -505,7 +504,6 @@ function createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath) { refactored.queryMatch = 'OPTIONAL ' + refactored.queryMatch; matchStatements.push(refactored.queryMatch); - let lastNamePathContent = ''; if ( refactored.returnAggregation != '' ) { const thisWithId = withStatements.push({carryOver: refactored.returnCarryOver, inLevel: '', content: `${refactored.returnAggregation} AS ${refactored.inLevel}`}) -1; let i = withStatements.findIndex(({carryOver}) => carryOver.startsWith(lastNamePath)); @@ -545,7 +543,7 @@ function createQueryFieldLeafStatement(fieldSchemaInfo, lastNamePath) { createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath); } } else { - withStatements[i].content += ' ' + lastNamePath + '.' + fieldSchemaInfo.alias; + withStatements[i].content += ' ' + lastNamePath + '.' + `\`${fieldSchemaInfo.alias}\``; } } } @@ -570,9 +568,9 @@ function createTypeFieldStatementAndRecurse(e, fieldSchemaInfo, lastNamePath, la if (schemaTypeInfo.isRelationship) { if (schemaTypeInfo.relationship.direction === 'IN') { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } else { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } } const thisWithId = withStatements.push({carryOver: schemaTypeInfo.pathName, inLevel: '', content: ''}) - 1; @@ -641,7 +639,7 @@ function finalizeGraphQuery(matchStatements, withStatements, returnString) { // make a string out of match statements let ocMatchStatements = ''; matchStatements.forEach(e => { - ocMatchStatements += e + '\n'; + ocMatchStatements += e + '\n'; }); ocMatchStatements = ocMatchStatements.substring(0, ocMatchStatements.length - 1); @@ -739,21 +737,21 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // createNode if (querySchemaInfo.name.startsWith('createNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let ocQuery = `CREATE (${nodeName}:${querySchemaInfo.returnTypeAlias} {${inputFields.fields}})\nRETURN ${returnBlock}`; + const ocQuery = `CREATE (${nodeName}:\`${querySchemaInfo.returnTypeAlias}\` {${inputFields.fields}})\nRETURN ${returnBlock}`; return ocQuery; } // updateNode if (querySchemaInfo.name.startsWith('updateNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeID = inputFields.graphIdValue; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeID = inputFields.graphIdValue; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); @@ -761,7 +759,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // :( SET += is not working, so let's work around it. //let ocQuery = `MATCH (${nodeName}) WHERE ID(${nodeName}) = '${nodeID}' SET ${nodeName} += {${inputFields}} RETURN ${returnBlock}`; // workaround: - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -770,17 +768,17 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { setString = setString.substring(0, setString.length - 1); let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } // deleteNode if (querySchemaInfo.name.startsWith('deleteNode') && querySchemaInfo.graphQuery == null) { - let nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; return ocQuery; } @@ -788,10 +786,10 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('connectNode') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let egdgeTypeAlias = getTypeAlias(edgeType); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const egdgeTypeAlias = getTypeAlias(edgeType); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); let paramFromId = edgeName + '_' + 'whereFromId'; let paramToId = edgeName + '_' + 'whereToId'; @@ -799,11 +797,11 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { Object.assign(parameters, {[paramToId]: toID}); if (obj.definitions[0].selectionSet.selections[0].arguments.length > 2) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; return ocQuery; } else { - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`]->(to)\nRETURN ${returnBlock}`; return ocQuery; } } @@ -814,13 +812,13 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; let edgeType = querySchemaInfo.name.match(new RegExp('updateEdge' + "(.*)" + 'From'))[1]; let egdgeTypeAlias = getTypeAlias(edgeType); - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${edgeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -828,12 +826,12 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { }); setString = setString.substring(0, setString.length - 1); - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from)-[${edgeName}:$\`${egdgeTypeAlias}\`]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } @@ -841,14 +839,14 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('deleteEdge') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; + const ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; return ocQuery; } @@ -858,7 +856,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let ocQuery = querySchemaInfo.graphQuery; if (ocQuery.includes('$input')) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); ocQuery = ocQuery.replace('$input', inputFields.fields); } else { obj.definitions[0].selectionSet.selections[0].arguments.forEach(arg => { @@ -868,9 +866,9 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (ocQuery.includes('RETURN')) { const statements = ocQuery.split(' RETURN '); - let entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let body = statements[0].replace("this", entityName); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const body = statements[0].replace("this", entityName); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); ocQuery = body + '\nRETURN ' + returnBlock; } diff --git a/test/TestCases/Case01/outputReference/output.resolver.graphql.cjs b/test/TestCases/Case01/outputReference/output.resolver.graphql.cjs index d6fb6fa..21d16ba 100644 --- a/test/TestCases/Case01/outputReference/output.resolver.graphql.cjs +++ b/test/TestCases/Case01/outputReference/output.resolver.graphql.cjs @@ -14,7 +14,7 @@ const gql = require('graphql-tag'); // GraphQL library to parse the GraphQL quer const useCallSubquery = false; -// 2023-10-10T23:49:35.621Z +// 2024-11-19T00:06:35.400Z const schemaDataModelJSON = `{ "kind": "Document", @@ -3500,7 +3500,7 @@ function resolveGraphDBQueryFromAppSyncEvent(event) { query += event.selectionSetGraphQL; query += '\n}'; - let graphQuery = resolveGraphDBQuery(query); + const graphQuery = resolveGraphDBQuery(query); return graphQuery; } @@ -3537,7 +3537,6 @@ function getTypeAlias(typeName) { else return alias; } - function getSchemaInputTypeArgs (inputType, schemaInfo) { @@ -3845,16 +3844,16 @@ function createQueryFunctionMatchStatement(obj, matchStatements, querySchemaInfo gq = gq.replace('$' + arg.name.value, arg.value.value); }); - matchStatements.push(gq); + matchStatements.push(gq); } else { let { queryArguments, where } = getQueryArguments(obj.definitions[0].selectionSet.selections[0].arguments, querySchemaInfo); if (queryArguments.length > 0) { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias}{${queryArguments}})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`{${queryArguments}})${where}`); } else { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`)${where}`); } if (querySchemaInfo.argOptionsLimit != null) @@ -3956,7 +3955,6 @@ function createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath) { refactored.queryMatch = 'OPTIONAL ' + refactored.queryMatch; matchStatements.push(refactored.queryMatch); - let lastNamePathContent = ''; if ( refactored.returnAggregation != '' ) { const thisWithId = withStatements.push({carryOver: refactored.returnCarryOver, inLevel: '', content: `${refactored.returnAggregation} AS ${refactored.inLevel}`}) -1; let i = withStatements.findIndex(({carryOver}) => carryOver.startsWith(lastNamePath)); @@ -3996,7 +3994,7 @@ function createQueryFieldLeafStatement(fieldSchemaInfo, lastNamePath) { createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath); } } else { - withStatements[i].content += ' ' + lastNamePath + '.' + fieldSchemaInfo.alias; + withStatements[i].content += ' ' + lastNamePath + '.' + `\`${fieldSchemaInfo.alias}\``; } } } @@ -4021,9 +4019,9 @@ function createTypeFieldStatementAndRecurse(e, fieldSchemaInfo, lastNamePath, la if (schemaTypeInfo.isRelationship) { if (schemaTypeInfo.relationship.direction === 'IN') { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } else { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } } const thisWithId = withStatements.push({carryOver: schemaTypeInfo.pathName, inLevel: '', content: ''}) - 1; @@ -4092,7 +4090,7 @@ function finalizeGraphQuery(matchStatements, withStatements, returnString) { // make a string out of match statements let ocMatchStatements = ''; matchStatements.forEach(e => { - ocMatchStatements += e + '\n'; + ocMatchStatements += e + '\n'; }); ocMatchStatements = ocMatchStatements.substring(0, ocMatchStatements.length - 1); @@ -4190,21 +4188,21 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // createNode if (querySchemaInfo.name.startsWith('createNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let ocQuery = `CREATE (${nodeName}:${querySchemaInfo.returnTypeAlias} {${inputFields.fields}})\nRETURN ${returnBlock}`; + const ocQuery = `CREATE (${nodeName}:\`${querySchemaInfo.returnTypeAlias}\` {${inputFields.fields}})\nRETURN ${returnBlock}`; return ocQuery; } // updateNode if (querySchemaInfo.name.startsWith('updateNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeID = inputFields.graphIdValue; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeID = inputFields.graphIdValue; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); @@ -4212,7 +4210,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // :( SET += is not working, so let's work around it. //let ocQuery = `MATCH (${nodeName}) WHERE ID(${nodeName}) = '${nodeID}' SET ${nodeName} += {${inputFields}} RETURN ${returnBlock}`; // workaround: - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4221,17 +4219,17 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { setString = setString.substring(0, setString.length - 1); let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } // deleteNode if (querySchemaInfo.name.startsWith('deleteNode') && querySchemaInfo.graphQuery == null) { - let nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; return ocQuery; } @@ -4239,10 +4237,10 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('connectNode') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let egdgeTypeAlias = getTypeAlias(edgeType); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const egdgeTypeAlias = getTypeAlias(edgeType); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); let paramFromId = edgeName + '_' + 'whereFromId'; let paramToId = edgeName + '_' + 'whereToId'; @@ -4250,11 +4248,11 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { Object.assign(parameters, {[paramToId]: toID}); if (obj.definitions[0].selectionSet.selections[0].arguments.length > 2) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; return ocQuery; } else { - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`]->(to)\nRETURN ${returnBlock}`; return ocQuery; } } @@ -4265,13 +4263,13 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; let edgeType = querySchemaInfo.name.match(new RegExp('updateEdge' + "(.*)" + 'From'))[1]; let egdgeTypeAlias = getTypeAlias(edgeType); - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${edgeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4279,12 +4277,12 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { }); setString = setString.substring(0, setString.length - 1); - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from)-[${edgeName}:$\`${egdgeTypeAlias}\`]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } @@ -4292,14 +4290,14 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('deleteEdge') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; + const ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; return ocQuery; } @@ -4309,7 +4307,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let ocQuery = querySchemaInfo.graphQuery; if (ocQuery.includes('$input')) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); ocQuery = ocQuery.replace('$input', inputFields.fields); } else { obj.definitions[0].selectionSet.selections[0].arguments.forEach(arg => { @@ -4319,9 +4317,9 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (ocQuery.includes('RETURN')) { const statements = ocQuery.split(' RETURN '); - let entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let body = statements[0].replace("this", entityName); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const body = statements[0].replace("this", entityName); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); ocQuery = body + '\nRETURN ' + returnBlock; } diff --git a/test/TestCases/Case01/outputReference/output.resolver.graphql.js b/test/TestCases/Case01/outputReference/output.resolver.graphql.js index 68dd1cf..21d16ba 100644 --- a/test/TestCases/Case01/outputReference/output.resolver.graphql.js +++ b/test/TestCases/Case01/outputReference/output.resolver.graphql.js @@ -14,7 +14,7 @@ const gql = require('graphql-tag'); // GraphQL library to parse the GraphQL quer const useCallSubquery = false; -// 2023-10-10T23:49:35.620Z +// 2024-11-19T00:06:35.400Z const schemaDataModelJSON = `{ "kind": "Document", @@ -3500,7 +3500,7 @@ function resolveGraphDBQueryFromAppSyncEvent(event) { query += event.selectionSetGraphQL; query += '\n}'; - let graphQuery = resolveGraphDBQuery(query); + const graphQuery = resolveGraphDBQuery(query); return graphQuery; } @@ -3537,7 +3537,6 @@ function getTypeAlias(typeName) { else return alias; } - function getSchemaInputTypeArgs (inputType, schemaInfo) { @@ -3845,16 +3844,16 @@ function createQueryFunctionMatchStatement(obj, matchStatements, querySchemaInfo gq = gq.replace('$' + arg.name.value, arg.value.value); }); - matchStatements.push(gq); + matchStatements.push(gq); } else { let { queryArguments, where } = getQueryArguments(obj.definitions[0].selectionSet.selections[0].arguments, querySchemaInfo); if (queryArguments.length > 0) { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias}{${queryArguments}})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`{${queryArguments}})${where}`); } else { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`)${where}`); } if (querySchemaInfo.argOptionsLimit != null) @@ -3956,7 +3955,6 @@ function createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath) { refactored.queryMatch = 'OPTIONAL ' + refactored.queryMatch; matchStatements.push(refactored.queryMatch); - let lastNamePathContent = ''; if ( refactored.returnAggregation != '' ) { const thisWithId = withStatements.push({carryOver: refactored.returnCarryOver, inLevel: '', content: `${refactored.returnAggregation} AS ${refactored.inLevel}`}) -1; let i = withStatements.findIndex(({carryOver}) => carryOver.startsWith(lastNamePath)); @@ -3996,7 +3994,7 @@ function createQueryFieldLeafStatement(fieldSchemaInfo, lastNamePath) { createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath); } } else { - withStatements[i].content += ' ' + lastNamePath + '.' + fieldSchemaInfo.alias; + withStatements[i].content += ' ' + lastNamePath + '.' + `\`${fieldSchemaInfo.alias}\``; } } } @@ -4021,9 +4019,9 @@ function createTypeFieldStatementAndRecurse(e, fieldSchemaInfo, lastNamePath, la if (schemaTypeInfo.isRelationship) { if (schemaTypeInfo.relationship.direction === 'IN') { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } else { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } } const thisWithId = withStatements.push({carryOver: schemaTypeInfo.pathName, inLevel: '', content: ''}) - 1; @@ -4092,7 +4090,7 @@ function finalizeGraphQuery(matchStatements, withStatements, returnString) { // make a string out of match statements let ocMatchStatements = ''; matchStatements.forEach(e => { - ocMatchStatements += e + '\n'; + ocMatchStatements += e + '\n'; }); ocMatchStatements = ocMatchStatements.substring(0, ocMatchStatements.length - 1); @@ -4190,21 +4188,21 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // createNode if (querySchemaInfo.name.startsWith('createNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let ocQuery = `CREATE (${nodeName}:${querySchemaInfo.returnTypeAlias} {${inputFields.fields}})\nRETURN ${returnBlock}`; + const ocQuery = `CREATE (${nodeName}:\`${querySchemaInfo.returnTypeAlias}\` {${inputFields.fields}})\nRETURN ${returnBlock}`; return ocQuery; } // updateNode if (querySchemaInfo.name.startsWith('updateNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeID = inputFields.graphIdValue; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeID = inputFields.graphIdValue; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); @@ -4212,7 +4210,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // :( SET += is not working, so let's work around it. //let ocQuery = `MATCH (${nodeName}) WHERE ID(${nodeName}) = '${nodeID}' SET ${nodeName} += {${inputFields}} RETURN ${returnBlock}`; // workaround: - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4221,17 +4219,17 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { setString = setString.substring(0, setString.length - 1); let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } // deleteNode if (querySchemaInfo.name.startsWith('deleteNode') && querySchemaInfo.graphQuery == null) { - let nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; return ocQuery; } @@ -4239,10 +4237,10 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('connectNode') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let egdgeTypeAlias = getTypeAlias(edgeType); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const egdgeTypeAlias = getTypeAlias(edgeType); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); let paramFromId = edgeName + '_' + 'whereFromId'; let paramToId = edgeName + '_' + 'whereToId'; @@ -4250,11 +4248,11 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { Object.assign(parameters, {[paramToId]: toID}); if (obj.definitions[0].selectionSet.selections[0].arguments.length > 2) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; return ocQuery; } else { - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`]->(to)\nRETURN ${returnBlock}`; return ocQuery; } } @@ -4265,13 +4263,13 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; let edgeType = querySchemaInfo.name.match(new RegExp('updateEdge' + "(.*)" + 'From'))[1]; let egdgeTypeAlias = getTypeAlias(edgeType); - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${edgeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4279,12 +4277,12 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { }); setString = setString.substring(0, setString.length - 1); - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from)-[${edgeName}:$\`${egdgeTypeAlias}\`]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } @@ -4292,14 +4290,14 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('deleteEdge') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; + const ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; return ocQuery; } @@ -4309,7 +4307,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let ocQuery = querySchemaInfo.graphQuery; if (ocQuery.includes('$input')) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); ocQuery = ocQuery.replace('$input', inputFields.fields); } else { obj.definitions[0].selectionSet.selections[0].arguments.forEach(arg => { @@ -4319,9 +4317,9 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (ocQuery.includes('RETURN')) { const statements = ocQuery.split(' RETURN '); - let entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let body = statements[0].replace("this", entityName); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const body = statements[0].replace("this", entityName); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); ocQuery = body + '\nRETURN ' + returnBlock; } diff --git a/test/TestCases/Case01/queries/Query0001.json b/test/TestCases/Case01/queries/Query0001.json index 1b9506c..93c7cc0 100644 --- a/test/TestCases/Case01/queries/Query0001.json +++ b/test/TestCases/Case01/queries/Query0001.json @@ -3,7 +3,7 @@ "description": "Inference query from return type", "graphql": "query MyQuery {\n getAirport(code: \"SEA\") {\n city \n }\n}", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'SEA'})\nRETURN {city: getAirport_Airport.city} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'SEA'})\nRETURN {city: getAirport_Airport.`city`} LIMIT 1", "result":{ "city": "Seattle" } diff --git a/test/TestCases/Case01/queries/Query0002.json b/test/TestCases/Case01/queries/Query0002.json index be98291..0926dc1 100644 --- a/test/TestCases/Case01/queries/Query0002.json +++ b/test/TestCases/Case01/queries/Query0002.json @@ -3,7 +3,7 @@ "description": "Get neptune _id", "graphql": "query MyQuery {\n getAirport(code: \"SEA\") {\n _id\n }\n }", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'SEA'})\nRETURN {_id:ID(getAirport_Airport)} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'SEA'})\nRETURN {_id:ID(getAirport_Airport)} LIMIT 1", "result":{ "_id": "22" } diff --git a/test/TestCases/Case01/queries/Query0003.json b/test/TestCases/Case01/queries/Query0003.json index d11e2f5..0fd49c6 100644 --- a/test/TestCases/Case01/queries/Query0003.json +++ b/test/TestCases/Case01/queries/Query0003.json @@ -4,7 +4,7 @@ "description": "Nested types single and array, references in and out", "graphql": "query MyQuery {\n getAirport(code: \"YKM\") {\n city\n continentContainsIn {\n desc\n }\n countryContainsIn {\n desc\n }\n airportRoutesOut {\n code\n }\n }\n }", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'YKM'})\nOPTIONAL MATCH (getAirport_Airport)<-[getAirport_Airport_continentContainsIn_contains:contains]-(getAirport_Airport_continentContainsIn:continent)\nOPTIONAL MATCH (getAirport_Airport)<-[getAirport_Airport_countryContainsIn_contains:contains]-(getAirport_Airport_countryContainsIn:country)\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_airportRoutesOut_route:route]->(getAirport_Airport_airportRoutesOut:airport)\nWITH getAirport_Airport, getAirport_Airport_continentContainsIn, getAirport_Airport_countryContainsIn, collect({code: getAirport_Airport_airportRoutesOut.code}) AS getAirport_Airport_airportRoutesOut_collect\nWITH getAirport_Airport, getAirport_Airport_continentContainsIn, getAirport_Airport_airportRoutesOut_collect, {desc: getAirport_Airport_countryContainsIn.desc} AS getAirport_Airport_countryContainsIn_one\nWITH getAirport_Airport, getAirport_Airport_countryContainsIn_one, getAirport_Airport_airportRoutesOut_collect, {desc: getAirport_Airport_continentContainsIn.desc} AS getAirport_Airport_continentContainsIn_one\nRETURN {city: getAirport_Airport.city, continentContainsIn: getAirport_Airport_continentContainsIn_one, countryContainsIn: getAirport_Airport_countryContainsIn_one, airportRoutesOut: getAirport_Airport_airportRoutesOut_collect} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'YKM'})\nOPTIONAL MATCH (getAirport_Airport)<-[getAirport_Airport_continentContainsIn_contains:contains]-(getAirport_Airport_continentContainsIn:`continent`)\nOPTIONAL MATCH (getAirport_Airport)<-[getAirport_Airport_countryContainsIn_contains:contains]-(getAirport_Airport_countryContainsIn:`country`)\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_airportRoutesOut_route:route]->(getAirport_Airport_airportRoutesOut:`airport`)\nWITH getAirport_Airport, getAirport_Airport_continentContainsIn, getAirport_Airport_countryContainsIn, collect({code: getAirport_Airport_airportRoutesOut.`code`}) AS getAirport_Airport_airportRoutesOut_collect\nWITH getAirport_Airport, getAirport_Airport_continentContainsIn, getAirport_Airport_airportRoutesOut_collect, {desc: getAirport_Airport_countryContainsIn.`desc`} AS getAirport_Airport_countryContainsIn_one\nWITH getAirport_Airport, getAirport_Airport_countryContainsIn_one, getAirport_Airport_airportRoutesOut_collect, {desc: getAirport_Airport_continentContainsIn.`desc`} AS getAirport_Airport_continentContainsIn_one\nRETURN {city: getAirport_Airport.`city`, continentContainsIn: getAirport_Airport_continentContainsIn_one, countryContainsIn: getAirport_Airport_countryContainsIn_one, airportRoutesOut: getAirport_Airport_airportRoutesOut_collect} LIMIT 1", "result":{ "airportRoutesOut": [ { diff --git a/test/TestCases/Case01/queries/Query0004.json b/test/TestCases/Case01/queries/Query0004.json index eeafd85..229230f 100644 --- a/test/TestCases/Case01/queries/Query0004.json +++ b/test/TestCases/Case01/queries/Query0004.json @@ -3,7 +3,7 @@ "description": "Get edge properties in nested array", "graphql": "query MyQuery {\n getAirport(code: \"SEA\") {\n airportRoutesOut {\n code\n route {\n dist\n }\n }\n }\n }\n", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'SEA'})\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_airportRoutesOut_route:route]->(getAirport_Airport_airportRoutesOut:airport)\nWITH getAirport_Airport, getAirport_Airport_airportRoutesOut, {dist: getAirport_Airport_airportRoutesOut_route.dist} AS getAirport_Airport_airportRoutesOut_route_one\nWITH getAirport_Airport, collect({code: getAirport_Airport_airportRoutesOut.code, route: getAirport_Airport_airportRoutesOut_route_one}) AS getAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getAirport_Airport_airportRoutesOut_collect} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'SEA'})\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_airportRoutesOut_route:route]->(getAirport_Airport_airportRoutesOut:`airport`)\nWITH getAirport_Airport, getAirport_Airport_airportRoutesOut, {dist: getAirport_Airport_airportRoutesOut_route.`dist`} AS getAirport_Airport_airportRoutesOut_route_one\nWITH getAirport_Airport, collect({code: getAirport_Airport_airportRoutesOut.`code`, route: getAirport_Airport_airportRoutesOut_route_one}) AS getAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getAirport_Airport_airportRoutesOut_collect} LIMIT 1", "result": { "airportRoutesOut": [ { diff --git a/test/TestCases/Case01/queries/Query0005.json b/test/TestCases/Case01/queries/Query0005.json index f0dde38..a707faa 100644 --- a/test/TestCases/Case01/queries/Query0005.json +++ b/test/TestCases/Case01/queries/Query0005.json @@ -3,7 +3,7 @@ "description": "Type with graph query returning a scalar", "graphql": "query MyQuery {\n getAirport(code: \"SEA\") {\n outboundRoutesCount\n }\n }\n", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'SEA'})\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_outboundRoutesCount_r:route]->(getAirport_Airport_outboundRoutesCount_a)\nWITH getAirport_Airport, count(getAirport_Airport_outboundRoutesCount_r) AS getAirport_Airport_outboundRoutesCount\nRETURN {outboundRoutesCount:getAirport_Airport_outboundRoutesCount} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'SEA'})\nOPTIONAL MATCH (getAirport_Airport)-[getAirport_Airport_outboundRoutesCount_r:route]->(getAirport_Airport_outboundRoutesCount_a)\nWITH getAirport_Airport, count(getAirport_Airport_outboundRoutesCount_r) AS getAirport_Airport_outboundRoutesCount\nRETURN {outboundRoutesCount:getAirport_Airport_outboundRoutesCount} LIMIT 1", "result": { "outboundRoutesCount": 122 } diff --git a/test/TestCases/Case01/queries/Query0006.json b/test/TestCases/Case01/queries/Query0006.json index d7376e2..ccdd6dd 100644 --- a/test/TestCases/Case01/queries/Query0006.json +++ b/test/TestCases/Case01/queries/Query0006.json @@ -3,7 +3,7 @@ "description": "Map type name to different graph db property name", "graphql": "query MyQuery {\n getAirport(code: \"SEA\") {\n desc2\n }\n }\n", "parameters": {}, - "resolved": "MATCH (getAirport_Airport:airport{code:'SEA'})\nRETURN {desc2: getAirport_Airport.desc} LIMIT 1", + "resolved": "MATCH (getAirport_Airport:`airport`{code:'SEA'})\nRETURN {desc2: getAirport_Airport.`desc`} LIMIT 1", "result":{ "desc2": "Seattle-Tacoma" } diff --git a/test/TestCases/Case01/queries/Query0007.json b/test/TestCases/Case01/queries/Query0007.json index 61e5701..f41436d 100644 --- a/test/TestCases/Case01/queries/Query0007.json +++ b/test/TestCases/Case01/queries/Query0007.json @@ -3,7 +3,7 @@ "description": "Query using a graphQuery returing a type", "graphql": "query MyQuery {\n getAirportConnection(fromCode: \"SEA\", toCode: \"BLQ\") {\n city\n code\n }\n }\n", "parameters": {}, - "resolved": "MATCH (:airport{code: 'SEA'})-[:route]->(getAirportConnection_Airport:airport)-[:route]->(:airport{code:'BLQ'})\nRETURN {city: getAirportConnection_Airport.city, code: getAirportConnection_Airport.code} LIMIT 1", + "resolved": "MATCH (:airport{code: 'SEA'})-[:route]->(getAirportConnection_Airport:airport)-[:route]->(:airport{code:'BLQ'})\nRETURN {city: getAirportConnection_Airport.`city`, code: getAirportConnection_Airport.`code`} LIMIT 1", "result":{ "code": "LHR", "city": "London" diff --git a/test/TestCases/Case01/queries/Query0011.json b/test/TestCases/Case01/queries/Query0011.json index 401bc9e..2a1f32c 100644 --- a/test/TestCases/Case01/queries/Query0011.json +++ b/test/TestCases/Case01/queries/Query0011.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirport_Airport_code": "SEA" }, - "resolved": "MATCH (getNodeAirport_Airport:airport{code: $getNodeAirport_Airport_code})\nRETURN {city: getNodeAirport_Airport.city} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`{code: $getNodeAirport_Airport_code})\nRETURN {city: getNodeAirport_Airport.`city`} LIMIT 1", "result":{ "city": "Seattle" } diff --git a/test/TestCases/Case01/queries/Query0012.json b/test/TestCases/Case01/queries/Query0012.json index 169b2b0..d4f6302 100644 --- a/test/TestCases/Case01/queries/Query0012.json +++ b/test/TestCases/Case01/queries/Query0012.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirport_Airport_code": "SEA" }, - "resolved": "MATCH (getNodeAirport_Airport:airport{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_airportRoutesOut_route:route]->(getNodeAirport_Airport_airportRoutesOut:airport)\nWITH getNodeAirport_Airport, collect({code: getNodeAirport_Airport_airportRoutesOut.code})[..2] AS getNodeAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getNodeAirport_Airport_airportRoutesOut_collect} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_airportRoutesOut_route:route]->(getNodeAirport_Airport_airportRoutesOut:`airport`)\nWITH getNodeAirport_Airport, collect({code: getNodeAirport_Airport_airportRoutesOut.`code`})[..2] AS getNodeAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getNodeAirport_Airport_airportRoutesOut_collect} LIMIT 1", "result":{ "airportRoutesOut": [ { diff --git a/test/TestCases/Case01/queries/Query0013.json b/test/TestCases/Case01/queries/Query0013.json index 14fb3d0..5c8448b 100644 --- a/test/TestCases/Case01/queries/Query0013.json +++ b/test/TestCases/Case01/queries/Query0013.json @@ -6,7 +6,7 @@ "getNodeAirport_Airport_code": "SEA", "getNodeAirport_Airport_airportRoutesOut_code": "LAX" }, - "resolved": "MATCH (getNodeAirport_Airport:airport{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_airportRoutesOut_route:route]->(getNodeAirport_Airport_airportRoutesOut:airport{code: $getNodeAirport_Airport_airportRoutesOut_code})\nWITH getNodeAirport_Airport, collect({city: getNodeAirport_Airport_airportRoutesOut.city}) AS getNodeAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getNodeAirport_Airport_airportRoutesOut_collect, city: getNodeAirport_Airport.city} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_airportRoutesOut_route:route]->(getNodeAirport_Airport_airportRoutesOut:`airport`{code: $getNodeAirport_Airport_airportRoutesOut_code})\nWITH getNodeAirport_Airport, collect({city: getNodeAirport_Airport_airportRoutesOut.`city`}) AS getNodeAirport_Airport_airportRoutesOut_collect\nRETURN {airportRoutesOut: getNodeAirport_Airport_airportRoutesOut_collect, city: getNodeAirport_Airport.`city`} LIMIT 1", "result":{ "airportRoutesOut": [ { diff --git a/test/TestCases/Case01/queries/Query0014.json b/test/TestCases/Case01/queries/Query0014.json index 9800cd9..11cc1a1 100644 --- a/test/TestCases/Case01/queries/Query0014.json +++ b/test/TestCases/Case01/queries/Query0014.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirport_Airport_code": "SEA" }, - "resolved": "MATCH (getNodeAirport_Airport:airport{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_outboundRoutesCount_r:route]->(getNodeAirport_Airport_outboundRoutesCount_a)\nWITH getNodeAirport_Airport, count(getNodeAirport_Airport_outboundRoutesCount_r) AS getNodeAirport_Airport_outboundRoutesCount\nRETURN {outboundRoutesCount:getNodeAirport_Airport_outboundRoutesCount} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`{code: $getNodeAirport_Airport_code})\nOPTIONAL MATCH (getNodeAirport_Airport)-[getNodeAirport_Airport_outboundRoutesCount_r:route]->(getNodeAirport_Airport_outboundRoutesCount_a)\nWITH getNodeAirport_Airport, count(getNodeAirport_Airport_outboundRoutesCount_r) AS getNodeAirport_Airport_outboundRoutesCount\nRETURN {outboundRoutesCount:getNodeAirport_Airport_outboundRoutesCount} LIMIT 1", "result":{ "outboundRoutesCount": 122 } diff --git a/test/TestCases/Case01/queries/Query0015.json b/test/TestCases/Case01/queries/Query0015.json index 3e156f3..c751a9b 100644 --- a/test/TestCases/Case01/queries/Query0015.json +++ b/test/TestCases/Case01/queries/Query0015.json @@ -6,7 +6,7 @@ "createNodeAirport_Airport_code": "NAX", "createNodeAirport_Airport_city": "Reggio Emilia" }, - "resolved": "CREATE (createNodeAirport_Airport:airport {code: $createNodeAirport_Airport_code, city: $createNodeAirport_Airport_city})\nRETURN {code: createNodeAirport_Airport.code}", + "resolved": "CREATE (createNodeAirport_Airport:`airport` {code: $createNodeAirport_Airport_code, city: $createNodeAirport_Airport_city})\nRETURN {code: createNodeAirport_Airport.`code`}", "result":{ "code": "NAX" } diff --git a/test/TestCases/Case01/queries/Query0016.json b/test/TestCases/Case01/queries/Query0016.json index 5628d77..3e7337c 100644 --- a/test/TestCases/Case01/queries/Query0016.json +++ b/test/TestCases/Case01/queries/Query0016.json @@ -6,7 +6,7 @@ "updateNodeAirport_Airport_city": "Seattle", "updateNodeAirport_Airport_whereId": "22" }, - "resolved": "MATCH (updateNodeAirport_Airport)\nWHERE ID(updateNodeAirport_Airport) = $updateNodeAirport_Airport_whereId\nSET updateNodeAirport_Airport.city = $updateNodeAirport_Airport_city\nRETURN {city: updateNodeAirport_Airport.city}", + "resolved": "MATCH (updateNodeAirport_Airport)\nWHERE ID(updateNodeAirport_Airport) = $updateNodeAirport_Airport_whereId\nSET updateNodeAirport_Airport.city = $updateNodeAirport_Airport_city\nRETURN {city: updateNodeAirport_Airport.`city`}", "result":{ "city": "Seattle" } diff --git a/test/TestCases/Case01/queries/Query0017.json b/test/TestCases/Case01/queries/Query0017.json index ffce41a..d9d4c92 100644 --- a/test/TestCases/Case01/queries/Query0017.json +++ b/test/TestCases/Case01/queries/Query0017.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirport_Airport_whereId": "22" }, - "resolved": "MATCH (getNodeAirport_Airport:airport) WHERE ID(getNodeAirport_Airport) = $getNodeAirport_Airport_whereId\nRETURN {city: getNodeAirport_Airport.city} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`) WHERE ID(getNodeAirport_Airport) = $getNodeAirport_Airport_whereId\nRETURN {city: getNodeAirport_Airport.`city`} LIMIT 1", "result":{ "city": "Seattle" } diff --git a/test/TestCases/Case01/queries/Query0018.json b/test/TestCases/Case01/queries/Query0018.json index d33ffa6..4798be8 100644 --- a/test/TestCases/Case01/queries/Query0018.json +++ b/test/TestCases/Case01/queries/Query0018.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirports_Airport_code": "SEA" }, - "resolved": "MATCH (getNodeAirports_Airport:airport{code: $getNodeAirports_Airport_code})\nWITH getNodeAirports_Airport LIMIT 1\nRETURN collect({city: getNodeAirports_Airport.city})[..1]", + "resolved": "MATCH (getNodeAirports_Airport:`airport`{code: $getNodeAirports_Airport_code})\nWITH getNodeAirports_Airport LIMIT 1\nRETURN collect({city: getNodeAirports_Airport.`city`})[..1]", "result":[ { "city": "Seattle" diff --git a/test/TestCases/Case01/queries/Query0019.json b/test/TestCases/Case01/queries/Query0019.json index 596d0e4..e403313 100644 --- a/test/TestCases/Case01/queries/Query0019.json +++ b/test/TestCases/Case01/queries/Query0019.json @@ -5,7 +5,7 @@ "parameters": { "getNodeAirport_Airport_code": "SEA" }, - "resolved": "MATCH (getNodeAirport_Airport:airport{code: $getNodeAirport_Airport_code})\nRETURN {_id:ID(getNodeAirport_Airport), city: getNodeAirport_Airport.city, elev: getNodeAirport_Airport.elev, runways: getNodeAirport_Airport.runways, lat: getNodeAirport_Airport.lat, lon: getNodeAirport_Airport.lon} LIMIT 1", + "resolved": "MATCH (getNodeAirport_Airport:`airport`{code: $getNodeAirport_Airport_code})\nRETURN {_id:ID(getNodeAirport_Airport), city: getNodeAirport_Airport.`city`, elev: getNodeAirport_Airport.`elev`, runways: getNodeAirport_Airport.`runways`, lat: getNodeAirport_Airport.`lat`, lon: getNodeAirport_Airport.`lon`} LIMIT 1", "result":{ "city": "Seattle", "elev": 432, diff --git a/test/TestCases/Case07/outputReference/output.resolver.graphql.js b/test/TestCases/Case07/outputReference/output.resolver.graphql.js index 1a6a30d..0ec193c 100644 --- a/test/TestCases/Case07/outputReference/output.resolver.graphql.js +++ b/test/TestCases/Case07/outputReference/output.resolver.graphql.js @@ -14,7 +14,7 @@ const gql = require('graphql-tag'); // GraphQL library to parse the GraphQL quer const useCallSubquery = false; -// 2023-10-10T23:49:35.620Z +// 2024-11-19T00:07:26.413Z const schemaDataModelJSON = `{ "kind": "Document", @@ -3532,7 +3532,7 @@ function resolveGraphDBQueryFromAppSyncEvent(event) { query += event.selectionSetGraphQL; query += '\n}'; - let graphQuery = resolveGraphDBQuery(query); + const graphQuery = resolveGraphDBQuery(query); return graphQuery; } @@ -3569,7 +3569,6 @@ function getTypeAlias(typeName) { else return alias; } - function getSchemaInputTypeArgs (inputType, schemaInfo) { @@ -3877,16 +3876,16 @@ function createQueryFunctionMatchStatement(obj, matchStatements, querySchemaInfo gq = gq.replace('$' + arg.name.value, arg.value.value); }); - matchStatements.push(gq); + matchStatements.push(gq); } else { let { queryArguments, where } = getQueryArguments(obj.definitions[0].selectionSet.selections[0].arguments, querySchemaInfo); if (queryArguments.length > 0) { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias}{${queryArguments}})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`{${queryArguments}})${where}`); } else { - matchStatements.push(`MATCH (${querySchemaInfo.pathName}:${querySchemaInfo.returnTypeAlias})${where}`); + matchStatements.push(`MATCH (${querySchemaInfo.pathName}:\`${querySchemaInfo.returnTypeAlias}\`)${where}`); } if (querySchemaInfo.argOptionsLimit != null) @@ -3988,7 +3987,6 @@ function createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath) { refactored.queryMatch = 'OPTIONAL ' + refactored.queryMatch; matchStatements.push(refactored.queryMatch); - let lastNamePathContent = ''; if ( refactored.returnAggregation != '' ) { const thisWithId = withStatements.push({carryOver: refactored.returnCarryOver, inLevel: '', content: `${refactored.returnAggregation} AS ${refactored.inLevel}`}) -1; let i = withStatements.findIndex(({carryOver}) => carryOver.startsWith(lastNamePath)); @@ -4028,7 +4026,7 @@ function createQueryFieldLeafStatement(fieldSchemaInfo, lastNamePath) { createQueryFieldMatchStatement(fieldSchemaInfo, lastNamePath); } } else { - withStatements[i].content += ' ' + lastNamePath + '.' + fieldSchemaInfo.alias; + withStatements[i].content += ' ' + lastNamePath + '.' + `\`${fieldSchemaInfo.alias}\``; } } } @@ -4053,9 +4051,9 @@ function createTypeFieldStatementAndRecurse(e, fieldSchemaInfo, lastNamePath, la if (schemaTypeInfo.isRelationship) { if (schemaTypeInfo.relationship.direction === 'IN') { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } else { - matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:${schemaTypeInfo.typeAlias}${queryArguments})`); + matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`); } } const thisWithId = withStatements.push({carryOver: schemaTypeInfo.pathName, inLevel: '', content: ''}) - 1; @@ -4124,7 +4122,7 @@ function finalizeGraphQuery(matchStatements, withStatements, returnString) { // make a string out of match statements let ocMatchStatements = ''; matchStatements.forEach(e => { - ocMatchStatements += e + '\n'; + ocMatchStatements += e + '\n'; }); ocMatchStatements = ocMatchStatements.substring(0, ocMatchStatements.length - 1); @@ -4222,21 +4220,21 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // createNode if (querySchemaInfo.name.startsWith('createNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let ocQuery = `CREATE (${nodeName}:${querySchemaInfo.returnTypeAlias} {${inputFields.fields}})\nRETURN ${returnBlock}`; + const ocQuery = `CREATE (${nodeName}:\`${querySchemaInfo.returnTypeAlias}\` {${inputFields.fields}})\nRETURN ${returnBlock}`; return ocQuery; } // updateNode if (querySchemaInfo.name.startsWith('updateNode') && querySchemaInfo.graphQuery == null) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); - let nodeID = inputFields.graphIdValue; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const nodeID = inputFields.graphIdValue; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${nodeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); @@ -4244,7 +4242,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { // :( SET += is not working, so let's work around it. //let ocQuery = `MATCH (${nodeName}) WHERE ID(${nodeName}) = '${nodeID}' SET ${nodeName} += {${inputFields}} RETURN ${returnBlock}`; // workaround: - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4253,17 +4251,17 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { setString = setString.substring(0, setString.length - 1); let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } // deleteNode if (querySchemaInfo.name.startsWith('deleteNode') && querySchemaInfo.graphQuery == null) { - let nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; - let nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const nodeID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; + const nodeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let param = nodeName + '_' + 'whereId'; Object.assign(parameters, {[param]: nodeID}); - let ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; + const ocQuery = `MATCH (${nodeName})\nWHERE ID(${nodeName}) = $${param}\nDETACH DELETE ${nodeName}\nRETURN true`; return ocQuery; } @@ -4271,10 +4269,10 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('connectNode') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let egdgeTypeAlias = getTypeAlias(edgeType); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const edgeType = querySchemaInfo.name.match(new RegExp('Edge' + "(.*)" + ''))[1]; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const egdgeTypeAlias = getTypeAlias(edgeType); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); let paramFromId = edgeName + '_' + 'whereFromId'; let paramToId = edgeName + '_' + 'whereToId'; @@ -4282,11 +4280,11 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { Object.assign(parameters, {[paramToId]: toID}); if (obj.definitions[0].selectionSet.selections[0].arguments.length > 2) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`{${inputFields.fields}}]->(to)\nRETURN ${returnBlock}`; return ocQuery; } else { - let ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from), (to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nCREATE (from)-[${edgeName}:\`${egdgeTypeAlias}\`]->(to)\nRETURN ${returnBlock}`; return ocQuery; } } @@ -4297,13 +4295,13 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; let edgeType = querySchemaInfo.name.match(new RegExp('updateEdge' + "(.*)" + 'From'))[1]; let egdgeTypeAlias = getTypeAlias(edgeType); - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[2].value.fields, querySchemaInfo); + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; let returnBlock = `ID(${edgeName})`; if (obj.definitions[0].selectionSet.selections[0].selectionSet != undefined) { returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); } - let propertyList = inputFields.fields.split(', '); + const propertyList = inputFields.fields.split(', '); let setString = ''; propertyList.forEach(property => { let kv = property.split(': '); @@ -4311,12 +4309,12 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { }); setString = setString.substring(0, setString.length - 1); - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}:${egdgeTypeAlias}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; + const ocQuery = `MATCH (from)-[${edgeName}:$\`${egdgeTypeAlias}\`]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nSET ${setString}\nRETURN ${returnBlock}`; return ocQuery; } @@ -4324,14 +4322,14 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (querySchemaInfo.name.startsWith('deleteEdge') && querySchemaInfo.graphQuery == null) { let fromID = obj.definitions[0].selectionSet.selections[0].arguments[0].value.value; let toID = obj.definitions[0].selectionSet.selections[0].arguments[1].value.value; - let edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const edgeName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let paramFromId = edgeName + '_' + 'whereFromId'; - let paramToId = edgeName + '_' + 'whereToId'; + const paramFromId = edgeName + '_' + 'whereFromId'; + const paramToId = edgeName + '_' + 'whereToId'; Object.assign(parameters, {[paramFromId]: fromID}); Object.assign(parameters, {[paramToId]: toID}); - let ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; + const ocQuery = `MATCH (from)-[${edgeName}]->(to)\nWHERE ID(from) = $${paramFromId} AND ID(to) = $${paramToId}\nDELETE ${edgeName}\nRETURN true`; return ocQuery; } @@ -4341,7 +4339,7 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { let ocQuery = querySchemaInfo.graphQuery; if (ocQuery.includes('$input')) { - let inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); + const inputFields = transformFunctionInputParameters(obj.definitions[0].selectionSet.selections[0].arguments[0].value.fields, querySchemaInfo); ocQuery = ocQuery.replace('$input', inputFields.fields); } else { obj.definitions[0].selectionSet.selections[0].arguments.forEach(arg => { @@ -4351,9 +4349,9 @@ function resolveGrapgDBqueryForGraphQLMutation (obj, querySchemaInfo) { if (ocQuery.includes('RETURN')) { const statements = ocQuery.split(' RETURN '); - let entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; - let body = statements[0].replace("this", entityName); - let returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); + const entityName = querySchemaInfo.name + '_' + querySchemaInfo.returnType; + const body = statements[0].replace("this", entityName); + const returnBlock = returnStringOnly(obj.definitions[0].selectionSet.selections[0].selectionSet.selections, querySchemaInfo); ocQuery = body + '\nRETURN ' + returnBlock; }