This repository has been archived by the owner on Mar 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-builder.ts
76 lines (63 loc) · 2.31 KB
/
context-builder.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
import {GraphQLLogger, PolarisGraphQLLogger} from "@enigmatis/polaris-graphql-logger";
import {DeltaMiddlewareContext} from '@enigmatis/polaris-delta-middleware';
import { PolarisConnection } from "@enigmatis/polaris-typeorm/src/connections/connection";
export interface PolarisContext extends DeltaMiddlewareContext {
}
export class ContextBuilder {
private _logger: GraphQLLogger;
private _dataVersion: number;
private _realityId: number;
private _includeLinkedOper: boolean;
graphqlLogger(logger: GraphQLLogger) {
if (!logger) {
throw new Error("No logger supplied");
}
this._logger = logger;
return this;
}
dataVersion(dataVersion: number) {
if (!dataVersion) {
throw new Error("No data version supplied");
}
this._dataVersion = dataVersion;
return this;
}
realityId(realityId: number) {
if (realityId) {
this._realityId = +realityId;
} else {
this._realityId = 0;
}
}
includeLinkedOper(includeLinkedOper){
this._includeLinkedOper = includeLinkedOper == "false"? false: true;
}
build(): PolarisContext {
return {logger: this._logger, dataVersion: this._dataVersion};
}
}
export class ContextInitializer {
private readonly logger: GraphQLLogger;
private readonly polarisConnection: PolarisConnection;
constructor(logger: GraphQLLogger, polarisConnection: PolarisConnection) {
this.logger = logger;
this.polarisConnection = polarisConnection;
}
async initializeContextForRequest({req}) {
const contextBuilder = new ContextBuilder();
let dataVersionHeader = req.headers['data-version'];
let realityIdHeader = req.headers['reality-id'];
let includeLinkedOper = req.headers['include-linked-oper'];
contextBuilder.graphqlLogger(this.logger);
if (dataVersionHeader) {
contextBuilder.dataVersion(dataVersionHeader);
}
if (includeLinkedOper) {
contextBuilder.includeLinkedOper(includeLinkedOper);
}
contextBuilder.realityId(realityIdHeader);
let context: PolarisContext = contextBuilder.build();
this.polarisConnection.manager.queryRunner.data.context = context;
return context;
}
}