Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Created November 13, 2023 13:50
Show Gist options
  • Save michaelbromley/090f1d4eb0ca0d2f5f73b3e65790a5fe to your computer and use it in GitHub Desktop.
Save michaelbromley/090f1d4eb0ca0d2f5f73b3e65790a5fe to your computer and use it in GitHub Desktop.
Insomnia GraphQL export to Hoppscotch
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 } : {}),
};
}
@michaelbromley
Copy link
Author

A very rudimentary script to convert a JSON file exported by Insomnia to the format expected by Hoppscotch's GraphQL import.

limitations:

  • does not handle nested folders.
  • probably other bugs? Threw this together in 10 mins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment