@vertikalx/vtx-backend-client 3.0.1-dev-max.1 → 3.0.1-dev-max.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api/api-call-headers.ts +0 -35
- package/src/api/backend-response.ts +0 -14
- package/src/api/domains.ts +0 -6
- package/src/api/response-builder.ts +0 -165
- package/src/api/types.ts +0 -9
- package/src/api/vtx-apikey-api.ts +0 -23
- package/src/api/vtx-base-api.ts +0 -2253
- package/src/api/vtx-mobile-api.ts +0 -43
- package/src/api/vtx-web-browser-api.ts +0 -46
- package/src/api/vtx-web-server-api.ts +0 -41
- package/src/client/index.ts +0 -65
- package/src/client/runtime/batcher.ts +0 -275
- package/src/client/runtime/createClient.ts +0 -68
- package/src/client/runtime/error.ts +0 -29
- package/src/client/runtime/fetcher.ts +0 -97
- package/src/client/runtime/generateGraphqlOperation.ts +0 -225
- package/src/client/runtime/index.ts +0 -13
- package/src/client/runtime/linkTypeMap.ts +0 -156
- package/src/client/runtime/typeSelection.ts +0 -95
- package/src/client/runtime/types.ts +0 -69
- package/src/client/schema.ts +0 -8329
- package/src/client/types.ts +0 -10578
- package/src/generated/graphql.ts +0 -12445
- package/src/index.ts +0 -16
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import pjson from "../../package.json";
|
|
3
|
-
import { ValidatedToken } from "../client";
|
|
4
|
-
import { APICallHeaders, DEFAULT_APP_KEY_HEADER, DEFAULT_HEADERS, DEFAULT_SYSTEM_USER_TOKEN_HEADER } from "./api-call-headers";
|
|
5
|
-
import { ITypedBackendResponse } from "./backend-response";
|
|
6
|
-
import { buildErrorResponse, buildResponse } from "./response-builder";
|
|
7
|
-
import { VTXBaseAPI } from "./vtx-base-api";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export class VTXMobileAPI extends VTXBaseAPI{
|
|
14
|
-
|
|
15
|
-
constructor(backendUrl:string, userToken?:string, requestConfig?: { fetch?: typeof fetch }){
|
|
16
|
-
super(backendUrl, VTXMobileAPI.getDefaultHeaders(userToken), requestConfig);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public static getDefaultHeaders(userToken?:string):APICallHeaders{
|
|
20
|
-
|
|
21
|
-
if (userToken){
|
|
22
|
-
return {
|
|
23
|
-
//[DEFAULT_APP_KEY_HEADER]: mobileAppKey,
|
|
24
|
-
[DEFAULT_SYSTEM_USER_TOKEN_HEADER]: userToken,
|
|
25
|
-
...DEFAULT_HEADERS
|
|
26
|
-
}
|
|
27
|
-
}else{
|
|
28
|
-
return {
|
|
29
|
-
//[DEFAULT_APP_KEY_HEADER]: mobileAppKey,
|
|
30
|
-
...DEFAULT_HEADERS
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public async getMobileToken(token: string): Promise<ITypedBackendResponse<ValidatedToken>> {
|
|
36
|
-
return this.execute<ValidatedToken>(
|
|
37
|
-
() => this.sdk.ValidateToken({ input: token }),
|
|
38
|
-
'validateToken',
|
|
39
|
-
(r: any) => !!r?.validateToken?.accessToken,
|
|
40
|
-
'validateToken'
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import pjson from "../../package.json";
|
|
3
|
-
import { APICallHeaders, DEFAULT_APP_KEY_HEADER, DEFAULT_HEADERS, DEFAULT_SYSTEM_USER_TOKEN_HEADER } from "./api-call-headers";
|
|
4
|
-
import { VTXBaseAPI } from "./vtx-base-api";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export class VTXWebBrowserAPI extends VTXBaseAPI{
|
|
11
|
-
|
|
12
|
-
constructor(backendUrl:string, userToken?:string ){
|
|
13
|
-
super(backendUrl, VTXWebBrowserAPI.getDefaultHeaders(userToken));
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public setUserToken(userToken:string|null){
|
|
18
|
-
if (userToken){
|
|
19
|
-
this.headers = {
|
|
20
|
-
...this.headers,
|
|
21
|
-
[DEFAULT_SYSTEM_USER_TOKEN_HEADER]: userToken,
|
|
22
|
-
}
|
|
23
|
-
}else{
|
|
24
|
-
delete this.headers[DEFAULT_SYSTEM_USER_TOKEN_HEADER];
|
|
25
|
-
}
|
|
26
|
-
this.gqlClient.setHeaders(this.headers as Record<string, string>);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public static getDefaultHeaders(userToken?:string):APICallHeaders{
|
|
30
|
-
|
|
31
|
-
if (userToken){
|
|
32
|
-
return {
|
|
33
|
-
...DEFAULT_HEADERS,
|
|
34
|
-
[DEFAULT_SYSTEM_USER_TOKEN_HEADER]: userToken,
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}else{
|
|
38
|
-
return {
|
|
39
|
-
...DEFAULT_HEADERS
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import pjson from "../../package.json";
|
|
3
|
-
import { APICallHeaders, DEFAULT_APP_KEY_HEADER, DEFAULT_HEADERS, DEFAULT_SYSTEM_KEY_NAME_HEADER, DEFAULT_SYSTEM_KEY_VALUE_HEADER, DEFAULT_SYSTEM_USER_TOKEN_HEADER } from "./api-call-headers";
|
|
4
|
-
import { VTXBaseAPI } from "./vtx-base-api";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export class VTXWebServerAPI extends VTXBaseAPI{
|
|
11
|
-
|
|
12
|
-
constructor(backendUrl:string, systemKeyName:string, systemKeyValue:string){
|
|
13
|
-
super(backendUrl, VTXWebServerAPI.getDefaultHeaders(systemKeyName, systemKeyValue));
|
|
14
|
-
|
|
15
|
-
if (VTXBaseAPI.isBrowser()){
|
|
16
|
-
throw new Error('VTXWebServerAPI should NOT be used in Browser');
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
public setUserToken(userToken:string|null){
|
|
20
|
-
if (userToken){
|
|
21
|
-
this.headers = {
|
|
22
|
-
[DEFAULT_SYSTEM_USER_TOKEN_HEADER]: userToken,
|
|
23
|
-
...this.headers
|
|
24
|
-
}
|
|
25
|
-
}else{
|
|
26
|
-
delete this.headers[DEFAULT_SYSTEM_USER_TOKEN_HEADER];
|
|
27
|
-
}
|
|
28
|
-
this.gqlClient.setHeaders(this.headers as Record<string, string>);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public static getDefaultHeaders(systemKeyName:string, systemKeyValue:string):APICallHeaders{
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
[DEFAULT_SYSTEM_KEY_NAME_HEADER]: systemKeyName,
|
|
35
|
-
[DEFAULT_SYSTEM_KEY_VALUE_HEADER]: systemKeyValue,
|
|
36
|
-
...DEFAULT_HEADERS
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
}
|
package/src/client/index.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import type {
|
|
3
|
-
QueryGenqlSelection,
|
|
4
|
-
Query,
|
|
5
|
-
MutationGenqlSelection,
|
|
6
|
-
Mutation,
|
|
7
|
-
} from './schema'
|
|
8
|
-
import {
|
|
9
|
-
linkTypeMap,
|
|
10
|
-
createClient as createClientOriginal,
|
|
11
|
-
generateGraphqlOperation,
|
|
12
|
-
type FieldsSelection,
|
|
13
|
-
type GraphqlOperation,
|
|
14
|
-
type ClientOptions,
|
|
15
|
-
GenqlError,
|
|
16
|
-
} from './runtime'
|
|
17
|
-
export type { FieldsSelection } from './runtime'
|
|
18
|
-
export { GenqlError }
|
|
19
|
-
|
|
20
|
-
import types from './types'
|
|
21
|
-
export * from './schema'
|
|
22
|
-
const typeMap = linkTypeMap(types as any)
|
|
23
|
-
|
|
24
|
-
export interface Client {
|
|
25
|
-
query<R extends QueryGenqlSelection>(
|
|
26
|
-
request: R & { __name?: string },
|
|
27
|
-
): Promise<FieldsSelection<Query, R>>
|
|
28
|
-
|
|
29
|
-
mutation<R extends MutationGenqlSelection>(
|
|
30
|
-
request: R & { __name?: string },
|
|
31
|
-
): Promise<FieldsSelection<Mutation, R>>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const createClient = function (options?: ClientOptions): Client {
|
|
35
|
-
return createClientOriginal({
|
|
36
|
-
url: undefined,
|
|
37
|
-
|
|
38
|
-
...options,
|
|
39
|
-
queryRoot: typeMap.Query!,
|
|
40
|
-
mutationRoot: typeMap.Mutation!,
|
|
41
|
-
subscriptionRoot: typeMap.Subscription!,
|
|
42
|
-
}) as any
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const everything = {
|
|
46
|
-
__scalar: true,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export type QueryResult<fields extends QueryGenqlSelection> = FieldsSelection<
|
|
50
|
-
Query,
|
|
51
|
-
fields
|
|
52
|
-
>
|
|
53
|
-
export const generateQueryOp: (
|
|
54
|
-
fields: QueryGenqlSelection & { __name?: string },
|
|
55
|
-
) => GraphqlOperation = function (fields) {
|
|
56
|
-
return generateGraphqlOperation('query', typeMap.Query!, fields as any)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export type MutationResult<fields extends MutationGenqlSelection> =
|
|
60
|
-
FieldsSelection<Mutation, fields>
|
|
61
|
-
export const generateMutationOp: (
|
|
62
|
-
fields: MutationGenqlSelection & { __name?: string },
|
|
63
|
-
) => GraphqlOperation = function (fields) {
|
|
64
|
-
return generateGraphqlOperation('mutation', typeMap.Mutation!, fields as any)
|
|
65
|
-
}
|
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import type { GraphqlOperation } from './generateGraphqlOperation'
|
|
3
|
-
import { GenqlError } from './error'
|
|
4
|
-
|
|
5
|
-
type Variables = Record<string, any>
|
|
6
|
-
|
|
7
|
-
type QueryError = Error & {
|
|
8
|
-
message: string
|
|
9
|
-
|
|
10
|
-
locations?: Array<{
|
|
11
|
-
line: number
|
|
12
|
-
column: number
|
|
13
|
-
}>
|
|
14
|
-
path?: any
|
|
15
|
-
rid: string
|
|
16
|
-
details?: Record<string, any>
|
|
17
|
-
}
|
|
18
|
-
type Result = {
|
|
19
|
-
data: Record<string, any>
|
|
20
|
-
errors: Array<QueryError>
|
|
21
|
-
}
|
|
22
|
-
type Fetcher = (
|
|
23
|
-
batchedQuery: GraphqlOperation | Array<GraphqlOperation>,
|
|
24
|
-
) => Promise<Array<Result>>
|
|
25
|
-
type Options = {
|
|
26
|
-
batchInterval?: number
|
|
27
|
-
shouldBatch?: boolean
|
|
28
|
-
maxBatchSize?: number
|
|
29
|
-
}
|
|
30
|
-
type Queue = Array<{
|
|
31
|
-
request: GraphqlOperation
|
|
32
|
-
resolve: (...args: Array<any>) => any
|
|
33
|
-
reject: (...args: Array<any>) => any
|
|
34
|
-
}>
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* takes a list of requests (queue) and batches them into a single server request.
|
|
38
|
-
* It will then resolve each individual requests promise with the appropriate data.
|
|
39
|
-
* @private
|
|
40
|
-
* @param {QueryBatcher} client - the client to use
|
|
41
|
-
* @param {Queue} queue - the list of requests to batch
|
|
42
|
-
*/
|
|
43
|
-
function dispatchQueueBatch(client: QueryBatcher, queue: Queue): void {
|
|
44
|
-
let batchedQuery: any = queue.map((item) => item.request)
|
|
45
|
-
|
|
46
|
-
if (batchedQuery.length === 1) {
|
|
47
|
-
batchedQuery = batchedQuery[0]
|
|
48
|
-
}
|
|
49
|
-
(() => {
|
|
50
|
-
try {
|
|
51
|
-
return client.fetcher(batchedQuery);
|
|
52
|
-
} catch(e) {
|
|
53
|
-
return Promise.reject(e);
|
|
54
|
-
}
|
|
55
|
-
})().then((responses: any) => {
|
|
56
|
-
if (queue.length === 1 && !Array.isArray(responses)) {
|
|
57
|
-
if (responses.errors && responses.errors.length) {
|
|
58
|
-
queue[0].reject(
|
|
59
|
-
new GenqlError(responses.errors, responses.data),
|
|
60
|
-
)
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
queue[0].resolve(responses)
|
|
65
|
-
return
|
|
66
|
-
} else if (responses.length !== queue.length) {
|
|
67
|
-
throw new Error('response length did not match query length')
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
for (let i = 0; i < queue.length; i++) {
|
|
71
|
-
if (responses[i].errors && responses[i].errors.length) {
|
|
72
|
-
queue[i].reject(
|
|
73
|
-
new GenqlError(responses[i].errors, responses[i].data),
|
|
74
|
-
)
|
|
75
|
-
} else {
|
|
76
|
-
queue[i].resolve(responses[i])
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
.catch((e) => {
|
|
81
|
-
for (let i = 0; i < queue.length; i++) {
|
|
82
|
-
queue[i].reject(e)
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* creates a list of requests to batch according to max batch size.
|
|
89
|
-
* @private
|
|
90
|
-
* @param {QueryBatcher} client - the client to create list of requests from from
|
|
91
|
-
* @param {Options} options - the options for the batch
|
|
92
|
-
*/
|
|
93
|
-
function dispatchQueue(client: QueryBatcher, options: Options): void {
|
|
94
|
-
const queue = client._queue
|
|
95
|
-
const maxBatchSize = options.maxBatchSize || 0
|
|
96
|
-
client._queue = []
|
|
97
|
-
|
|
98
|
-
if (maxBatchSize > 0 && maxBatchSize < queue.length) {
|
|
99
|
-
for (let i = 0; i < queue.length / maxBatchSize; i++) {
|
|
100
|
-
dispatchQueueBatch(
|
|
101
|
-
client,
|
|
102
|
-
queue.slice(i * maxBatchSize, (i + 1) * maxBatchSize),
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
dispatchQueueBatch(client, queue)
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Create a batcher client.
|
|
111
|
-
* @param {Fetcher} fetcher - A function that can handle the network requests to graphql endpoint
|
|
112
|
-
* @param {Options} options - the options to be used by client
|
|
113
|
-
* @param {boolean} options.shouldBatch - should the client batch requests. (default true)
|
|
114
|
-
* @param {integer} options.batchInterval - duration (in MS) of each batch window. (default 6)
|
|
115
|
-
* @param {integer} options.maxBatchSize - max number of requests in a batch. (default 0)
|
|
116
|
-
* @param {boolean} options.defaultHeaders - default headers to include with every request
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* const fetcher = batchedQuery => fetch('path/to/graphql', {
|
|
120
|
-
* method: 'post',
|
|
121
|
-
* headers: {
|
|
122
|
-
* Accept: 'application/json',
|
|
123
|
-
* 'Content-Type': 'application/json',
|
|
124
|
-
* },
|
|
125
|
-
* body: JSON.stringify(batchedQuery),
|
|
126
|
-
* credentials: 'include',
|
|
127
|
-
* })
|
|
128
|
-
* .then(response => response.json())
|
|
129
|
-
*
|
|
130
|
-
* const client = new QueryBatcher(fetcher, { maxBatchSize: 10 })
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
export class QueryBatcher {
|
|
134
|
-
fetcher: Fetcher
|
|
135
|
-
_options: Options
|
|
136
|
-
_queue: Queue
|
|
137
|
-
|
|
138
|
-
constructor(
|
|
139
|
-
fetcher: Fetcher,
|
|
140
|
-
{
|
|
141
|
-
batchInterval = 6,
|
|
142
|
-
shouldBatch = true,
|
|
143
|
-
maxBatchSize = 0,
|
|
144
|
-
}: Options = {},
|
|
145
|
-
) {
|
|
146
|
-
this.fetcher = fetcher
|
|
147
|
-
this._options = {
|
|
148
|
-
batchInterval,
|
|
149
|
-
shouldBatch,
|
|
150
|
-
maxBatchSize,
|
|
151
|
-
}
|
|
152
|
-
this._queue = []
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Fetch will send a graphql request and return the parsed json.
|
|
157
|
-
* @param {string} query - the graphql query.
|
|
158
|
-
* @param {Variables} variables - any variables you wish to inject as key/value pairs.
|
|
159
|
-
* @param {[string]} operationName - the graphql operationName.
|
|
160
|
-
* @param {Options} overrides - the client options overrides.
|
|
161
|
-
*
|
|
162
|
-
* @return {promise} resolves to parsed json of server response
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* client.fetch(`
|
|
166
|
-
* query getHuman($id: ID!) {
|
|
167
|
-
* human(id: $id) {
|
|
168
|
-
* name
|
|
169
|
-
* height
|
|
170
|
-
* }
|
|
171
|
-
* }
|
|
172
|
-
* `, { id: "1001" }, 'getHuman')
|
|
173
|
-
* .then(human => {
|
|
174
|
-
* // do something with human
|
|
175
|
-
* console.log(human);
|
|
176
|
-
* });
|
|
177
|
-
*/
|
|
178
|
-
fetch(
|
|
179
|
-
query: string,
|
|
180
|
-
variables?: Variables,
|
|
181
|
-
operationName?: string,
|
|
182
|
-
overrides: Options = {},
|
|
183
|
-
): Promise<Result> {
|
|
184
|
-
const request: GraphqlOperation = {
|
|
185
|
-
query,
|
|
186
|
-
}
|
|
187
|
-
const options = Object.assign({}, this._options, overrides)
|
|
188
|
-
|
|
189
|
-
if (variables) {
|
|
190
|
-
request.variables = variables
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (operationName) {
|
|
194
|
-
request.operationName = operationName
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const promise = new Promise<Result>((resolve, reject) => {
|
|
198
|
-
this._queue.push({
|
|
199
|
-
request,
|
|
200
|
-
resolve,
|
|
201
|
-
reject,
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
if (this._queue.length === 1) {
|
|
205
|
-
if (options.shouldBatch) {
|
|
206
|
-
setTimeout(
|
|
207
|
-
() => dispatchQueue(this, options),
|
|
208
|
-
options.batchInterval,
|
|
209
|
-
)
|
|
210
|
-
} else {
|
|
211
|
-
dispatchQueue(this, options)
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
})
|
|
215
|
-
return promise
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Fetch will send a graphql request and return the parsed json.
|
|
220
|
-
* @param {string} query - the graphql query.
|
|
221
|
-
* @param {Variables} variables - any variables you wish to inject as key/value pairs.
|
|
222
|
-
* @param {[string]} operationName - the graphql operationName.
|
|
223
|
-
* @param {Options} overrides - the client options overrides.
|
|
224
|
-
*
|
|
225
|
-
* @return {Promise<Array<Result>>} resolves to parsed json of server response
|
|
226
|
-
*
|
|
227
|
-
* @example
|
|
228
|
-
* client.forceFetch(`
|
|
229
|
-
* query getHuman($id: ID!) {
|
|
230
|
-
* human(id: $id) {
|
|
231
|
-
* name
|
|
232
|
-
* height
|
|
233
|
-
* }
|
|
234
|
-
* }
|
|
235
|
-
* `, { id: "1001" }, 'getHuman')
|
|
236
|
-
* .then(human => {
|
|
237
|
-
* // do something with human
|
|
238
|
-
* console.log(human);
|
|
239
|
-
* });
|
|
240
|
-
*/
|
|
241
|
-
forceFetch(
|
|
242
|
-
query: string,
|
|
243
|
-
variables?: Variables,
|
|
244
|
-
operationName?: string,
|
|
245
|
-
overrides: Options = {},
|
|
246
|
-
): Promise<Result> {
|
|
247
|
-
const request: GraphqlOperation = {
|
|
248
|
-
query,
|
|
249
|
-
}
|
|
250
|
-
const options = Object.assign({}, this._options, overrides, {
|
|
251
|
-
shouldBatch: false,
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
if (variables) {
|
|
255
|
-
request.variables = variables
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (operationName) {
|
|
259
|
-
request.operationName = operationName
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const promise = new Promise<Result>((resolve, reject) => {
|
|
263
|
-
const client = new QueryBatcher(this.fetcher, this._options)
|
|
264
|
-
client._queue = [
|
|
265
|
-
{
|
|
266
|
-
request,
|
|
267
|
-
resolve,
|
|
268
|
-
reject,
|
|
269
|
-
},
|
|
270
|
-
]
|
|
271
|
-
dispatchQueue(client, options)
|
|
272
|
-
})
|
|
273
|
-
return promise
|
|
274
|
-
}
|
|
275
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
|
-
import { type BatchOptions, createFetcher } from './fetcher'
|
|
4
|
-
import type { ExecutionResult, LinkedType } from './types'
|
|
5
|
-
import {
|
|
6
|
-
generateGraphqlOperation,
|
|
7
|
-
type GraphqlOperation,
|
|
8
|
-
} from './generateGraphqlOperation'
|
|
9
|
-
|
|
10
|
-
export type Headers =
|
|
11
|
-
| HeadersInit
|
|
12
|
-
| (() => HeadersInit)
|
|
13
|
-
| (() => Promise<HeadersInit>)
|
|
14
|
-
|
|
15
|
-
export type BaseFetcher = (
|
|
16
|
-
operation: GraphqlOperation | GraphqlOperation[],
|
|
17
|
-
) => Promise<ExecutionResult | ExecutionResult[]>
|
|
18
|
-
|
|
19
|
-
export type ClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
|
|
20
|
-
url?: string
|
|
21
|
-
batch?: BatchOptions | boolean
|
|
22
|
-
fetcher?: BaseFetcher
|
|
23
|
-
fetch?: Function
|
|
24
|
-
headers?: Headers
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const createClient = ({
|
|
28
|
-
queryRoot,
|
|
29
|
-
mutationRoot,
|
|
30
|
-
subscriptionRoot,
|
|
31
|
-
...options
|
|
32
|
-
}: ClientOptions & {
|
|
33
|
-
queryRoot?: LinkedType
|
|
34
|
-
mutationRoot?: LinkedType
|
|
35
|
-
subscriptionRoot?: LinkedType
|
|
36
|
-
}) => {
|
|
37
|
-
const fetcher = createFetcher(options)
|
|
38
|
-
const client: {
|
|
39
|
-
query?: Function
|
|
40
|
-
mutation?: Function
|
|
41
|
-
} = {}
|
|
42
|
-
|
|
43
|
-
if (queryRoot) {
|
|
44
|
-
client.query = (request: any) => {
|
|
45
|
-
if (!queryRoot) throw new Error('queryRoot argument is missing')
|
|
46
|
-
|
|
47
|
-
const resultPromise = fetcher(
|
|
48
|
-
generateGraphqlOperation('query', queryRoot, request),
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
return resultPromise
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (mutationRoot) {
|
|
55
|
-
client.mutation = (request: any) => {
|
|
56
|
-
if (!mutationRoot)
|
|
57
|
-
throw new Error('mutationRoot argument is missing')
|
|
58
|
-
|
|
59
|
-
const resultPromise = fetcher(
|
|
60
|
-
generateGraphqlOperation('mutation', mutationRoot, request),
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
return resultPromise
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return client as any
|
|
68
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
export class GenqlError extends Error {
|
|
3
|
-
errors: Array<GraphqlError> = []
|
|
4
|
-
/**
|
|
5
|
-
* Partial data returned by the server
|
|
6
|
-
*/
|
|
7
|
-
data?: any
|
|
8
|
-
constructor(errors: any[], data: any) {
|
|
9
|
-
let message = Array.isArray(errors)
|
|
10
|
-
? errors.map((x) => x?.message || '').join('\n')
|
|
11
|
-
: ''
|
|
12
|
-
if (!message) {
|
|
13
|
-
message = 'GraphQL error'
|
|
14
|
-
}
|
|
15
|
-
super(message)
|
|
16
|
-
this.errors = errors
|
|
17
|
-
this.data = data
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface GraphqlError {
|
|
22
|
-
message: string
|
|
23
|
-
locations?: Array<{
|
|
24
|
-
line: number
|
|
25
|
-
column: number
|
|
26
|
-
}>
|
|
27
|
-
path?: string[]
|
|
28
|
-
extensions?: Record<string, any>
|
|
29
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { QueryBatcher } from './batcher'
|
|
3
|
-
|
|
4
|
-
import type { ClientOptions } from './createClient'
|
|
5
|
-
import type { GraphqlOperation } from './generateGraphqlOperation'
|
|
6
|
-
import { GenqlError } from './error'
|
|
7
|
-
|
|
8
|
-
export interface Fetcher {
|
|
9
|
-
(gql: GraphqlOperation): Promise<any>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type BatchOptions = {
|
|
13
|
-
batchInterval?: number // ms
|
|
14
|
-
maxBatchSize?: number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const DEFAULT_BATCH_OPTIONS = {
|
|
18
|
-
maxBatchSize: 10,
|
|
19
|
-
batchInterval: 40,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const createFetcher = ({
|
|
23
|
-
url,
|
|
24
|
-
headers = {},
|
|
25
|
-
fetcher,
|
|
26
|
-
fetch: _fetch,
|
|
27
|
-
batch = false,
|
|
28
|
-
...rest
|
|
29
|
-
}: ClientOptions): Fetcher => {
|
|
30
|
-
if (!url && !fetcher) {
|
|
31
|
-
throw new Error('url or fetcher is required')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
fetcher = fetcher || (async (body) => {
|
|
35
|
-
let headersObject =
|
|
36
|
-
typeof headers == 'function' ? await headers() : headers
|
|
37
|
-
headersObject = headersObject || {}
|
|
38
|
-
if (typeof fetch === 'undefined' && !_fetch) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
'Global `fetch` function is not available, pass a fetch polyfill to Genql `createClient`',
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
let fetchImpl = _fetch || fetch
|
|
44
|
-
const res = await fetchImpl(url!, {
|
|
45
|
-
headers: {
|
|
46
|
-
'Content-Type': 'application/json',
|
|
47
|
-
...headersObject,
|
|
48
|
-
},
|
|
49
|
-
method: 'POST',
|
|
50
|
-
body: JSON.stringify(body),
|
|
51
|
-
...rest,
|
|
52
|
-
})
|
|
53
|
-
if (!res.ok) {
|
|
54
|
-
throw new Error(`${res.statusText}: ${await res.text()}`)
|
|
55
|
-
}
|
|
56
|
-
const json = await res.json()
|
|
57
|
-
return json
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
if (!batch) {
|
|
61
|
-
return async (body) => {
|
|
62
|
-
const json = await fetcher!(body)
|
|
63
|
-
if (Array.isArray(json)) {
|
|
64
|
-
return json.map((json) => {
|
|
65
|
-
if (json?.errors?.length) {
|
|
66
|
-
throw new GenqlError(json.errors || [], json.data)
|
|
67
|
-
}
|
|
68
|
-
return json.data
|
|
69
|
-
})
|
|
70
|
-
} else {
|
|
71
|
-
if (json?.errors?.length) {
|
|
72
|
-
throw new GenqlError(json.errors || [], json.data)
|
|
73
|
-
}
|
|
74
|
-
return json.data
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const batcher = new QueryBatcher(
|
|
80
|
-
async (batchedQuery) => {
|
|
81
|
-
// console.log(batchedQuery) // [{ query: 'query{user{age}}', variables: {} }, ...]
|
|
82
|
-
const json = await fetcher!(batchedQuery)
|
|
83
|
-
return json as any
|
|
84
|
-
},
|
|
85
|
-
batch === true ? DEFAULT_BATCH_OPTIONS : batch,
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
return async ({ query, variables }) => {
|
|
89
|
-
const json = await batcher.fetch(query, variables)
|
|
90
|
-
if (json?.data) {
|
|
91
|
-
return json.data
|
|
92
|
-
}
|
|
93
|
-
throw new Error(
|
|
94
|
-
'Genql batch fetcher returned unexpected result ' + JSON.stringify(json),
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
}
|