From 5df9981e20f5df0a6a3497a963365181d77ce201 Mon Sep 17 00:00:00 2001 From: Vaugen Wakeling Date: Fri, 27 Oct 2023 00:34:16 +0100 Subject: [PATCH 1/2] feat: Adding ability to import postman graphql collections Fixes: #790 --- .../src/utils/importers/postman-collection.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index bc7f475e6..d70aac9e8 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -14,6 +14,56 @@ const readFile = (files) => { }); }; +/** + * + * @param {string} query + * @returns {string} + */ +const parseGraphQLQuery = (query) => { + return query + .replace(/\s+/g, ' ') + .replace(/{ /g, '{') + .replace(/ {/g, '{') + .replace(/ }/g, '}') + .replace(/, /g, ',') + .replace(/ : /g, ': ') + .replace(/\n/g, ''); +}; + +const parseGraphQLVariables = (string) => { + const cleanedString = string.replace(/[\n\t]/g, '').replace(/\\"/g, '"'); + const variables = JSON.stringify(JSON.parse(cleanedString)); + return typeof variables === 'string' ? variables : ''; +}; + +const parseGraphQLRequest = (graphqlSource) => { + try { + let queryResultObject = { + query: '', + variables: '' + }; + + if (typeof graphqlSource === 'string') { + graphqlSource = JSON.parse(text); + } + + if (graphqlSource.hasOwnProperty('variables') && graphqlSource.variables !== '') { + queryResultObject.variables = parseGraphQLVariables(graphqlSource.variables); + } + + if (graphqlSource.hasOwnProperty('query') && graphqlSource.query !== '') { + queryResultObject.query = parseGraphQLQuery(graphqlSource.query); + } + + return queryResultObject; + } catch (e) { + return { + query: '', + variables: '' + }; + } +}; + const isItemAFolder = (item) => { return !item.request; }; @@ -133,6 +183,12 @@ const importPostmanV2CollectionItem = (brunoParent, item) => { } } + if (bodyMode === 'graphql') { + brunoRequestItem.type = 'graphql-request'; + brunoRequestItem.request.body.mode = 'graphql'; + brunoRequestItem.request.body.graphql = parseGraphQLRequest(i.request.body.graphql); + } + each(i.request.header, (header) => { brunoRequestItem.request.headers.push({ uid: uuid(), From 99b25fc5b2e93078351b090c4f50adf2bba1cb48 Mon Sep 17 00:00:00 2001 From: Vaugen Wakeling Date: Fri, 27 Oct 2023 17:21:16 +0100 Subject: [PATCH 2/2] feat: Remove unneeded formatting --- .../src/utils/importers/postman-collection.js | 26 ++----------------- 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index d70aac9e8..eceafac20 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -14,28 +14,6 @@ const readFile = (files) => { }); }; -/** - * - * @param {string} query - * @returns {string} - */ -const parseGraphQLQuery = (query) => { - return query - .replace(/\s+/g, ' ') - .replace(/{ /g, '{') - .replace(/ {/g, '{') - .replace(/ }/g, '}') - .replace(/, /g, ',') - .replace(/ : /g, ': ') - .replace(/\n/g, ''); -}; - -const parseGraphQLVariables = (string) => { - const cleanedString = string.replace(/[\n\t]/g, '').replace(/\\"/g, '"'); - const variables = JSON.stringify(JSON.parse(cleanedString)); - return typeof variables === 'string' ? variables : ''; -}; - const parseGraphQLRequest = (graphqlSource) => { try { let queryResultObject = { @@ -48,11 +26,11 @@ const parseGraphQLRequest = (graphqlSource) => { } if (graphqlSource.hasOwnProperty('variables') && graphqlSource.variables !== '') { - queryResultObject.variables = parseGraphQLVariables(graphqlSource.variables); + queryResultObject.variables = graphqlSource.variables; } if (graphqlSource.hasOwnProperty('query') && graphqlSource.query !== '') { - queryResultObject.query = parseGraphQLQuery(graphqlSource.query); + queryResultObject.query = graphqlSource.query; } return queryResultObject;