-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.ts
97 lines (89 loc) · 2.53 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import type {
DataborgClientOptions,
QueryOptions,
UpdateOptions,
} from 'types/client';
import type { DataborgExchange, ExchangeResult } from 'types/exchange';
import { HttpExchange } from './exchanges/http';
import { ParseExchange } from './exchanges/parse';
import { exchangesFrom } from './exchanges/util';
import { QueryManager } from './QueryManager';
export class DataborgClient {
#queryEndpoint: string;
#updateEndpoint: string;
#headers: Record<string, string>;
#exchange: DataborgExchange;
#queryManager: QueryManager;
name: string;
version: string;
prefixes: Record<string, string>;
constructor({
queryEndpoint,
updateEndpoint,
headers,
prefixes,
name,
version,
exchange,
}: DataborgClientOptions) {
this.#queryEndpoint = queryEndpoint;
this.#updateEndpoint = updateEndpoint ?? queryEndpoint;
this.#headers = headers ?? {};
this.prefixes = prefixes ?? {};
// set public client properties
this.name = name ?? 'DataborgClient';
this.version = version ?? '1.0.0';
// init query manager
this.#queryManager = new QueryManager({ prefixes: this.prefixes });
// init exchange
if (exchange) {
// use provided exchange if given
this.#exchange = exchange;
} else {
// otherwise use default http exchange with parse exchange in chain
const httpEx = new HttpExchange({
queryEndpoint: this.#queryEndpoint,
updateEndpoint: this.#updateEndpoint,
headers: this.#headers,
});
const parseEx = new ParseExchange();
this.#exchange = exchangesFrom(httpEx, parseEx);
}
}
async query({
query,
variables,
options = {},
}: QueryOptions): Promise<ExchangeResult> {
// transform query, substitute vars, validate, etc
const queryWithVars = this.#queryManager.transform({ query, variables });
// execute via exchange
const results = await this.#exchange.execute({
client: this,
queryManager: this.#queryManager,
query: queryWithVars,
options,
});
return results;
}
async update({
query,
variables,
options = {},
}: UpdateOptions): Promise<ExchangeResult> {
// transform query, substitute vars, validate, etc
const updateWithVars = this.#queryManager.transform({
query,
variables,
});
// execute via exchange
const results = await this.#exchange.execute({
client: this,
queryManager: this.#queryManager,
query: updateWithVars,
update: true,
options,
});
return results;
}
}