Skip to content

Commit

Permalink
refactor: Migrate 'fetch-graphql-schema' to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-treason committed Nov 10, 2024
1 parent 9c801c2 commit cb749cd
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ const GraphQLVariables = ({ variables, item, collection }) => {
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));

return (
<CodeEditor
value={variables || ''}
onChange={onEdit}
mode="javascript"
onRun={onRun}
onSave={onSave}
height={'100%'}
/>
<CodeEditor value={variables || ''} onChange={onEdit} mode="json" onRun={onRun} onSave={onSave} height={'100%'} />
);
};

Expand Down
15 changes: 14 additions & 1 deletion packages/bruno-app/src/utils/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,20 @@ export const fetchGqlSchema = async (endpoint, environment, request, collection)
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;

ipcRenderer.invoke('fetch-gql-schema', endpoint, environment, request, collection).then(resolve).catch(reject);
// Get current global environment and convert it from Array to Record
const globalEnvState = globalEnvironmentStore.getState();
const globalVariableList = globalEnvState.environments.get(globalEnvState.activeEnvironment)?.variables ?? [];
const globalVariables = globalVariableList.reduce((acc, variable) => {
if (variable.enabled) {
acc[variable.name] = variable.value;
}
return acc;
}, {});

ipcRenderer
.invoke('fetch-gql-schema', endpoint, environment, request, collection, globalVariables)
.then(resolve)
.catch(reject);
});
};

Expand Down
78 changes: 0 additions & 78 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,84 +553,6 @@ const registerNetworkIpc = (mainWindow) => {
});
});

ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, _request, collection) => {
try {
const envVars = getEnvVars(environment);
const collectionRoot = get(collection, 'root', {});
const request = prepareGqlIntrospectionRequest(endpoint, envVars, _request, collectionRoot);

request.timeout = preferencesUtil.getRequestTimeout();

if (!preferencesUtil.shouldVerifyTls()) {
request.httpsAgent = new https.Agent({
rejectUnauthorized: false
});
}

const requestUid = uuid();
const collectionPath = collection.pathname;
const collectionUid = collection.uid;
const runtimeVariables = collection.runtimeVariables;
const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collection.uid);
const scriptingConfig = get(brunoConfig, 'scripts', {});

await runPreRequest(
request,
requestUid,
envVars,
collectionPath,
collectionRoot,
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
);

interpolateVars(request, envVars, collection.runtimeVariables, processEnvVars);
const axiosInstance = await configureRequest(
collection.uid,
request,
envVars,
collection.runtimeVariables,
processEnvVars,
collectionPath
);
const response = await axiosInstance(request);

await runPostResponse(
request,
response,
requestUid,
envVars,
collectionPath,
collectionRoot,
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
);

return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
data: response.data
};
} catch (error) {
if (error.response) {
return {
status: error.response.status,
statusText: error.response.statusText,
headers: error.response.headers,
data: error.response.data
};
}

return Promise.reject(error);
}
});

// Ensure the response dir directory exists
const responseCacheDir = path.join(app.getPath('userData'), 'responseCache');
try {
Expand Down
93 changes: 92 additions & 1 deletion packages/bruno-electron/src/ipc/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/
import { ipcMain, app } from 'electron';
import path from 'node:path';
import { request as executeRequest, FolderItem, RequestItem } from '@usebruno/core';
import { request as executeRequest, FolderItem, request, RequestItem } from '@usebruno/core';
import { CollectionSchema, EnvironmentSchema } from '@usebruno/schema';
import { getIntrospectionQuery } from 'graphql';
const { uuid, safeStringifyJSON } = require('../utils/common');
const { cookieJar } = require('../utils/cookies');
const { getPreferences } = require('../store/preferences');
Expand Down Expand Up @@ -243,6 +244,96 @@ ipcMain.handle(
}
);

ipcMain.handle(
'fetch-gql-schema',
async (event, endpoint, environment, requestItem: RequestItem['request'], collection, globalVariables) => {
const webContents = event.sender;
const cancelToken = uuid();
const abortController = new AbortController();
cancelTokens.set(cancelToken, abortController);

const body = {
mode: 'json',
json: JSON.stringify({
query: getIntrospectionQuery()
})
} as const;

const headers = requestItem.headers;
headers.push({
enabled: true,
name: 'accept',
value: 'application/json'
});

const item: RequestItem = {
depth: 0,
draft: null,
filename: 'graphql-introspection',
name: 'graphql-introspection',
pathname: '',
seq: 0,
type: 'http-request',
uid: uuid(),
request: {
...requestItem,
assertions: [],
tests: '',
script: { req: '', res: '' },
params: [],
headers,
url: endpoint,
body
}
};

const res = await executeRequest(
item,
collection,
globalVariables,
getPreferences(),
cookieJar,
responseDataDir,
cancelToken,
abortController,
// @ts-expect-error Defined in `vite.base.config.js`
BRUNO_VERSION,
'standalone',
environment,
{
updateScriptEnvironment: (payload) => {
webContents.send('main:script-environment-update', payload);
},
cookieUpdated: (payload) => {
webContents.send('main:cookies-update', payload);
},
requestEvent: (payload) => {
webContents.send('main:run-request-event', payload);
},
consoleLog: (payload) => {
webContents.send('main:console-log', payload);
}
}
);
if (res.error) {
throw res.error;
}

cancelTokens.delete(cancelToken);

if (abortController.signal.aborted) {
throw new Error('Request aborted');
}

return {
status: res.response?.statusCode,
headers: res.response?.headers,
size: res.response?.size ?? 0,
data: res.responseBody
};
}
);

ipcMain.handle('cancel-http-request', async (_event, cancelTokenUid) => {
cancelTokens.get(cancelTokenUid)?.abort();
cancelTokens.delete(cancelTokenUid);
Expand Down

0 comments on commit cb749cd

Please sign in to comment.