@nx-ddd/hasura 19.0.0-preview.29 → 19.0.0-preview.5
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/README.md +11 -0
- package/package.json +6 -20
- package/src/index.d.ts +1 -0
- package/src/index.js +5 -0
- package/src/index.js.map +1 -0
- package/{apollo-multi.service.d.ts → src/lib/apollo-multi.service.d.ts} +8 -4
- package/src/lib/apollo-multi.service.js +52 -0
- package/src/lib/apollo-multi.service.js.map +1 -0
- package/src/lib/decorators.js +50 -0
- package/src/lib/decorators.js.map +1 -0
- package/src/lib/hasura.config.js +15 -0
- package/src/lib/hasura.config.js.map +1 -0
- package/src/lib/hasura.converter.d.ts +24 -0
- package/src/lib/hasura.converter.js +82 -0
- package/src/lib/hasura.converter.js.map +1 -0
- package/{hasura.di.d.ts → src/lib/hasura.di.d.ts} +2 -2
- package/src/lib/hasura.di.js +79 -0
- package/src/lib/hasura.di.js.map +1 -0
- package/{hasura.interceptor.d.ts → src/lib/hasura.interceptor.d.ts} +0 -3
- package/src/lib/hasura.interceptor.js +24 -0
- package/src/lib/hasura.interceptor.js.map +1 -0
- package/src/lib/hasura.repository.d.ts +40 -0
- package/src/lib/hasura.repository.js +261 -0
- package/src/lib/hasura.repository.js.map +1 -0
- package/src/lib/hasura.service.d.ts +18 -0
- package/src/lib/hasura.service.js +49 -0
- package/src/lib/hasura.service.js.map +1 -0
- package/{index.d.ts → src/lib/index.d.ts} +0 -1
- package/src/lib/index.js +12 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/links.js +23 -0
- package/src/lib/links.js.map +1 -0
- package/builders/index.d.ts +0 -2
- package/builders/parts/_meta/graphql-query-part.d.ts +0 -12
- package/builders/parts/mutations/create.builder.d.ts +0 -13
- package/builders/parts/mutations/delete.builder.d.ts +0 -12
- package/builders/parts/mutations/index.d.ts +0 -5
- package/builders/parts/mutations/mutations.builder.d.ts +0 -14
- package/builders/parts/mutations/save.builder.d.ts +0 -21
- package/builders/parts/mutations/update.builder.d.ts +0 -13
- package/builders/parts/parts.builder.d.ts +0 -10
- package/builders/parts/queries/get.builder.d.ts +0 -9
- package/builders/parts/queries/index.d.ts +0 -2
- package/builders/parts/queries/queries.builder.d.ts +0 -16
- package/builders/query-builder.d.ts +0 -15
- package/fesm2022/nx-ddd-hasura.mjs +0 -707
- package/fesm2022/nx-ddd-hasura.mjs.map +0 -1
- package/hasura.converter.d.ts +0 -37
- package/hasura.repository.d.ts +0 -40
- package/hasura.service.d.ts +0 -32
- /package/{decorators.d.ts → src/lib/decorators.d.ts} +0 -0
- /package/{hasura.config.d.ts → src/lib/hasura.config.d.ts} +0 -0
- /package/{links.d.ts → src/lib/links.d.ts} +0 -0
|
@@ -1,707 +0,0 @@
|
|
|
1
|
-
import * as i0 from '@angular/core';
|
|
2
|
-
import { inject, Injectable, InjectionToken, importProvidersFrom } from '@angular/core';
|
|
3
|
-
import { Apollo, APOLLO_OPTIONS, ApolloModule, gql } from 'apollo-angular';
|
|
4
|
-
import { HttpLink } from 'apollo-angular/http';
|
|
5
|
-
import { InMemoryCache } from '@apollo/client/cache';
|
|
6
|
-
import { setContext } from '@apollo/client/link/context';
|
|
7
|
-
import { split, ApolloLink, HttpLink as HttpLink$1, InMemoryCache as InMemoryCache$1, gql as gql$1 } from '@apollo/client/core';
|
|
8
|
-
import { getMainDefinition } from '@apollo/client/utilities';
|
|
9
|
-
import { makeDI, makeDecoratorFactories, plainToInstanceWithValid } from '@nx-ddd/core';
|
|
10
|
-
import { WebSocketLink } from '@apollo/client/link/ws';
|
|
11
|
-
import { from, of, isObservable, lastValueFrom, filter, take, distinctUntilChanged, tap, switchMap, map } from 'rxjs';
|
|
12
|
-
import { camelCase as camelCase$1, snakeCase, get, pick, omitBy } from 'lodash-es';
|
|
13
|
-
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
|
|
14
|
-
import dayjs from 'dayjs';
|
|
15
|
-
import { format } from 'date-fns';
|
|
16
|
-
import { Repository } from '@nx-ddd/common/domain';
|
|
17
|
-
export { TransformToDayjs } from '@nx-ddd/common/domain';
|
|
18
|
-
export { IsDayjs } from 'class-validator-extended';
|
|
19
|
-
|
|
20
|
-
function wrap(token) {
|
|
21
|
-
return isObservable(token) ? token : token instanceof Promise ? from(token) : of(token);
|
|
22
|
-
}
|
|
23
|
-
function resolve(getHeaders) {
|
|
24
|
-
const headers$ = wrap(getHeaders());
|
|
25
|
-
return lastValueFrom(headers$.pipe(filter((headers) => !!headers), take(1), distinctUntilChanged(), tap((headers) => console.debug('[resolve] headers:', headers))));
|
|
26
|
-
}
|
|
27
|
-
function buildWebsocketLink(endpoint, getHeaders) {
|
|
28
|
-
return new WebSocketLink({
|
|
29
|
-
uri: endpoint.replace('http', 'ws'),
|
|
30
|
-
options: {
|
|
31
|
-
reconnect: true,
|
|
32
|
-
connectionParams: () => resolve(getHeaders).then((headers) => ({ headers })),
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function isSubscription(query) {
|
|
38
|
-
const definition = getMainDefinition(query);
|
|
39
|
-
return (definition.kind === 'OperationDefinition' &&
|
|
40
|
-
definition.operation === 'subscription');
|
|
41
|
-
}
|
|
42
|
-
class ApolloMultiService {
|
|
43
|
-
apollo = inject(Apollo);
|
|
44
|
-
httpLink = inject(HttpLink);
|
|
45
|
-
createClient(name, baseUrl, getHeaders) {
|
|
46
|
-
const wsLink = buildWebsocketLink(baseUrl, getHeaders);
|
|
47
|
-
const link = split(({ query }) => isSubscription(query), wsLink, ApolloLink.from([
|
|
48
|
-
setContext(async () => {
|
|
49
|
-
const headers = await resolve(getHeaders);
|
|
50
|
-
return { headers };
|
|
51
|
-
}),
|
|
52
|
-
this.httpLink.create({ uri: baseUrl, }),
|
|
53
|
-
]));
|
|
54
|
-
this.apollo.removeClient(name);
|
|
55
|
-
this.apollo.createNamed(name, { link, cache: new InMemoryCache() });
|
|
56
|
-
}
|
|
57
|
-
getClient(endpoint, getHeaders) {
|
|
58
|
-
const name = endpoint;
|
|
59
|
-
let client = this.apollo.use(name);
|
|
60
|
-
console.debug('[ApolloMultiService] getClient:', name);
|
|
61
|
-
if (!client) {
|
|
62
|
-
this.createClient(name, endpoint, getHeaders);
|
|
63
|
-
client = this.apollo.use(name);
|
|
64
|
-
}
|
|
65
|
-
return client;
|
|
66
|
-
}
|
|
67
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: ApolloMultiService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
68
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: ApolloMultiService, providedIn: 'root' });
|
|
69
|
-
}
|
|
70
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: ApolloMultiService, decorators: [{
|
|
71
|
-
type: Injectable,
|
|
72
|
-
args: [{ providedIn: 'root' }]
|
|
73
|
-
}] });
|
|
74
|
-
const GET_APOLLO_CLIENT = makeDI('GET_APOLLO_CLIENT');
|
|
75
|
-
|
|
76
|
-
const { createDecorator, getAnnotations, } = makeDecoratorFactories((type, fieldName, propName, options) => ({ type, fieldName, propName, childType: options.childType }), '[@nx-ddd/hasura] annotations');
|
|
77
|
-
function getFlattenFieldAnnotations(T, prefix = '') {
|
|
78
|
-
const annotations = Hasura.getAnnotations(T);
|
|
79
|
-
return (annotations ?? []).reduce((acc, annotation) => {
|
|
80
|
-
const currentKey = prefix ? camelCase$1(`${prefix}_${annotation.fieldName}`) : annotation.fieldName;
|
|
81
|
-
if (annotation.childType) {
|
|
82
|
-
return {
|
|
83
|
-
...acc,
|
|
84
|
-
...getFlattenFieldAnnotations(annotation.childType(), currentKey)
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
...acc,
|
|
89
|
-
[currentKey]: annotation
|
|
90
|
-
};
|
|
91
|
-
}, {});
|
|
92
|
-
}
|
|
93
|
-
function getFields(T, options = {
|
|
94
|
-
prefix: '',
|
|
95
|
-
case: 'snake'
|
|
96
|
-
}) {
|
|
97
|
-
const annotations = Hasura.getAnnotations(T);
|
|
98
|
-
const changeCase = options?.case === 'snake' ? snakeCase : camelCase$1;
|
|
99
|
-
const fields = (annotations ?? []).flatMap((annotation) => {
|
|
100
|
-
const currentKey = changeCase(options?.prefix ? `${options?.prefix}_${annotation.fieldName}` : annotation.fieldName);
|
|
101
|
-
if (annotation.childType) {
|
|
102
|
-
return [
|
|
103
|
-
...getFields(annotation.childType(), {
|
|
104
|
-
prefix: currentKey,
|
|
105
|
-
case: options?.case,
|
|
106
|
-
})
|
|
107
|
-
];
|
|
108
|
-
}
|
|
109
|
-
return [currentKey];
|
|
110
|
-
});
|
|
111
|
-
return fields;
|
|
112
|
-
}
|
|
113
|
-
const Hasura = {
|
|
114
|
-
Text: createDecorator('text'),
|
|
115
|
-
Timestamp: createDecorator('timestamp'),
|
|
116
|
-
Date: createDecorator('date'),
|
|
117
|
-
Integer: createDecorator('integer'),
|
|
118
|
-
Numeric: createDecorator('numeric'),
|
|
119
|
-
Boolean: createDecorator('boolean'),
|
|
120
|
-
Map: (childType) => createDecorator('map', { childType })(),
|
|
121
|
-
JSON: createDecorator('json'),
|
|
122
|
-
getAnnotations,
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
class HasuraInterceptor {
|
|
126
|
-
config = inject(HASURA_CONFIG);
|
|
127
|
-
intercept(req, next) {
|
|
128
|
-
if (req.url.startsWith(this.config.url)) {
|
|
129
|
-
return next.handle(req.clone({
|
|
130
|
-
headers: req.headers.set('x-hasura-admin-secret', this.config.adminSecret),
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
return next.handle(req);
|
|
134
|
-
}
|
|
135
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
136
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraInterceptor });
|
|
137
|
-
}
|
|
138
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraInterceptor, decorators: [{
|
|
139
|
-
type: Injectable
|
|
140
|
-
}] });
|
|
141
|
-
|
|
142
|
-
const HASURA_CONFIG = new InjectionToken('HASURA_CONFIG');
|
|
143
|
-
function provideHasuraConfig(config) {
|
|
144
|
-
return [
|
|
145
|
-
{ provide: HASURA_CONFIG, useValue: config },
|
|
146
|
-
config.adminSecret ? { provide: HTTP_INTERCEPTORS, useClass: HasuraInterceptor, multi: true } : [],
|
|
147
|
-
];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function deepCamelToSnakeCase(obj) {
|
|
151
|
-
if (Array.isArray(obj))
|
|
152
|
-
return obj.map(deepCamelToSnakeCase);
|
|
153
|
-
if (obj instanceof Date)
|
|
154
|
-
return obj;
|
|
155
|
-
if (obj && typeof obj === "object") {
|
|
156
|
-
const result = {};
|
|
157
|
-
for (const key in obj) {
|
|
158
|
-
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
159
|
-
result[snakeCase(key)] = deepCamelToSnakeCase(obj[key]);
|
|
160
|
-
}
|
|
161
|
-
return result;
|
|
162
|
-
}
|
|
163
|
-
return obj;
|
|
164
|
-
}
|
|
165
|
-
function camelCase(str) {
|
|
166
|
-
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
167
|
-
}
|
|
168
|
-
function deepSnakeToCamelCase(obj) {
|
|
169
|
-
if (Array.isArray(obj))
|
|
170
|
-
return obj.map(deepSnakeToCamelCase);
|
|
171
|
-
if (obj instanceof Date)
|
|
172
|
-
return obj;
|
|
173
|
-
if (obj && typeof obj === "object") {
|
|
174
|
-
const result = {};
|
|
175
|
-
for (const key in obj)
|
|
176
|
-
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
177
|
-
result[camelCase(key)] = deepSnakeToCamelCase(obj[key]);
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
return obj;
|
|
181
|
-
}
|
|
182
|
-
function fromHasura(_object, type) {
|
|
183
|
-
return HasuraUtils.fromHasura(_object, type);
|
|
184
|
-
}
|
|
185
|
-
function toHasura(object, type, { extraFunc = deepCamelToSnakeCase } = {}) {
|
|
186
|
-
return HasuraUtils.toHasura(object, type, { extraFunc });
|
|
187
|
-
}
|
|
188
|
-
function getConverter(type) {
|
|
189
|
-
return {
|
|
190
|
-
toHasura(entity) {
|
|
191
|
-
return toHasura(entity, type);
|
|
192
|
-
},
|
|
193
|
-
toHasuraMany(entities) {
|
|
194
|
-
return entities.map((entity) => this.toHasura(entity));
|
|
195
|
-
},
|
|
196
|
-
fromHasura(entity) {
|
|
197
|
-
return fromHasura(entity, type);
|
|
198
|
-
},
|
|
199
|
-
fromHasuraMany(entities) {
|
|
200
|
-
return entities.map((entity) => this.fromHasura(entity));
|
|
201
|
-
},
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
/** @deprecated use `getConverter` instead */
|
|
205
|
-
const makeConverter = getConverter;
|
|
206
|
-
class HasuraUtils {
|
|
207
|
-
static toTimestamp(date) {
|
|
208
|
-
if (dayjs.isDayjs(date)) {
|
|
209
|
-
return date.toISOString();
|
|
210
|
-
}
|
|
211
|
-
else if (date instanceof Date) {
|
|
212
|
-
return format(date, 'yyyy-MM-dd HH:mm:ss');
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
static toDate(date) {
|
|
219
|
-
if (typeof date === 'undefined') {
|
|
220
|
-
return undefined;
|
|
221
|
-
}
|
|
222
|
-
else if (dayjs.isDayjs(date)) {
|
|
223
|
-
return format(date.toDate(), 'yyyy-MM-dd');
|
|
224
|
-
}
|
|
225
|
-
else if (date instanceof Date) {
|
|
226
|
-
return format(date, 'yyyy-MM-dd');
|
|
227
|
-
}
|
|
228
|
-
else if (typeof date === 'string') {
|
|
229
|
-
return date;
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
return null;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
static fromHasura(_object, type) {
|
|
236
|
-
const object = deepSnakeToCamelCase(_object);
|
|
237
|
-
return plainToInstanceWithValid(type, object);
|
|
238
|
-
}
|
|
239
|
-
static toNumber(value) {
|
|
240
|
-
if (typeof value === 'undefined')
|
|
241
|
-
return undefined;
|
|
242
|
-
return isNaN(Number(value)) ? null : Number(value);
|
|
243
|
-
}
|
|
244
|
-
static toHasura(object, type, { extraFunc = deepCamelToSnakeCase } = {}) {
|
|
245
|
-
const fields = getFlattenFieldAnnotations(type);
|
|
246
|
-
const obj = Object.entries(fields).reduce((acc, [key, annotation]) => {
|
|
247
|
-
const value = get(object, annotation.fieldName);
|
|
248
|
-
if (typeof value === 'undefined')
|
|
249
|
-
return acc;
|
|
250
|
-
switch (annotation.type) {
|
|
251
|
-
case 'text': return { ...acc, [key]: value };
|
|
252
|
-
case 'timestamp': return { ...acc, [key]: this.toTimestamp(value) };
|
|
253
|
-
case 'date': return { ...acc, [key]: this.toDate(value) };
|
|
254
|
-
case 'numeric': return { ...acc, [key]: this.toNumber(value) };
|
|
255
|
-
case 'integer': return { ...acc, [key]: this.toNumber(value) };
|
|
256
|
-
case 'json': return { ...acc, [key]: value };
|
|
257
|
-
case 'map': return { ...acc };
|
|
258
|
-
default: return { ...acc, [key]: value };
|
|
259
|
-
}
|
|
260
|
-
}, {});
|
|
261
|
-
return extraFunc(obj);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function provideHttpLink() {
|
|
266
|
-
return {
|
|
267
|
-
provide: ApolloLink,
|
|
268
|
-
useFactory: (config) => new HttpLink$1({
|
|
269
|
-
uri: config.url,
|
|
270
|
-
headers: config?.adminSecret && { 'x-hasura-admin-secret': config.adminSecret },
|
|
271
|
-
}),
|
|
272
|
-
deps: [HASURA_CONFIG],
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
const HASURA_AUTHORIZATION = new InjectionToken('HASURA_AUTHORIZATION');
|
|
276
|
-
function provideHasuraAuthorization(useFactory) {
|
|
277
|
-
return {
|
|
278
|
-
provide: HASURA_AUTHORIZATION,
|
|
279
|
-
useFactory,
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
const GRAPHQL_HEADERS = new InjectionToken('GRAPHQL_HEADERS', {
|
|
283
|
-
providedIn: 'root',
|
|
284
|
-
factory: () => () => ({}),
|
|
285
|
-
});
|
|
286
|
-
function provideGraphqlHeaders(useFactory) {
|
|
287
|
-
return [
|
|
288
|
-
{ provide: GRAPHQL_HEADERS, useFactory },
|
|
289
|
-
];
|
|
290
|
-
}
|
|
291
|
-
function provideWebsocketLink() {
|
|
292
|
-
return {
|
|
293
|
-
provide: ApolloLink,
|
|
294
|
-
useFactory: () => {
|
|
295
|
-
const config = inject(HASURA_CONFIG);
|
|
296
|
-
const _getHeaders = inject(GRAPHQL_HEADERS);
|
|
297
|
-
return buildWebsocketLink(config.url, _getHeaders);
|
|
298
|
-
},
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
function provideApolloOptions() {
|
|
302
|
-
return {
|
|
303
|
-
provide: APOLLO_OPTIONS,
|
|
304
|
-
useFactory: (link) => ({ cache: new InMemoryCache$1(), link }),
|
|
305
|
-
deps: [ApolloLink],
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
function provideHasura(config) {
|
|
309
|
-
return [
|
|
310
|
-
importProvidersFrom(ApolloModule),
|
|
311
|
-
provideHasuraConfig(config),
|
|
312
|
-
provideApolloOptions(),
|
|
313
|
-
provideHttpLink(),
|
|
314
|
-
];
|
|
315
|
-
}
|
|
316
|
-
function provideHasuraWithWebSocket(config) {
|
|
317
|
-
return [
|
|
318
|
-
importProvidersFrom(ApolloModule),
|
|
319
|
-
provideHasuraConfig(config),
|
|
320
|
-
provideApolloOptions(),
|
|
321
|
-
provideWebsocketLink(),
|
|
322
|
-
];
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
class GetQueryPartBuilder {
|
|
326
|
-
config;
|
|
327
|
-
constructor(config) {
|
|
328
|
-
this.config = config;
|
|
329
|
-
}
|
|
330
|
-
build(params) {
|
|
331
|
-
const queryName = `${this.config.tableName}_by_pk`;
|
|
332
|
-
const variableDefinitions = ['$id: String!'];
|
|
333
|
-
const queryPart = `${queryName}(id: $id) { ${this.config.columns.join(' ')} }`;
|
|
334
|
-
return { queryPart, variableDefinitions, variables: params, operationName: queryName };
|
|
335
|
-
}
|
|
336
|
-
buildMany() {
|
|
337
|
-
const queryName = this.config.tableName;
|
|
338
|
-
const queryPart = `${queryName} { ${this.config.columns.join(' ')} }`;
|
|
339
|
-
return { queryPart, variableDefinitions: [], variables: {}, operationName: queryName };
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function makeSubscription(query) {
|
|
344
|
-
const { kind, definitions } = query;
|
|
345
|
-
const subscriptionDefinitions = definitions.map((definition) => {
|
|
346
|
-
if (definition.kind === 'OperationDefinition' && definition.operation === 'query') {
|
|
347
|
-
return {
|
|
348
|
-
...definition,
|
|
349
|
-
operation: 'subscription',
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
return definition;
|
|
353
|
-
});
|
|
354
|
-
return { kind, definitions: subscriptionDefinitions };
|
|
355
|
-
}
|
|
356
|
-
;
|
|
357
|
-
class QueriesBuilder {
|
|
358
|
-
get;
|
|
359
|
-
constructor(config) {
|
|
360
|
-
this.get = new GetQueryPartBuilder(config);
|
|
361
|
-
}
|
|
362
|
-
makeSubscription(query) {
|
|
363
|
-
return makeSubscription(query);
|
|
364
|
-
}
|
|
365
|
-
;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
class CreateMutationBuilder {
|
|
369
|
-
config;
|
|
370
|
-
constructor(config) {
|
|
371
|
-
this.config = config;
|
|
372
|
-
}
|
|
373
|
-
build(data, alias = 'create') {
|
|
374
|
-
const object = this.convertToObject(data);
|
|
375
|
-
const mutationName = `insert_${this.config.tableName}`;
|
|
376
|
-
const variableDefinitions = [`$${alias}: ${this.config.tableName}_insert_input!`];
|
|
377
|
-
const queryPart = `${alias}: ${mutationName}(objects: [$${alias}]) { affected_rows returning { ${this.config.columns.join(' ')} } }`;
|
|
378
|
-
const variables = { [alias]: object };
|
|
379
|
-
return { queryPart, variableDefinitions, variables, operationName: mutationName, alias };
|
|
380
|
-
}
|
|
381
|
-
buildMany(data, alias = 'createMany') {
|
|
382
|
-
const objects = this.convertToObjects(data);
|
|
383
|
-
const mutationName = `insert_${this.config.tableName}`;
|
|
384
|
-
const variableDefinitions = [`$${alias}: [${this.config.tableName}_insert_input!]!`];
|
|
385
|
-
const queryPart = `${alias}: ${mutationName}(objects: $${alias}) { affected_rows returning { ${this.config.columns.join(' ')} } }`;
|
|
386
|
-
const variables = { [alias]: objects };
|
|
387
|
-
return { queryPart, variableDefinitions, variables, operationName: mutationName, alias };
|
|
388
|
-
}
|
|
389
|
-
convertToObject(item) {
|
|
390
|
-
const fields = Object.keys(item).map(snakeCase).filter((field) => !['created_at', 'updated_at'].includes(field));
|
|
391
|
-
const picked = pick(item, [...fields]);
|
|
392
|
-
return omitBy(picked, (value) => typeof value === 'undefined');
|
|
393
|
-
}
|
|
394
|
-
convertToObjects(items) {
|
|
395
|
-
return items.map((item) => this.convertToObject(item));
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
class DeleteMutationBuilder {
|
|
400
|
-
config;
|
|
401
|
-
constructor(config) {
|
|
402
|
-
this.config = config;
|
|
403
|
-
}
|
|
404
|
-
build({ id }) {
|
|
405
|
-
const operationName = `delete_${this.config.tableName}`;
|
|
406
|
-
const queryPart = `${operationName}(where: {id: {_eq: $id}}) { affected_rows }`;
|
|
407
|
-
const variableDefinitions = ['$id: String!'];
|
|
408
|
-
return { queryPart, variableDefinitions, variables: { id }, operationName };
|
|
409
|
-
}
|
|
410
|
-
buildMany(params, alias = 'deleteMany') {
|
|
411
|
-
const operationName = `delete_${this.config.tableName}`;
|
|
412
|
-
const variableDefinitions = [`$${alias}: [String!]!`];
|
|
413
|
-
const queryPart = `${alias}: ${operationName}(where: {id: {_in: $${alias}}}) { affected_rows }`;
|
|
414
|
-
const variables = { [alias]: params.map((p) => p.id) };
|
|
415
|
-
return { queryPart, variableDefinitions, variables, operationName, alias };
|
|
416
|
-
}
|
|
417
|
-
buildAll(alias = 'deleteAll') {
|
|
418
|
-
const operationName = `delete_${this.config.tableName}`;
|
|
419
|
-
const uniqueAlias = `${alias}_${this.config.tableName}`;
|
|
420
|
-
const variableDefinitions = [];
|
|
421
|
-
const queryPart = `${uniqueAlias}: ${operationName}(where: {}) { affected_rows }`;
|
|
422
|
-
const variables = {};
|
|
423
|
-
return { queryPart, variableDefinitions, variables, operationName, alias };
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
class SaveMutationBuilder {
|
|
428
|
-
config;
|
|
429
|
-
constructor(config) {
|
|
430
|
-
this.config = config;
|
|
431
|
-
}
|
|
432
|
-
build(data, { operationName = `insert_${this.config.tableName}`, constraint = this.config.pk, columns = this.config.columns, returningColumns = this.config.columns, alias = operationName, } = {}) {
|
|
433
|
-
const object = this.buildObject(data);
|
|
434
|
-
const onConflict = this.buildOnConflict(data, constraint, columns);
|
|
435
|
-
const variableDefinitions = [
|
|
436
|
-
`$${alias}Object: ${this.config.tableName}_insert_input!`,
|
|
437
|
-
`$${alias}OnConflict: ${this.config.tableName}_on_conflict!`
|
|
438
|
-
];
|
|
439
|
-
const queryPart = `${alias}: ${operationName}( objects: [$${alias}Object] on_conflict: $${alias}OnConflict) { affected_rows returning { ${returningColumns.join(', ')} } }`;
|
|
440
|
-
const variables = { [`${alias}Object`]: object, [`${alias}OnConflict`]: onConflict };
|
|
441
|
-
return { queryPart, variableDefinitions, variables, operationName, alias };
|
|
442
|
-
}
|
|
443
|
-
buildMany(entities, { operationName = `insert_${this.config.tableName}`, constraint = this.config.pk, columns = this.config.columns, returningColumns = this.config.columns, alias = 'save' } = {}) {
|
|
444
|
-
const objects = this.buildObjects(entities);
|
|
445
|
-
const onConflict = this.buildOnConflict(entities[0], constraint, columns);
|
|
446
|
-
const variableDefinitions = [
|
|
447
|
-
`$${alias}Objects: [${this.config.tableName}_insert_input!]!`,
|
|
448
|
-
`$${alias}OnConflict: ${this.config.tableName}_on_conflict!`
|
|
449
|
-
];
|
|
450
|
-
const queryPart = `${alias}: ${operationName}(objects: $${alias}Objects on_conflict: $${alias}OnConflict) {
|
|
451
|
-
affected_rows returning { ${returningColumns.join(', ')} }
|
|
452
|
-
}`;
|
|
453
|
-
const variables = { [`${alias}Objects`]: objects, [`${alias}OnConflict`]: onConflict };
|
|
454
|
-
return { queryPart, variableDefinitions, variables, operationName: operationName, alias };
|
|
455
|
-
}
|
|
456
|
-
buildObject(item) {
|
|
457
|
-
const fields = Object.keys(item).map(snakeCase).filter((field) => !['created_at', 'updated_at'].includes(field));
|
|
458
|
-
const picked = pick(item, [...fields]);
|
|
459
|
-
return omitBy(picked, (value) => typeof value === 'undefined');
|
|
460
|
-
}
|
|
461
|
-
buildObjects(items) {
|
|
462
|
-
return items.map((item) => this.buildObject(item));
|
|
463
|
-
}
|
|
464
|
-
buildOnConflict(data, constraint, columns) {
|
|
465
|
-
const fields = Object.keys(this.buildObject(data));
|
|
466
|
-
const updateColumns = fields.filter(column => columns.includes(column));
|
|
467
|
-
return { constraint, update_columns: updateColumns };
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
class UpdateMutationBuilder {
|
|
472
|
-
config;
|
|
473
|
-
constructor(config) {
|
|
474
|
-
this.config = config;
|
|
475
|
-
}
|
|
476
|
-
build(data, alias = 'update') {
|
|
477
|
-
const mutationName = `update_${this.config.tableName}`;
|
|
478
|
-
const object = this.buildObject(data);
|
|
479
|
-
const variableDefinitions = [`$${alias}Id: String!`, `$${alias}Object: ${this.config.tableName}_set_input!`];
|
|
480
|
-
const queryPart = `${alias}: ${mutationName}(where: {id: {_eq: $${alias}Id}} _set: $${alias}Object) { affected_rows returning { id } }`;
|
|
481
|
-
const variables = { [`${alias}Id`]: data.id, [`${alias}Object`]: object };
|
|
482
|
-
return { queryPart, variableDefinitions, variables, operationName: mutationName, alias };
|
|
483
|
-
}
|
|
484
|
-
buildMany(data, alias = 'updateMany') {
|
|
485
|
-
const mutationName = `update_${this.config.tableName}_many`;
|
|
486
|
-
const objects = this.buildObjects(data);
|
|
487
|
-
const variableDefinitions = [`$${alias}Object: [${this.config.tableName}_updates!]!`];
|
|
488
|
-
const queryPart = `${alias}: ${mutationName}(updates: $${alias}Objects) { affected_rows returning { id } }`;
|
|
489
|
-
const variables = { [`${alias}Objects`]: objects };
|
|
490
|
-
return { queryPart, variableDefinitions, variables, operationName: mutationName, alias };
|
|
491
|
-
}
|
|
492
|
-
buildObject(item) {
|
|
493
|
-
const fields = Object.keys(item).map(snakeCase).filter((field) => !['created_at', 'updated_at'].includes(field));
|
|
494
|
-
const picked = pick(item, fields);
|
|
495
|
-
return omitBy(picked, (value) => typeof value === 'undefined');
|
|
496
|
-
}
|
|
497
|
-
buildObjects(items) {
|
|
498
|
-
return items.map((item) => ({ where: { id: { _eq: item.id } }, _set: this.buildObject(item) }));
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
class HasuraMutationBuilder {
|
|
503
|
-
create;
|
|
504
|
-
update;
|
|
505
|
-
save;
|
|
506
|
-
delete_;
|
|
507
|
-
constructor(config) {
|
|
508
|
-
this.create = new CreateMutationBuilder(config);
|
|
509
|
-
this.update = new UpdateMutationBuilder(config);
|
|
510
|
-
this.save = new SaveMutationBuilder(config);
|
|
511
|
-
this.delete_ = new DeleteMutationBuilder(config);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
class HasuraQueryPartsBuilder {
|
|
516
|
-
mutations;
|
|
517
|
-
queries;
|
|
518
|
-
constructor(config) {
|
|
519
|
-
this.mutations = new HasuraMutationBuilder(config);
|
|
520
|
-
this.queries = new QueriesBuilder(config);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
class HasuraQueryBuilder {
|
|
525
|
-
parts;
|
|
526
|
-
constructor(config) {
|
|
527
|
-
this.parts = new HasuraQueryPartsBuilder(config);
|
|
528
|
-
}
|
|
529
|
-
buildQuery(part) {
|
|
530
|
-
const operationNameStr = part.operationName ? `${part.operationName} ` : '';
|
|
531
|
-
const variablesStr = part.variableDefinitions?.length ? `(${part.variableDefinitions.join(', ')}) ` : '';
|
|
532
|
-
return `query ${operationNameStr}${variablesStr}{ ${part.queryPart} }`;
|
|
533
|
-
}
|
|
534
|
-
static buildMutation(part) {
|
|
535
|
-
return `mutation ${part.operationName}(${part.variableDefinitions.join(', ')}) { ${part.queryPart} }`;
|
|
536
|
-
}
|
|
537
|
-
buildMutation(part) {
|
|
538
|
-
return HasuraQueryBuilder.buildMutation(part);
|
|
539
|
-
}
|
|
540
|
-
buildTransactionQuery(parts) {
|
|
541
|
-
const operationName = `Transaction`;
|
|
542
|
-
const variableDefinitions = parts.reduce((acc, m) => [...acc, ...m.variableDefinitions], []);
|
|
543
|
-
const mutationParts = parts.map(m => m.queryPart);
|
|
544
|
-
const query = `mutation ${operationName} (${variableDefinitions.join(', ')}) { ${mutationParts.join('\n')} }`;
|
|
545
|
-
const variables = parts.reduce((acc, m) => ({ ...acc, ...m.variables }), {});
|
|
546
|
-
return { query, variables };
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
class HasuraService {
|
|
551
|
-
http = inject(HttpClient);
|
|
552
|
-
apollo = inject(Apollo);
|
|
553
|
-
config = inject(HASURA_CONFIG);
|
|
554
|
-
apolloMulti = inject(ApolloMultiService);
|
|
555
|
-
_getApolloClient = GET_APOLLO_CLIENT.inject() ?? (() => this.apollo.client);
|
|
556
|
-
getApolloClient() {
|
|
557
|
-
const clientOrObsOrPromise = this._getApolloClient(this.config);
|
|
558
|
-
if (isObservable(clientOrObsOrPromise)) {
|
|
559
|
-
return clientOrObsOrPromise;
|
|
560
|
-
}
|
|
561
|
-
else if (clientOrObsOrPromise instanceof Promise) {
|
|
562
|
-
return from(clientOrObsOrPromise);
|
|
563
|
-
}
|
|
564
|
-
else {
|
|
565
|
-
return of(clientOrObsOrPromise);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
/** @deprecated */
|
|
569
|
-
graphql(args) {
|
|
570
|
-
return this.post('/v1/graphql', {
|
|
571
|
-
query: args.query,
|
|
572
|
-
variables: args.variables,
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
/** @deprecated */
|
|
576
|
-
post(endpoint, body) {
|
|
577
|
-
return this.http.post(`${this.config.url}${endpoint}`, body, {});
|
|
578
|
-
}
|
|
579
|
-
/** @deprecated */
|
|
580
|
-
delete(endpoint) {
|
|
581
|
-
return this.http.delete(`${this.config.url}${endpoint}`);
|
|
582
|
-
}
|
|
583
|
-
subscribe({ query, variables }) {
|
|
584
|
-
return this.getApolloClient().pipe(switchMap((client) => client.subscribe({
|
|
585
|
-
query: makeSubscription(query),
|
|
586
|
-
variables: variables,
|
|
587
|
-
fetchPolicy: 'network-only'
|
|
588
|
-
})));
|
|
589
|
-
}
|
|
590
|
-
async mutate(...args) {
|
|
591
|
-
const client = await lastValueFrom(this.getApolloClient());
|
|
592
|
-
return client.mutate(...args).catch((error) => {
|
|
593
|
-
console.error(error.graphQLErrors);
|
|
594
|
-
throw error;
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
async mutateByQueryPart(part) {
|
|
598
|
-
return this.mutate({
|
|
599
|
-
mutation: gql `${HasuraQueryBuilder.buildMutation(part)}`,
|
|
600
|
-
variables: part.variables,
|
|
601
|
-
}).then((res) => {
|
|
602
|
-
console.debug('[HasuraService] mutateByQueryPart:', res);
|
|
603
|
-
return res.data[part?.alias ?? part.operationName];
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
async query(...args) {
|
|
607
|
-
const client = await lastValueFrom(this.getApolloClient());
|
|
608
|
-
return client.query(...args).catch((error) => {
|
|
609
|
-
console.error(error.graphQLErrors);
|
|
610
|
-
throw error;
|
|
611
|
-
});
|
|
612
|
-
}
|
|
613
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
614
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraService, providedIn: 'root' });
|
|
615
|
-
}
|
|
616
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraService, decorators: [{
|
|
617
|
-
type: Injectable,
|
|
618
|
-
args: [{ providedIn: 'root' }]
|
|
619
|
-
}] });
|
|
620
|
-
|
|
621
|
-
class HasuraRepository extends Repository {
|
|
622
|
-
hasura = inject(HasuraService);
|
|
623
|
-
get pkey() {
|
|
624
|
-
return `${this.tableName}_pkey`;
|
|
625
|
-
}
|
|
626
|
-
getQueryBuilder() {
|
|
627
|
-
return new HasuraQueryBuilder({ tableName: this.tableName, pk: this.pkey, columns: this.updateColumns });
|
|
628
|
-
}
|
|
629
|
-
async get(params) {
|
|
630
|
-
const part = this.getQueryBuilder().parts.queries.get.build({ id: params.id });
|
|
631
|
-
return this.hasura.query({
|
|
632
|
-
query: gql$1 `${this.getQueryBuilder().buildQuery(part)}`,
|
|
633
|
-
variables: part.variables,
|
|
634
|
-
fetchPolicy: 'network-only',
|
|
635
|
-
})
|
|
636
|
-
.then((res) => this.converter.fromHasura(res.data[part.operationName]))
|
|
637
|
-
.then(data => data ? this.converter.fromHasura(data) : data);
|
|
638
|
-
}
|
|
639
|
-
async list() {
|
|
640
|
-
const part = this.getQueryBuilder().parts.queries.get.buildMany();
|
|
641
|
-
return this.hasura.query({
|
|
642
|
-
query: gql$1 `${this.getQueryBuilder().buildQuery(part)}`,
|
|
643
|
-
fetchPolicy: 'network-only'
|
|
644
|
-
}).then(res => this.converter.fromHasuraMany(res.data[part.operationName]));
|
|
645
|
-
}
|
|
646
|
-
listChanges() {
|
|
647
|
-
const part = this.getQueryBuilder().parts.queries.get.buildMany();
|
|
648
|
-
const query = this.getQueryBuilder().buildQuery(part);
|
|
649
|
-
return this.hasura.subscribe({ query: gql$1 `${query}`, variables: {} }).pipe(map(res => this.converter.fromHasuraMany(res.data[part.operationName])));
|
|
650
|
-
}
|
|
651
|
-
async create(data) {
|
|
652
|
-
const converted = this.converter.toHasura(data);
|
|
653
|
-
const part = this.getQueryBuilder().parts.mutations.create.build(converted);
|
|
654
|
-
return this.hasura.mutateByQueryPart(part).then(result => this.converter.fromHasura(result.returning[0]));
|
|
655
|
-
}
|
|
656
|
-
async createMany(data) {
|
|
657
|
-
const items = this.converter.toHasuraMany(data);
|
|
658
|
-
const part = this.getQueryBuilder().parts.mutations.create.buildMany(items);
|
|
659
|
-
return this.hasura.mutateByQueryPart(part).then((result) => this.converter.fromHasuraMany(result.returning));
|
|
660
|
-
}
|
|
661
|
-
async update(data) {
|
|
662
|
-
console.debug('data:', data);
|
|
663
|
-
const item = this.converter.toHasura(data);
|
|
664
|
-
console.debug('item:', item);
|
|
665
|
-
const part = this.getQueryBuilder().parts.mutations.update.build(item);
|
|
666
|
-
return this.hasura.mutateByQueryPart(part).then(() => { });
|
|
667
|
-
}
|
|
668
|
-
async updateMany(data) {
|
|
669
|
-
const items = this.converter.toHasuraMany(data);
|
|
670
|
-
const part = this.getQueryBuilder().parts.mutations.update.buildMany(items);
|
|
671
|
-
return this.hasura.mutateByQueryPart(part).then((result) => result.affected_rows).then(() => { });
|
|
672
|
-
}
|
|
673
|
-
async delete(params) {
|
|
674
|
-
const part = this.getQueryBuilder().parts.mutations.delete_.build(params);
|
|
675
|
-
return this.hasura.mutateByQueryPart(part).then(() => { });
|
|
676
|
-
}
|
|
677
|
-
async deleteMany(params) {
|
|
678
|
-
const part = this.getQueryBuilder().parts.mutations.delete_.buildMany(params);
|
|
679
|
-
return this.hasura.mutateByQueryPart(part).then((result) => result.affected_rows);
|
|
680
|
-
}
|
|
681
|
-
async deleteAll() {
|
|
682
|
-
const part = this.getQueryBuilder().parts.mutations.delete_.buildAll();
|
|
683
|
-
return this.hasura.mutateByQueryPart(part).then(() => { });
|
|
684
|
-
}
|
|
685
|
-
async save(data) {
|
|
686
|
-
const item = this.converter.toHasura(data);
|
|
687
|
-
const part = this.getQueryBuilder().parts.mutations.save.build(item);
|
|
688
|
-
return this.hasura.mutateByQueryPart(part).then((res) => res.returning[0]);
|
|
689
|
-
}
|
|
690
|
-
async saveMany(entities) {
|
|
691
|
-
const items = this.converter.toHasuraMany(entities);
|
|
692
|
-
const part = this.getQueryBuilder().parts.mutations.save.buildMany(items);
|
|
693
|
-
return this.hasura.mutateByQueryPart(part).then(() => { });
|
|
694
|
-
}
|
|
695
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraRepository, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
|
|
696
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraRepository });
|
|
697
|
-
}
|
|
698
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: HasuraRepository, decorators: [{
|
|
699
|
-
type: Injectable
|
|
700
|
-
}] });
|
|
701
|
-
|
|
702
|
-
/**
|
|
703
|
-
* Generated bundle index. Do not edit.
|
|
704
|
-
*/
|
|
705
|
-
|
|
706
|
-
export { ApolloMultiService, GET_APOLLO_CLIENT, HASURA_CONFIG, Hasura, HasuraInterceptor, HasuraQueryBuilder, HasuraRepository, HasuraService, HasuraUtils, deepCamelToSnakeCase, deepSnakeToCamelCase, fromHasura, getConverter, getFields, getFlattenFieldAnnotations, makeConverter, provideApolloOptions, provideGraphqlHeaders, provideHasura, provideHasuraAuthorization, provideHasuraConfig, provideHasuraWithWebSocket, provideHttpLink, provideWebsocketLink, toHasura };
|
|
707
|
-
//# sourceMappingURL=nx-ddd-hasura.mjs.map
|