-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcounter-dataloader.ts
More file actions
48 lines (41 loc) · 1.49 KB
/
counter-dataloader.ts
File metadata and controls
48 lines (41 loc) · 1.49 KB
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
import { Container } from 'inversify';
import type { KeyValueCache } from '@apollo/utils.keyvaluecache';
import { ApolloError } from 'apollo-server-errors';
import { ICounterService } from '../interfaces';
import { setupCaching } from './cache';
import { Counter } from '../generated-models';
import { TYPES } from '../constants';
export interface CacheOptions {
ttl?: number;
}
type Options = {
cache: KeyValueCache;
context: {
container: Container;
};
};
export class CounterDataSource implements ICounterService {
// eslint-disable-next-line no-useless-constructor
constructor(private readonly options: Options) {
this.initialize();
}
private cacheCounterService: ICounterService;
public counterQuery(): Counter | Promise<Counter> | PromiseLike<Counter> {
return this.cacheCounterService.counterQuery();
}
public addCounter(amount?: number) {
return this.cacheCounterService.addCounter();
}
public initialize() {
const { context, cache } = this.options;
const counterService = context.container.getNamed<ICounterService>(TYPES.CounterMockService, 'proxy');
if (!counterService) {
throw new ApolloError('Missing TextFileService in the context!');
}
try {
this.cacheCounterService = setupCaching({ counterService, cache });
} catch (err) {
throw new ApolloError(`Setting up cache in the FilesDataSource failed due to ${err}`);
}
}
}