Skip to content

Commit

Permalink
Add get graph models to Faros client (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypc-faros committed Nov 23, 2022
1 parent 1d1f281 commit d488b15
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 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} from 'lodash';
import pino, {Logger} from 'pino';
import VError from 'verror';

import {makeAxiosInstanceWithRetry} from './axios';
import {wrapApiError} from './errors';
Expand All @@ -10,6 +11,7 @@ import {Schema} from './graphql/types';
import {
Account,
FarosClientConfig,
Model,
NamedQuery,
SecretName,
UpdateAccount,
Expand All @@ -20,9 +22,15 @@ export const DEFAULT_AXIOS_CONFIG: AxiosRequestConfig = {timeout: 60000};

export const GRAPH_VERSION_HEADER = 'x-faros-graph-version';

enum GraphVersion {
V1 = 'v1',
V2 = 'v2',
}

/** Faros API client **/
export class FarosClient {
private readonly api: AxiosInstance;
private readonly graphVersion: GraphVersion;

constructor(
cfg: FarosClientConfig,
Expand All @@ -43,6 +51,8 @@ export class FarosClient {
},
logger
);

this.graphVersion = cfg.useGraphQLV2 ? GraphVersion.V2 : GraphVersion.V1;
}

async tenant(): Promise<string> {
Expand Down Expand Up @@ -89,6 +99,20 @@ export class FarosClient {
}
}

async models(graph: string): Promise<ReadonlyArray<Model>> {
if (this.graphVersion !== GraphVersion.V1) {
throw new VError(`listing models is not supported for ${this.graphVersion}
graphs`);
}

try {
const {data} = await this.api.get(`/graphs/${graph}/models`);
return data.models;
} catch (err: any) {
throw wrapApiError(err, `unable to list models: ${graph}`);
}
}

async namedQuery(name: string): Promise<NamedQuery | undefined> {
try {
const {data} = await this.api.get(`/queries/${name}`);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
Coordinates,
FarosClientConfig,
Location,
Model,
NamedQuery,
SecretName,
UpdateAccount,
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ export interface Coordinates {
lat: number;
lon: number;
}

export interface Model {
name: string;
key: ReadonlyArray<string>;
forwardReferences: any;
backwardReferences: any;
keySchema: any;
dataSchema: any;
}
17 changes: 17 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ describe('client', () => {
expect(res).toBe(false);
});

test('get models', async () => {
const mock = nock(apiUrl)
.get('/graphs/g1/models')
.reply(200, {
models: [
{name: 'model1', key: ['key1A', 'key1B']},
{name: 'model2', key: ['key2A', 'key2B']},
],
});
const res = await client.models('g1');
mock.done();
expect(res).toEqual([
{name: 'model1', key: ['key1A', 'key1B']},
{name: 'model2', key: ['key2A', 'key2B']},
]);
});

test('get query', async () => {
const mock = nock(apiUrl)
.get('/queries/my-query')
Expand Down

0 comments on commit d488b15

Please sign in to comment.