Created
November 13, 2023 13:50
-
-
Save michaelbromley/090f1d4eb0ca0d2f5f73b3e65790a5fe to your computer and use it in GitHub Desktop.
Insomnia GraphQL export to Hoppscotch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('node:fs'); | |
const insomniaFilePath = './insomnia.json'; | |
const outputFilePath = './hoppscotch.json'; | |
const insomniaGraphqlExport = require(insomniaFilePath); | |
const folders = []; | |
const insomniaFolders = insomniaGraphqlExport.resources.filter( | |
resource => resource._type === 'request_group', | |
); | |
const insomniaRequests = insomniaGraphqlExport.resources.filter(resource => resource._type === 'request'); | |
for (const folder of insomniaFolders) { | |
folders.push({ | |
v: 1, | |
name: folder.name, | |
folders: [], | |
requests: insomniaRequests | |
.filter(r => r.parentId === folder._id) | |
.map(insomniaRequestToHoppscotch) | |
.filter(r => !!r), | |
}); | |
} | |
const hoppscotchExport = [ | |
{ | |
v: 1, | |
name: 'Imported from Insomnia GraphQL', | |
folders, | |
requests: [], | |
}, | |
]; | |
// write to json file | |
const stringified = JSON.stringify(hoppscotchExport, null, 2); | |
// replace {{ interpolation }} with <<interpolation>> | |
const interpolated = stringified.replace(/\{\{ ([a-zA-Z0-9._-]+) +}}/g, '<<$1>>'); | |
fs.writeFileSync(outputFilePath, interpolated); | |
function insomniaRequestToHoppscotch(insomniaRequest) { | |
let query; | |
let variables; | |
try { | |
const parsed = JSON.parse(insomniaRequest.body.text); | |
query = parsed.query; | |
variables = parsed.variables && JSON.stringify(parsed.variables, null, 2); | |
} catch (e) { | |
return null; | |
} | |
const auth = insomniaRequest.authentication?.token | |
? { | |
authType: insomniaRequest.authentication.type, | |
authActive: true, | |
token: insomniaRequest.authentication.token, | |
} | |
: undefined; | |
return { | |
v: 2, | |
name: insomniaRequest.name, | |
url: insomniaRequest.url, | |
query: query, | |
headers: insomniaRequest.headers.map(h => ({ | |
key: h.name, | |
value: h.value, | |
active: true, | |
})), | |
...(variables ? { variables } : {}), | |
...(auth ? { auth } : {}), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very rudimentary script to convert a JSON file exported by Insomnia to the format expected by Hoppscotch's GraphQL import.
limitations: