use libcurl (#5611)

This commit is contained in:
Jack Kavanagh 2023-01-05 11:08:53 +01:00 committed by GitHub
parent 6dffc7fcf2
commit 9e1e05a8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,17 +13,15 @@ import ReactDOM from 'react-dom';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import { useLocalStorage } from 'react-use'; import { useLocalStorage } from 'react-use';
import { jarFromCookies } from '../../../../common/cookies'; import { CONTENT_TYPE_JSON } from '../../../../common/constants';
import { database as db } from '../../../../common/database';
import { markdownToHTML } from '../../../../common/markdown-to-html'; import { markdownToHTML } from '../../../../common/markdown-to-html';
import { jsonParseOr } from '../../../../common/misc'; import { jsonParseOr } from '../../../../common/misc';
import { getRenderContext, render, RENDER_PURPOSE_SEND } from '../../../../common/render';
import type { ResponsePatch } from '../../../../main/network/libcurl-promise'; import type { ResponsePatch } from '../../../../main/network/libcurl-promise';
import * as models from '../../../../models'; import * as models from '../../../../models';
import type { Request } from '../../../../models/request'; import type { Request } from '../../../../models/request';
import { axiosRequest } from '../../../../network/axios-request'; import * as network from '../../../../network/network';
import { jsonPrettify } from '../../../../utils/prettify/json'; import { jsonPrettify } from '../../../../utils/prettify/json';
import { setDefaultProtocol } from '../../../../utils/url/protocol';
import { buildQueryStringFromParams, joinUrlAndQueryString } from '../../../../utils/url/querystring';
import { selectSettings } from '../../../redux/selectors'; import { selectSettings } from '../../../redux/selectors';
import { Dropdown } from '../../base/dropdown/dropdown'; import { Dropdown } from '../../base/dropdown/dropdown';
import { DropdownButton } from '../../base/dropdown/dropdown-button'; import { DropdownButton } from '../../base/dropdown/dropdown-button';
@ -47,12 +45,10 @@ const isOperationDefinition = (def: DefinitionNode): def is OperationDefinitionN
const fetchGraphQLSchemaForRequest = async ({ const fetchGraphQLSchemaForRequest = async ({
requestId, requestId,
environmentId, environmentId,
workspaceId,
url, url,
}: { }: {
requestId: string; requestId: string;
environmentId: string; environmentId: string;
workspaceId: string;
url: string; url: string;
}) => { }) => {
if (!url) { if (!url) {
@ -66,54 +62,26 @@ const fetchGraphQLSchemaForRequest = async ({
} }
try { try {
const renderContext = await getRenderContext({
request,
environmentId,
purpose: RENDER_PURPOSE_SEND,
});
const workspaceCookieJar = await models.cookieJar.getOrCreateForParentId(
workspaceId
);
const rendered = await render( const bodyJson = JSON.stringify({
{
url: request.url,
headers: request.headers,
authentication: request.authentication,
parameters: request.parameters,
workspaceCookieJar,
},
renderContext
);
const queryString = buildQueryStringFromParams(rendered.parameters);
const enabledHeaders: Record<string, string> = rendered.headers
.filter(({ name, disabled }) => Boolean(name) && !disabled)
.reduce(
(
acc: { [key: string]: string },
{ name, value }: Request['headers'][0]
) => ({ ...acc, [name.toLowerCase() || '']: value || '' }),
{}
);
if (request.settingSendCookies && workspaceCookieJar.cookies.length) {
const jar = jarFromCookies(workspaceCookieJar.cookies);
const cookieHeader = jar.getCookieStringSync(url);
if (cookieHeader) {
enabledHeaders['cookie'] = cookieHeader;
}
}
const response = await axiosRequest({
url: setDefaultProtocol(joinUrlAndQueryString(rendered.url, queryString)),
method: 'POST',
headers: { 'Content-Type': 'application/json', ...enabledHeaders },
data: {
query: getIntrospectionQuery(), query: getIntrospectionQuery(),
}, operationName: 'IntrospectionQuery',
}); });
const introspectionRequest = await db.upsert(
Object.assign({}, request, {
_id: request._id + '.graphql',
settingMaxTimelineDataSize: 5000,
parentId: request._id,
isPrivate: true,
// So it doesn't get synced or exported
body: {
mimeType: CONTENT_TYPE_JSON,
text: bodyJson,
},
}),
);
const response = await network.send(introspectionRequest._id, environmentId);
const statusCode = response.statusCode || 0;
if (!response) { if (!response) {
return { return {
schemaFetchError: { schemaFetchError: {
@ -121,21 +89,22 @@ const fetchGraphQLSchemaForRequest = async ({
}, },
}; };
} }
if (response.status < 200 || response.status >= 300) { if (statusCode < 200 || statusCode >= 300) {
const renderedURL = response.request.res.responseUrl || url; const renderedURL = response.url || request.url;
return { return {
schemaFetchError: { schemaFetchError: {
message: `Got status ${response.status} fetching schema from "${renderedURL}"`, message: `Got status ${statusCode} fetching schema from "${renderedURL}"`,
}, },
}; };
} }
if (response.data.data) { const bodyBuffer = models.response.getBodyBuffer(response);
return { schema: buildClientSchema(response.data.data) }; if (bodyBuffer) {
const { data } = JSON.parse(bodyBuffer.toString());
return { schema: buildClientSchema(data) };
} }
return { return {
schemaFetchError: { schemaFetchError: {
message: message: 'Something went wrong, no data was received from introspection query',
'Something went wrong, no data was received from introspection query',
}, },
}; };
} catch (err) { } catch (err) {
@ -232,7 +201,6 @@ export const GraphQLEditor: FC<Props> = ({
requestId: request._id, requestId: request._id,
environmentId, environmentId,
url: request.url, url: request.url,
workspaceId,
}); });
isMounted && setSchemaFetchError(newState?.schemaFetchError); isMounted && setSchemaFetchError(newState?.schemaFetchError);
@ -460,7 +428,6 @@ export const GraphQLEditor: FC<Props> = ({
requestId: request._id, requestId: request._id,
environmentId, environmentId,
url: request.url, url: request.url,
workspaceId,
}); });
setSchemaIsFetching(false); setSchemaIsFetching(false);
}} }}