Skip to content

Commit

Permalink
FAI-6740: Metrics Pipeline - Visibility Type APIs (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-faros authored Jul 27, 2023
1 parent dbee06c commit 41d38a0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
23 changes: 15 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AxiosInstance, AxiosRequestConfig} from 'axios';
import * as gql from 'graphql';
import {get as traverse, isEmpty, unset} from 'lodash';
import pino, {Logger} from 'pino';
import {Dictionary} from 'ts-essentials';
import {promisify} from 'util';
import VError from 'verror';
import * as zlib from 'zlib';
Expand Down Expand Up @@ -36,6 +37,7 @@ export class FarosClient {
private readonly api: AxiosInstance;
readonly graphVersion: GraphVersion;
readonly phantoms: Phantom;
readonly visibility?: string;

constructor(
cfg: FarosClientConfig,
Expand All @@ -61,6 +63,7 @@ export class FarosClient {

this.graphVersion = cfg.useGraphQLV2 ? GraphVersion.V2 : GraphVersion.V1;
this.phantoms = cfg.phantoms || Phantom.IncludeNestedOnly;
this.visibility = cfg.visibility;
}

async tenant(): Promise<string> {
Expand Down Expand Up @@ -185,10 +188,15 @@ export class FarosClient {
}
}

queryParameters(): string | undefined {
return this.graphVersion === GraphVersion.V2
? `phantoms=${this.phantoms}`
: undefined;
queryParameters(): Dictionary<any> {
const result: Dictionary<any> = {};
if (this.graphVersion === GraphVersion.V2) {
result.phantoms = this.phantoms;
if (this.visibility) {
result.visibility = this.visibility;
}
}
return result;
}

private async doGql(
Expand All @@ -215,17 +223,16 @@ export class FarosClient {
doCompression = false;
}
}
const queryParams = this.queryParameters();
const urlSuffix = queryParams ? `?${queryParams}` : '';
const params = this.queryParameters();
const headers: any = {};
if (doCompression) {
headers['content-encoding'] = 'gzip';
headers['content-type'] = 'application/json';
}
const {data} = await this.api.post(
`/graphs/${graph}/graphql${urlSuffix}`,
`/graphs/${graph}/graphql`,
req,
{headers}
{headers, params}
);
return data;
} catch (err: any) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface FarosClientConfig {
readonly apiKey: string;
readonly useGraphQLV2?: boolean;
readonly phantoms?: Phantom;
readonly visibility?: string;
}

export enum GraphVersion {
Expand Down
66 changes: 58 additions & 8 deletions test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';

import {FarosClient, Schema} from '../src';
import {FarosClient, FarosClientConfig, Schema} from '../src';
import {GRAPH_VERSION_HEADER} from '../src/client';
import {Phantom} from '../src/types';

Expand Down Expand Up @@ -179,19 +179,69 @@ describe('client', () => {
});

test('check v2 header value', async () => {
await expectV2Request({
url: apiUrl,
apiKey: 'test-key',
useGraphQLV2: true,
}, true);
});

async function expectV2Request(
cfg: FarosClientConfig,
expected: true | URLSearchParams
) {
expect(cfg.useGraphQLV2).toBeTruthy();
const mock = nock(apiUrl, {
reqheaders: {
// use literal to be sure as variable requires []'s
'x-faros-graph-version': /v2/i,
},
})
.get('/graphs/foobar/graphql/schema')
.reply(200, gqlSchema);

const clientConfig = {url: apiUrl, apiKey: 'test-key', useGraphQLV2: true};
const client = new FarosClient(clientConfig);
await client.gqlSchema('foobar');
.post('/graphs/foobar/graphql')
.query(expected)
.reply(200, {});
const client = new FarosClient(cfg);
await client.gql('foobar', 'query { __schema { types { name } } }');
mock.done();
}

test('v2 query parameters - visibility', async () => {
const expected = new URLSearchParams({
phantoms: Phantom.IncludeNestedOnly,
visibility: 'foobar',
});
const clientConfig = {
url: apiUrl,
apiKey: 'test-key',
useGraphQLV2: true,
visibility: 'foobar',
};
await expectV2Request(clientConfig, expected);
});

test('v2 query parameters - default', async () => {
const expected = new URLSearchParams({
phantoms: Phantom.IncludeNestedOnly,
});
const clientConfig = {
url: apiUrl,
apiKey: 'test-key',
useGraphQLV2: true,
};
await expectV2Request(clientConfig, expected);
});

test('v2 query parameters - custom phantoms', async () => {
const expected = new URLSearchParams({
phantoms: Phantom.Exclude,
});
const clientConfig = {
url: apiUrl,
apiKey: 'test-key',
useGraphQLV2: true,
phantoms: Phantom.Exclude,
};
await expectV2Request(clientConfig, expected);
});

test('gql v2 - default', async () => {
Expand Down Expand Up @@ -232,7 +282,7 @@ describe('client', () => {
const mock = nock(apiUrl)
.post(
'/graphs/g1/graphql',
JSON.stringify({query, variables: {pageSize: 100, after: 'abc'}})
JSON.stringify({query, variables: {pageSize: 100, after: 'abc'}}),
)
.reply(200, {
data: {tms: {tasks: {edges: [{node: {uid: '1'}}, {node: {uid: '2'}}]}}},
Expand Down

0 comments on commit 41d38a0

Please sign in to comment.