-
Notifications
You must be signed in to change notification settings - Fork 44
/
cache.ts
46 lines (37 loc) · 1.03 KB
/
cache.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
import { GraphQLSchema } from 'graphql'
import { importSchema } from 'graphql-import'
import { SharedLink } from './SharedLink'
import { makeRemoteExecutableSchema } from 'graphql-tools'
const typeDefsCache: { [schemaPath: string]: string } = {}
const remoteSchemaCache: { [endpoint: string]: GraphQLSchema } = {}
export function getCachedTypeDefs(
schemaPath: string,
disableCache: boolean = false,
): string {
if (typeDefsCache[schemaPath]) {
return typeDefsCache[schemaPath]
}
const schema = importSchema(schemaPath)
if (!disableCache) {
typeDefsCache[schemaPath] = schema
}
return schema
}
export function getCachedRemoteSchema(
endpoint: string,
typeDefs: string,
link: SharedLink,
disableCache: boolean = false,
): GraphQLSchema {
if (remoteSchemaCache[endpoint]) {
return remoteSchemaCache[endpoint]
}
const remoteSchema = makeRemoteExecutableSchema({
link: link,
schema: typeDefs,
})
if (!disableCache) {
remoteSchemaCache[endpoint] = remoteSchema
}
return remoteSchema
}