This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventCache.js
144 lines (119 loc) · 4.12 KB
/
eventCache.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { getLogger } = require("./utils");
const logger = getLogger("eventCache");
const { cloneDeep } = require("lodash");
const contentful = require("contentful");
const { graphql } = require("graphql");
const cfGraphql = require("cf-graphql");
const graphqlHTTP = require("express-graphql");
const LifetimeCache = require("./lifetimeCache");
const introspectionQuery = `{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}`;
class EventCache {
constructor({ space, accessToken }) {
this.client = contentful.createClient({ space, accessToken });
this.defaultEvent = Symbol("default event");
this.cache = new LifetimeCache(120000);
}
_getEventKey(eventName) {
return eventName ? eventName : this.defaultEvent;
}
_getEventCache(key) {
if (this.cache.has(key)) {
return this.cache.get(key);
}
const cache = new Map();
this.cache.set(key, cache);
return cache;
}
async getEvent(eventName) {
const key = this._getEventKey(eventName);
const options = { content_type: "event", limit: 1 };
if (eventName) {
options["fields.name"] = eventName;
} else {
options["fields.isDefault"] = true;
}
if (this._getEventCache(key).has("data")) {
const data = cloneDeep(this._getEventCache(key).get("data"));
logger.debug(`Found cached event ${data.name}`);
return data;
}
const entries = await this.client.getEntries(options);
const data = entries.items[0].fields;
this._getEventCache(key).set("data", cloneDeep(data));
logger.debug(`Fetched event ${data.name}`);
return data;
}
async getLandingPageEvents() {
const options = { content_type: "event", "fields.showOnLandingPage": true };
const entries = await this.client.getEntries(options);
const events = [];
entries.items.forEach(item => {
const data = item.fields;
this._getEventCache(data.name).set("data", cloneDeep(data));
logger.debug(`Fetched landing page event ${data.name}`);
events.push(data);
});
return events;
}
// TODO: Remove locale specific middleware once https://github.com/contentful-labs/cf-graphql/issues/29 has been resolved.
async getApi(eventName, locale) {
const key = this._getEventKey(eventName);
if (this._getEventCache(key).has(`${locale}_middleware`)) {
logger.debug(`Returning cached middleware for ${key}`);
return this._getEventCache(key).get(`${locale}_middleware`);
}
logger.info(`Creating GraphQL middleware for ${key}`);
const eventData = await this.getEvent(key);
const { spaceId, cdaToken, cmaToken } = eventData.secrets;
logger.debug(
`Fetching content types for space (${spaceId}) to create a space graph`
);
logger.debug(`Initializing contentful client for space ${spaceId}`);
logger.trace(
`Configuration: ${JSON.stringify({ spaceId, cdaToken, cmaToken })}`
);
const client = cfGraphql.createClient({
spaceId,
cdaToken,
cmaToken,
locale
});
logger.debug("Fetching content types");
const contentTypes = await client.getContentTypes();
logger.debug("Creating space graph");
const spaceGraph = await cfGraphql.prepareSpaceGraph(contentTypes);
const names = spaceGraph.map(ct => ct.names.type).join(", ");
logger.debug(`Contentful content types prepared: ${names}`);
logger.debug("Creating GraphQL schema");
const schema = await cfGraphql.createSchema(spaceGraph);
const introspection = (await graphql(schema, introspectionQuery)).data;
this._getEventCache(key).set("schema", introspection);
const middleware = graphqlHTTP(
cfGraphql.helpers.expressGraphqlExtension(client, schema, {
version: true,
timeline: true,
detailedErrors: false
})
);
this._getEventCache(key).set(`${locale}_middleware`, middleware);
return middleware;
}
async getSchema(eventName) {
const key = this._getEventKey(eventName);
if (!this._getEventCache(key).has("schema")) {
await this.getApi(key);
}
return this._getEventCache(key).get("schema");
}
}
module.exports = EventCache;