-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathloadServicesFromStorage.ts
More file actions
138 lines (111 loc) · 3.98 KB
/
Copy pathloadServicesFromStorage.ts
File metadata and controls
138 lines (111 loc) · 3.98 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
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
import { CachedFetcher } from './cachedFetcher';
import { ServiceDefinition } from '@apollo/federation';
import { parse } from 'graphql';
interface LinkFileResult {
configPath: string;
formatVersion: number;
}
interface ImplementingService {
formatVersion: number;
graphID: string;
graphVariant: string;
name: string;
revision: string;
url: string;
partialSchemaPath: string;
}
interface ImplementingServiceLocation {
name: string;
path: string;
}
interface ConfigFileResult {
formatVersion: number;
id: string;
implementingServiceLocations: ImplementingServiceLocation[];
schemaHash: string;
}
const envOverrideOperationManifest = 'APOLLO_PARTIAL_SCHEMA_BASE_URL';
const envOverrideStorageSecretBaseUrl = 'APOLLO_STORAGE_SECRET_BASE_URL';
const urlFromEnvOrDefault = (envKey: string, fallback: string) =>
(process.env[envKey] || fallback).replace(/\/$/, '');
// Generate and cache our desired operation manifest URL.
const urlPartialSchemaBase = urlFromEnvOrDefault(
envOverrideOperationManifest,
'https://storage.googleapis.com/engine-partial-schema-prod/',
);
const urlStorageSecretBase: string = urlFromEnvOrDefault(
envOverrideStorageSecretBaseUrl,
'https://storage.googleapis.com/engine-partial-schema-prod/',
);
const fetcher = new CachedFetcher();
function getStorageSecretUrl(graphId: string, apiKeyHash: string): string {
return `${urlStorageSecretBase}/${graphId}/storage-secret/${apiKeyHash}.json`;
}
async function fetchStorageSecret(
graphId: string,
apiKeyHash: string,
): Promise<string> {
const storageSecretUrl = getStorageSecretUrl(graphId, apiKeyHash);
const response = await fetcher.fetch(storageSecretUrl);
return JSON.parse(response.result);
}
export async function getServiceDefinitionsFromStorage({
graphId,
apiKeyHash,
graphVariant,
federationVersion,
}: {
graphId: string;
apiKeyHash: string;
graphVariant?: string;
federationVersion: number;
}): Promise<[ServiceDefinition[], boolean]> {
const secret = await fetchStorageSecret(graphId, apiKeyHash);
if (!graphVariant) {
graphVariant = 'current';
}
const baseUrl = `${urlPartialSchemaBase}/${secret}/${graphVariant}/v${federationVersion}`;
const {
isCacheHit: linkFileCacheHit,
result: linkFileResult,
} = await fetchLinkFile(baseUrl);
// If the link file is a cache hit, no further work is needed
if (linkFileCacheHit) return [[], false];
const parsedLink = JSON.parse(linkFileResult) as LinkFileResult;
const { result: configFileResult } = await fetcher.fetch(
`${urlPartialSchemaBase}/${parsedLink.configPath}`,
);
const parsedConfig = JSON.parse(configFileResult) as ConfigFileResult;
const partialSchemaFiles = await fetchPartialSchemaFiles(
parsedConfig.implementingServiceLocations,
);
// explicity return that this is a new schema, as the link file has changed.
// we can't use the hit property of the fetchPartialSchemaFiles, as the partial
// schema may all be cache hits with the final schema still being new
// (for instance if a partial schema is removed or a partial schema is rolled back to a prior version, which is still in cache)
return [partialSchemaFiles, true];
}
async function fetchLinkFile(baseUrl: string) {
return fetcher.fetch(`${baseUrl}/composition-config-link`);
}
// The order of implementingServices is IMPORTANT
async function fetchPartialSchemaFiles(
implementingServices: ImplementingServiceLocation[],
): Promise<ServiceDefinition[]> {
const fetchPartialSchemasPromises = implementingServices.map(
async ({ name, path }) => {
const serviceLocation = await fetcher.fetch(
`${urlPartialSchemaBase}/${path}`,
);
const { url, partialSchemaPath } = JSON.parse(
serviceLocation.result,
) as ImplementingService;
const { result } = await fetcher.fetch(
`${urlPartialSchemaBase}/${partialSchemaPath}`,
);
return { name, url, typeDefs: parse(result) };
},
);
// Respect the order here
return Promise.all(fetchPartialSchemasPromises);
}