-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
schema.mjs
67 lines (63 loc) · 2.35 KB
/
schema.mjs
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
import { makeExecutableSchema } from '@graphql-tools/schema';
import { mergeTypeDefs } from '@graphql-tools/merge';
import resolvers from './resolvers/index.mjs';
import schemaStatic from './schema-static.mjs';
import schemaDynamic from './schema-dynamic.mjs';
let schema, loadingSchema;
let lastSchemaRefresh = 0;
const schemaRefreshInterval = 1000 * 60 * 10;
export default async function getSchema(data, context) {
if (schema && new Date() - lastSchemaRefresh < schemaRefreshInterval) {
return schema;
}
if (loadingSchema) {
return new Promise((resolve) => {
let loadingTimedOut = false;
const loadingTimeout = setTimeout(() => {
loadingTimedOut = true;
}, 3100);
const loadingInterval = setInterval(() => {
if (!loadingSchema) {
clearTimeout(loadingTimeout);
clearInterval(loadingInterval);
return resolve(schema);
}
if (loadingTimedOut) {
console.log(`Schema loading timed out; forcing load`);
clearInterval(loadingInterval);
loadingSchema = false;
return resolve(getSchema(data, context));
}
}, 100);
});
}
loadingSchema = true;
return schemaDynamic(data, context).catch(error => {
console.error('Error loading dynamic type definitions', error);
return Promise.reject(error);
}).then(dynamicDefs => {
let mergedDefs;
try {
mergedDefs = mergeTypeDefs([schemaStatic, dynamicDefs]);
} catch (error) {
console.error('Error merging type defs', error);
return Promise.reject(error);
}
try {
schema = makeExecutableSchema({ typeDefs: mergedDefs, resolvers: resolvers });
//console.log('schema loaded');
lastSchemaRefresh = new Date();
return schema;
} catch (error) {
console.error('Error making schema executable');
if (!error.message) {
console.error('Check type names in resolvers');
} else {
console.error(error.message);
}
return Promise.reject(error);
}
}).finally(() => {
loadingSchema = false;
});
}