Skip to content

Commit

Permalink
enhance(http): improve json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 12, 2024
1 parent 9338e8c commit 4dd8bd2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
11 changes: 5 additions & 6 deletions packages/executors/http/src/createFormDataFromVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
FormData as DefaultFormData,
} from '@whatwg-node/fetch';
import { extractFiles, isExtractableFile } from 'extract-files';
import { SerializedRequest } from './index.js';
import { isGraphQLUpload } from './isGraphQLUpload.js';
import { jsonStringifyBody, SerializedRequest } from './utils.js';

function collectAsyncIterableValues<T>(
asyncIterable: AsyncIterable<T>,
Expand Down Expand Up @@ -42,11 +42,10 @@ export function createFormDataFromVariables(
},
) {
if (!body.variables) {
return JSON.stringify(body);
return jsonStringifyBody(body);
}
const vars = Object.assign({}, body.variables);
const { clone, files } = extractFiles(
vars,
body.variables,
'variables',
((v: any) =>
isExtractableFile(v) ||
Expand All @@ -56,7 +55,7 @@ export function createFormDataFromVariables(
typeof v?.arrayBuffer === 'function') as any,
);
if (files.size === 0) {
return JSON.stringify(body);
return jsonStringifyBody(body);
}
const map: Record<number, string[]> = {};
const uploads: any[] = [];
Expand All @@ -69,7 +68,7 @@ export function createFormDataFromVariables(
const form = new FormDataCtor();
form.append(
'operations',
JSON.stringify({
jsonStringifyBody({
...body,
variables: clone,
}),
Expand Down
10 changes: 2 additions & 8 deletions packages/executors/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
createGraphQLErrorForAbort,
createResultForAbort,
hashSHA256,
SerializedRequest,
} from './utils.js';

export type SyncFetchFn = (
Expand Down Expand Up @@ -122,13 +123,6 @@ export interface HTTPExecutorOptions {
disposable?: boolean;
}

export type SerializedRequest = {
query?: string;
variables?: Record<string, any>;
operationName?: string;
extensions?: any;
};

export type HeadersConfig = Record<string, string>;

// To prevent event listener warnings
Expand Down Expand Up @@ -257,7 +251,7 @@ export function buildHTTPExecutor(
? request.operationName
: undefined,
extensions:
request.extensions && Object.keys(request.extensions).length > 0
(request.extensions && Object.keys(request.extensions).length > 0)
? request.extensions
: undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/executors/http/src/prepareGETUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SerializedRequest } from '.';
import { SerializedRequest } from './utils.js';

export function prepareGETUrl({
baseUrl = '',
Expand Down
39 changes: 39 additions & 0 deletions packages/executors/http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,42 @@ export function hashSHA256(str: string) {
},
);
}

export type SerializedRequest = {
query?: string;
variables?: Record<string, any>;
operationName?: string;
extensions?: any;
};

// For faster serialization instead of JSON.stringify overhead
export function jsonStringifyBody(body: SerializedRequest) {
let str = '{';
let prev = false;
if (body.query) {
str += `"query":"${body.query.replaceAll('"', '\\"')}"`;
prev = true;
}
if (body.variables) {
if (prev) {
str += ',';
}
str += `"variables":${JSON.stringify(body.variables)}`;
prev = true;
}
if (body.operationName) {
if (prev) {
str += ',';
}
str += `"operationName":"${body.operationName}"`;
prev = true;
}
if (body.extensions) {
if (prev) {
str += ',';
}
str += `"extensions":${JSON.stringify(body.extensions)}`;
}
str += '}';
return str;
}

0 comments on commit 4dd8bd2

Please sign in to comment.