Skip to content

Commit

Permalink
FAI-6878: Add GraphQL request compression (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-faros committed Jul 9, 2023
1 parent c8c62d2 commit 9dc2cf6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {AxiosInstance, AxiosRequestConfig} from 'axios';
import * as gql from 'graphql';
import {get as traverse, isEmpty, unset} from 'lodash';
import pino, {Logger} from 'pino';
import {promisify} from 'util';
import VError from 'verror';
import * as zlib from 'zlib';

import {makeAxiosInstanceWithRetry} from './axios';
import {wrapApiError} from './errors';
Expand All @@ -22,6 +24,8 @@ import {
} from './types';
import {Utils} from './utils';

const gzip = promisify(zlib.gzip);

export const DEFAULT_AXIOS_CONFIG: AxiosRequestConfig = {timeout: 60000};

export const GRAPH_VERSION_HEADER = 'x-faros-graph-version';
Expand All @@ -34,7 +38,7 @@ export class FarosClient {

constructor(
cfg: FarosClientConfig,
logger: Logger = pino({name: 'faros-client'}),
readonly logger: Logger = pino({name: 'faros-client'}),
axiosConfig: AxiosRequestConfig = DEFAULT_AXIOS_CONFIG
) {
const url = Utils.urlWithoutTrailingSlashes(cfg.url);
Expand Down Expand Up @@ -190,12 +194,34 @@ export class FarosClient {
variables?: any
): Promise<any> {
try {
const req = variables ? {query, variables} : {query};
let req: any = variables ? {query, variables} : {query};
let doCompression = this.graphVersion === GraphVersion.V2
&& Buffer.byteLength(query, 'utf8') > 10 * 1024; // 10KB
if (doCompression) {
try {
const input = Buffer.from(JSON.stringify(req), 'utf8');
req = await gzip(input);
this.logger.debug(
`Compressed graphql request from ${input.length} `
+ `to ${req.length} bytes`
);
} catch (e) {
// gzip failed, send uncompressed
this.logger.warn(e, 'failed to compress graphql request');
doCompression = false;
}
}
const queryParams = this.queryParameters();
const urlSuffix = queryParams ? `?${queryParams}` : '';
const headers: any = {};
if (doCompression) {
headers['content-encoding'] = 'gzip';
headers['content-type'] = 'application/json';
}
const {data} = await this.api.post(
`/graphs/${graph}/graphql${urlSuffix}`,
req
req,
{headers}
);
return data;
} catch (err: any) {
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ describe('index', () => {
expect(mockPost).toHaveBeenNthCalledWith(1, '/graphs/graph/graphql', {
query: expectedQuery,
variables: {pageSize: 1},
});
}, expect.anything());
expect(mockPost).toHaveBeenNthCalledWith(2, '/graphs/graph/graphql', {
query: expectedQuery,
variables: {cursor: 'abc', pageSize: 1},
});
}, expect.anything());
});

test('query with invalid operation', async () => {
Expand Down

0 comments on commit 9dc2cf6

Please sign in to comment.