@apollo/gateway 0.50.2 → 0.52.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +4 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/datasources/LocalGraphQLDataSource.d.ts +2 -2
- package/dist/datasources/LocalGraphQLDataSource.d.ts.map +1 -1
- package/dist/datasources/LocalGraphQLDataSource.js +0 -2
- package/dist/datasources/LocalGraphQLDataSource.js.map +1 -1
- package/dist/datasources/RemoteGraphQLDataSource.d.ts +11 -10
- package/dist/datasources/RemoteGraphQLDataSource.d.ts.map +1 -1
- package/dist/datasources/RemoteGraphQLDataSource.js +28 -28
- package/dist/datasources/RemoteGraphQLDataSource.js.map +1 -1
- package/dist/datasources/types.d.ts +5 -5
- package/dist/datasources/types.d.ts.map +1 -1
- package/dist/executeQueryPlan.d.ts +3 -3
- package/dist/executeQueryPlan.d.ts.map +1 -1
- package/dist/executeQueryPlan.js +2 -2
- package/dist/executeQueryPlan.js.map +1 -1
- package/dist/index.d.ts +7 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -33
- package/dist/index.js.map +1 -1
- package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts.map +1 -1
- package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/index.d.ts +2 -2
- package/dist/supergraphManagers/UplinkFetcher/index.d.ts.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/index.js.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts +3 -3
- package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js +14 -13
- package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts +6 -5
- package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts.map +1 -1
- package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js +3 -3
- package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js.map +1 -1
- package/package.json +10 -12
- package/src/__generated__/graphqlTypes.ts +1 -1
- package/src/__tests__/executeQueryPlan.test.ts +14 -5
- package/src/__tests__/execution-utils.ts +3 -3
- package/src/__tests__/gateway/buildService.test.ts +81 -83
- package/src/__tests__/gateway/executor.test.ts +20 -17
- package/src/__tests__/gateway/opentelemetry.test.ts +3 -7
- package/src/__tests__/gateway/supergraphSdl.test.ts +10 -12
- package/src/config.ts +4 -6
- package/src/datasources/LocalGraphQLDataSource.ts +2 -4
- package/src/datasources/RemoteGraphQLDataSource.ts +72 -59
- package/src/datasources/__tests__/LocalGraphQLDataSource.test.ts +2 -2
- package/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts +120 -159
- package/src/datasources/types.ts +5 -5
- package/src/executeQueryPlan.ts +18 -18
- package/src/index.ts +24 -70
- package/src/supergraphManagers/IntrospectAndCompose/__tests__/IntrospectAndCompose.test.ts +0 -6
- package/src/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.ts +2 -2
- package/src/supergraphManagers/UplinkFetcher/__tests__/loadSupergraphSdlFromStorage.test.ts +70 -74
- package/src/supergraphManagers/UplinkFetcher/index.ts +2 -2
- package/src/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.ts +23 -17
- package/src/supergraphManagers/UplinkFetcher/outOfBandReporter.ts +9 -7
- package/dist/cache.d.ts +0 -18
- package/dist/cache.d.ts.map +0 -1
- package/dist/cache.js +0 -46
- package/dist/cache.js.map +0 -1
- package/src/__mocks__/apollo-server-env.ts +0 -56
- package/src/__mocks__/make-fetch-happen-fetcher.ts +0 -57
- package/src/cache.ts +0 -66
- package/src/make-fetch-happen.d.ts +0 -59
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { fetch } from 'apollo-server-env';
|
|
2
1
|
import type { Logger } from '@apollo/utils.logger';
|
|
3
2
|
import resolvable from '@josephg/resolvable';
|
|
4
3
|
import { SupergraphManager, SupergraphSdlHookOptions } from '../../config';
|
|
5
4
|
import { SubgraphHealthCheckFunction, SupergraphSdlUpdateFunction } from '../..';
|
|
6
5
|
import { loadSupergraphSdlFromUplinks } from './loadSupergraphSdlFromStorage';
|
|
6
|
+
import { Fetcher } from '@apollo/utils.fetcher';
|
|
7
7
|
|
|
8
8
|
export interface UplinkFetcherOptions {
|
|
9
9
|
fallbackPollIntervalInMs: number;
|
|
10
10
|
subgraphHealthCheck?: boolean;
|
|
11
11
|
graphRef: string;
|
|
12
12
|
apiKey: string;
|
|
13
|
-
fetcher:
|
|
13
|
+
fetcher: Fetcher;
|
|
14
14
|
maxRetries: number;
|
|
15
15
|
uplinkEndpoints: string[];
|
|
16
16
|
logger?: Logger;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { fetch, Response, Request } from 'apollo-server-env';
|
|
2
1
|
import { GraphQLError } from 'graphql';
|
|
3
2
|
import retry from 'async-retry';
|
|
4
3
|
import { SupergraphSdlUpdate } from '../../config';
|
|
5
4
|
import { submitOutOfBandReportIfConfigured } from './outOfBandReporter';
|
|
6
5
|
import { SupergraphSdlQuery } from '../../__generated__/graphqlTypes';
|
|
6
|
+
import type {
|
|
7
|
+
Fetcher,
|
|
8
|
+
FetcherResponse,
|
|
9
|
+
FetcherRequestInit,
|
|
10
|
+
} from '@apollo/utils.fetcher';
|
|
7
11
|
|
|
8
12
|
// Magic /* GraphQL */ comment below is for codegen, do not remove
|
|
9
13
|
export const SUPERGRAPH_SDL_QUERY = /* GraphQL */`#graphql
|
|
@@ -63,7 +67,7 @@ export async function loadSupergraphSdlFromUplinks({
|
|
|
63
67
|
apiKey: string;
|
|
64
68
|
endpoints: string[];
|
|
65
69
|
errorReportingEndpoint: string | undefined,
|
|
66
|
-
fetcher:
|
|
70
|
+
fetcher: Fetcher;
|
|
67
71
|
compositionId: string | null;
|
|
68
72
|
maxRetries: number,
|
|
69
73
|
roundRobinSeed: number,
|
|
@@ -105,20 +109,21 @@ export async function loadSupergraphSdlFromStorage({
|
|
|
105
109
|
apiKey: string;
|
|
106
110
|
endpoint: string;
|
|
107
111
|
errorReportingEndpoint?: string;
|
|
108
|
-
fetcher:
|
|
112
|
+
fetcher: Fetcher;
|
|
109
113
|
compositionId: string | null;
|
|
110
114
|
}) : Promise<SupergraphSdlUpdate | null> {
|
|
111
|
-
|
|
112
|
-
|
|
115
|
+
const requestBody = JSON.stringify({
|
|
116
|
+
query: SUPERGRAPH_SDL_QUERY,
|
|
117
|
+
variables: {
|
|
118
|
+
ref: graphRef,
|
|
119
|
+
apiKey,
|
|
120
|
+
ifAfterId: compositionId,
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const requestDetails: FetcherRequestInit = {
|
|
113
125
|
method: 'POST',
|
|
114
|
-
body:
|
|
115
|
-
query: SUPERGRAPH_SDL_QUERY,
|
|
116
|
-
variables: {
|
|
117
|
-
ref: graphRef,
|
|
118
|
-
apiKey,
|
|
119
|
-
ifAfterId: compositionId,
|
|
120
|
-
},
|
|
121
|
-
}),
|
|
126
|
+
body: requestBody,
|
|
122
127
|
headers: {
|
|
123
128
|
'apollographql-client-name': name,
|
|
124
129
|
'apollographql-client-version': version,
|
|
@@ -127,9 +132,8 @@ export async function loadSupergraphSdlFromStorage({
|
|
|
127
132
|
},
|
|
128
133
|
};
|
|
129
134
|
|
|
130
|
-
const request: Request = new Request(endpoint, requestDetails);
|
|
131
|
-
|
|
132
135
|
const startTime = new Date();
|
|
136
|
+
let result: FetcherResponse;
|
|
133
137
|
try {
|
|
134
138
|
result = await fetcher(endpoint, requestDetails);
|
|
135
139
|
} catch (e) {
|
|
@@ -137,7 +141,8 @@ export async function loadSupergraphSdlFromStorage({
|
|
|
137
141
|
|
|
138
142
|
await submitOutOfBandReportIfConfigured({
|
|
139
143
|
error: e,
|
|
140
|
-
|
|
144
|
+
requestEndpoint: endpoint,
|
|
145
|
+
requestBody,
|
|
141
146
|
endpoint: errorReportingEndpoint,
|
|
142
147
|
startedAt: startTime,
|
|
143
148
|
endedAt: endTime,
|
|
@@ -168,7 +173,8 @@ export async function loadSupergraphSdlFromStorage({
|
|
|
168
173
|
} else {
|
|
169
174
|
await submitOutOfBandReportIfConfigured({
|
|
170
175
|
error: new UplinkFetcherError(fetchErrorMsg + result.status + ' ' + result.statusText),
|
|
171
|
-
|
|
176
|
+
requestEndpoint: endpoint,
|
|
177
|
+
requestBody,
|
|
172
178
|
endpoint: errorReportingEndpoint,
|
|
173
179
|
response: result,
|
|
174
180
|
startedAt: startTime,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Fetcher, FetcherResponse } from '@apollo/utils.fetcher';
|
|
2
2
|
import { GraphQLError } from 'graphql';
|
|
3
3
|
import {
|
|
4
4
|
ErrorCode,
|
|
@@ -30,7 +30,8 @@ interface OobReportMutationFailure {
|
|
|
30
30
|
|
|
31
31
|
export async function submitOutOfBandReportIfConfigured({
|
|
32
32
|
error,
|
|
33
|
-
|
|
33
|
+
requestEndpoint,
|
|
34
|
+
requestBody,
|
|
34
35
|
endpoint,
|
|
35
36
|
response,
|
|
36
37
|
startedAt,
|
|
@@ -39,13 +40,14 @@ export async function submitOutOfBandReportIfConfigured({
|
|
|
39
40
|
fetcher,
|
|
40
41
|
}: {
|
|
41
42
|
error: Error;
|
|
42
|
-
|
|
43
|
+
requestEndpoint: string;
|
|
44
|
+
requestBody: string;
|
|
43
45
|
endpoint: string | undefined;
|
|
44
|
-
response?:
|
|
46
|
+
response?: FetcherResponse;
|
|
45
47
|
startedAt: Date;
|
|
46
48
|
endedAt: Date;
|
|
47
49
|
tags?: string[];
|
|
48
|
-
fetcher:
|
|
50
|
+
fetcher: Fetcher;
|
|
49
51
|
}) {
|
|
50
52
|
// don't send report if the endpoint url is not configured
|
|
51
53
|
if (!endpoint) {
|
|
@@ -85,8 +87,8 @@ export async function submitOutOfBandReportIfConfigured({
|
|
|
85
87
|
message: error.message,
|
|
86
88
|
},
|
|
87
89
|
request: {
|
|
88
|
-
url:
|
|
89
|
-
body:
|
|
90
|
+
url: requestEndpoint,
|
|
91
|
+
body: requestBody,
|
|
90
92
|
},
|
|
91
93
|
response: response
|
|
92
94
|
? {
|
package/dist/cache.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { CacheManager } from 'make-fetch-happen';
|
|
2
|
-
import { Request, Response, Headers } from 'apollo-server-env';
|
|
3
|
-
import { InMemoryLRUCache } from 'apollo-server-caching';
|
|
4
|
-
interface CachedRequest {
|
|
5
|
-
body: string;
|
|
6
|
-
status: number;
|
|
7
|
-
statusText: string;
|
|
8
|
-
headers: Headers;
|
|
9
|
-
}
|
|
10
|
-
export declare class HttpRequestCache implements CacheManager {
|
|
11
|
-
cache: InMemoryLRUCache<CachedRequest>;
|
|
12
|
-
constructor(cache?: InMemoryLRUCache<CachedRequest>);
|
|
13
|
-
delete(request: Request): Promise<boolean>;
|
|
14
|
-
put(request: Request, response: Response): Promise<Response>;
|
|
15
|
-
match(request: Request): Promise<Response | undefined>;
|
|
16
|
-
}
|
|
17
|
-
export {};
|
|
18
|
-
//# sourceMappingURL=cache.d.ts.map
|
package/dist/cache.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAQzD,UAAU,aAAa;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,qBAAa,gBAAiB,YAAW,YAAY;IAE1C,KAAK,EAAE,gBAAgB,CAAC,aAAa,CAAC;gBAAtC,KAAK,GAAE,gBAAgB,CAAC,aAAa,CAE1C;IAIE,MAAM,CAAC,OAAO,EAAE,OAAO;IAOvB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ;IAwBxC,KAAK,CAAC,OAAO,EAAE,OAAO;CAS7B"}
|
package/dist/cache.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpRequestCache = void 0;
|
|
4
|
-
const apollo_server_env_1 = require("apollo-server-env");
|
|
5
|
-
const apollo_server_caching_1 = require("apollo-server-caching");
|
|
6
|
-
const MAX_SIZE = 5 * 1024 * 1024;
|
|
7
|
-
function cacheKey(request) {
|
|
8
|
-
return `gateway:request-cache:${request.method}:${request.url}`;
|
|
9
|
-
}
|
|
10
|
-
class HttpRequestCache {
|
|
11
|
-
constructor(cache = new apollo_server_caching_1.InMemoryLRUCache({
|
|
12
|
-
maxSize: MAX_SIZE,
|
|
13
|
-
})) {
|
|
14
|
-
this.cache = cache;
|
|
15
|
-
}
|
|
16
|
-
async delete(request) {
|
|
17
|
-
const key = cacheKey(request);
|
|
18
|
-
const entry = await this.cache.get(key);
|
|
19
|
-
await this.cache.delete(key);
|
|
20
|
-
return Boolean(entry);
|
|
21
|
-
}
|
|
22
|
-
async put(request, response) {
|
|
23
|
-
if (request.method === "HEAD" || response.status === 304) {
|
|
24
|
-
return response;
|
|
25
|
-
}
|
|
26
|
-
const body = await response.text();
|
|
27
|
-
this.cache.set(cacheKey(request), {
|
|
28
|
-
body,
|
|
29
|
-
status: response.status,
|
|
30
|
-
statusText: response.statusText,
|
|
31
|
-
headers: response.headers,
|
|
32
|
-
});
|
|
33
|
-
return new apollo_server_env_1.Response(body, response);
|
|
34
|
-
}
|
|
35
|
-
async match(request) {
|
|
36
|
-
return this.cache.get(cacheKey(request)).then(response => {
|
|
37
|
-
if (response) {
|
|
38
|
-
const { body, ...requestInit } = response;
|
|
39
|
-
return new apollo_server_env_1.Response(body, requestInit);
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.HttpRequestCache = HttpRequestCache;
|
|
46
|
-
//# sourceMappingURL=cache.js.map
|
package/dist/cache.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":";;;AACA,yDAA+D;AAC/D,iEAAyD;AAEzD,MAAM,QAAQ,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAEjC,SAAS,QAAQ,CAAC,OAAgB;IAChC,OAAO,yBAAyB,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;AAClE,CAAC;AASD,MAAa,gBAAgB;IAC3B,YACS,QAAyC,IAAI,wCAAgB,CAAC;QACnE,OAAO,EAAE,QAAQ;KAClB,CAAC;QAFK,UAAK,GAAL,KAAK,CAEV;IACD,CAAC;IAGJ,KAAK,CAAC,MAAM,CAAC,OAAgB;QAC3B,MAAM,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACxC,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7B,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,OAAgB,EAAE,QAAkB;QAQ5C,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;YACxD,OAAO,QAAQ,CAAC;SACjB;QAED,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAEnC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YAChC,IAAI;YACJ,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,OAAO,EAAE,QAAQ,CAAC,OAAO;SAC1B,CAAC,CAAC;QAEH,OAAO,IAAI,4BAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,OAAgB;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YACvD,IAAI,QAAQ,EAAE;gBACZ,MAAM,EAAE,IAAI,EAAE,GAAG,WAAW,EAAE,GAAG,QAAQ,CAAC;gBAC1C,OAAO,IAAI,4BAAQ,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;aACxC;YACD,OAAO;QACT,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhDD,4CAgDC"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
fetch,
|
|
3
|
-
Request,
|
|
4
|
-
Response,
|
|
5
|
-
Body,
|
|
6
|
-
BodyInit,
|
|
7
|
-
Headers,
|
|
8
|
-
HeadersInit,
|
|
9
|
-
URL,
|
|
10
|
-
URLSearchParams,
|
|
11
|
-
} from 'apollo-server-env';
|
|
12
|
-
interface FetchMock extends jest.MockedFunction<typeof fetch> {
|
|
13
|
-
mockResponseOnce(data?: any, headers?: HeadersInit, status?: number): this;
|
|
14
|
-
mockJSONResponseOnce(data?: object, headers?: HeadersInit): this;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const mockFetch = jest.fn(fetch) as unknown as FetchMock;
|
|
18
|
-
|
|
19
|
-
mockFetch.mockResponseOnce = (
|
|
20
|
-
data?: BodyInit,
|
|
21
|
-
headers?: Headers,
|
|
22
|
-
status: number = 200,
|
|
23
|
-
) => {
|
|
24
|
-
return mockFetch.mockImplementationOnce(async () => {
|
|
25
|
-
return new Response(data, {
|
|
26
|
-
status,
|
|
27
|
-
headers,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
mockFetch.mockJSONResponseOnce = (
|
|
33
|
-
data = {},
|
|
34
|
-
headers?: Headers,
|
|
35
|
-
status?: number,
|
|
36
|
-
) => {
|
|
37
|
-
return mockFetch.mockResponseOnce(
|
|
38
|
-
JSON.stringify(data),
|
|
39
|
-
Object.assign({ 'Content-Type': 'application/json' }, headers),
|
|
40
|
-
status,
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const env = {
|
|
45
|
-
fetch: mockFetch,
|
|
46
|
-
Request,
|
|
47
|
-
Response,
|
|
48
|
-
Body,
|
|
49
|
-
Headers,
|
|
50
|
-
URL,
|
|
51
|
-
URLSearchParams,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
jest.doMock('apollo-server-env', () => env);
|
|
55
|
-
|
|
56
|
-
export = env;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/// <reference path="../make-fetch-happen.d.ts" />
|
|
2
|
-
// This explicit reference shouldn't be needed because the project references
|
|
3
|
-
// the main project, which includes these type declarations. For some reason,
|
|
4
|
-
// VS Code doesn't pick that up though.
|
|
5
|
-
// (This may be related to https://github.com/microsoft/TypeScript/issues/36708.)
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
fetch,
|
|
9
|
-
Response,
|
|
10
|
-
BodyInit,
|
|
11
|
-
Headers,
|
|
12
|
-
HeadersInit
|
|
13
|
-
} from 'apollo-server-env';
|
|
14
|
-
|
|
15
|
-
import fetcher from 'make-fetch-happen';
|
|
16
|
-
|
|
17
|
-
interface MakeFetchHappenMock extends jest.MockedFunction<typeof fetch> {
|
|
18
|
-
mockResponseOnce(data?: any, headers?: HeadersInit, status?: number): this;
|
|
19
|
-
mockJSONResponseOnce(data?: object, headers?: HeadersInit): this;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const mockMakeFetchHappen = jest.fn(fetcher) as unknown as MakeFetchHappenMock;
|
|
23
|
-
const defaults = () => mockMakeFetchHappen;
|
|
24
|
-
|
|
25
|
-
mockMakeFetchHappen.mockResponseOnce = (
|
|
26
|
-
data?: BodyInit,
|
|
27
|
-
headers?: Headers,
|
|
28
|
-
status: number = 200,
|
|
29
|
-
) => {
|
|
30
|
-
return mockMakeFetchHappen.mockImplementationOnce(async () => {
|
|
31
|
-
return new Response(data, {
|
|
32
|
-
status,
|
|
33
|
-
headers,
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
mockMakeFetchHappen.mockJSONResponseOnce = (
|
|
39
|
-
data = {},
|
|
40
|
-
headers?: Headers,
|
|
41
|
-
status?: number,
|
|
42
|
-
) => {
|
|
43
|
-
return mockMakeFetchHappen.mockResponseOnce(
|
|
44
|
-
JSON.stringify(data),
|
|
45
|
-
Object.assign({ 'Content-Type': 'application/json' }, headers),
|
|
46
|
-
status,
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const makeFetchMock = {
|
|
51
|
-
fetch: mockMakeFetchHappen,
|
|
52
|
-
defaults,
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
jest.doMock('make-fetch-happen', () => makeFetchMock);
|
|
56
|
-
|
|
57
|
-
export = makeFetchMock;
|
package/src/cache.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { CacheManager } from 'make-fetch-happen';
|
|
2
|
-
import { Request, Response, Headers } from 'apollo-server-env';
|
|
3
|
-
import { InMemoryLRUCache } from 'apollo-server-caching';
|
|
4
|
-
|
|
5
|
-
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
|
|
6
|
-
|
|
7
|
-
function cacheKey(request: Request) {
|
|
8
|
-
return `gateway:request-cache:${request.method}:${request.url}`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface CachedRequest {
|
|
12
|
-
body: string;
|
|
13
|
-
status: number;
|
|
14
|
-
statusText: string;
|
|
15
|
-
headers: Headers;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class HttpRequestCache implements CacheManager {
|
|
19
|
-
constructor(
|
|
20
|
-
public cache: InMemoryLRUCache<CachedRequest> = new InMemoryLRUCache({
|
|
21
|
-
maxSize: MAX_SIZE,
|
|
22
|
-
}),
|
|
23
|
-
) {}
|
|
24
|
-
|
|
25
|
-
// Return true if entry exists, else false
|
|
26
|
-
async delete(request: Request) {
|
|
27
|
-
const key = cacheKey(request);
|
|
28
|
-
const entry = await this.cache.get(key);
|
|
29
|
-
await this.cache.delete(key);
|
|
30
|
-
return Boolean(entry);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async put(request: Request, response: Response) {
|
|
34
|
-
// A `HEAD` request has no body to cache and a 304 response could have
|
|
35
|
-
// only been negotiated by using a cached body that was still valid.
|
|
36
|
-
// Therefore, we do NOT write to the cache in either of these cases.
|
|
37
|
-
// Without avoiding this, we will invalidate the cache, thus causing
|
|
38
|
-
// subsequent conditional requests (e.g., `If-None-Match: "MD%") to be
|
|
39
|
-
// lacking content to conditionally request against and necessitating
|
|
40
|
-
// a full request/response.
|
|
41
|
-
if (request.method === "HEAD" || response.status === 304) {
|
|
42
|
-
return response;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const body = await response.text();
|
|
46
|
-
|
|
47
|
-
this.cache.set(cacheKey(request), {
|
|
48
|
-
body,
|
|
49
|
-
status: response.status,
|
|
50
|
-
statusText: response.statusText,
|
|
51
|
-
headers: response.headers,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return new Response(body, response);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async match(request: Request) {
|
|
58
|
-
return this.cache.get(cacheKey(request)).then(response => {
|
|
59
|
-
if (response) {
|
|
60
|
-
const { body, ...requestInit } = response;
|
|
61
|
-
return new Response(body, requestInit);
|
|
62
|
-
}
|
|
63
|
-
return;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* We are attempting to get types included natively in this package, but it
|
|
3
|
-
* has not happened, yet!
|
|
4
|
-
*
|
|
5
|
-
* See https://github.com/npm/make-fetch-happen/issues/20
|
|
6
|
-
*/
|
|
7
|
-
declare module 'make-fetch-happen' {
|
|
8
|
-
import {
|
|
9
|
-
Response,
|
|
10
|
-
Request,
|
|
11
|
-
RequestInfo,
|
|
12
|
-
RequestInit,
|
|
13
|
-
} from 'apollo-server-env';
|
|
14
|
-
|
|
15
|
-
// If adding to these options, they should mirror those from `make-fetch-happen`
|
|
16
|
-
// @see: https://github.com/npm/make-fetch-happen/#extra-options
|
|
17
|
-
export interface FetcherOptions {
|
|
18
|
-
cacheManager?: string | CacheManager;
|
|
19
|
-
// @see: https://www.npmjs.com/package/retry#retrytimeoutsoptions
|
|
20
|
-
retry?:
|
|
21
|
-
| boolean
|
|
22
|
-
| number
|
|
23
|
-
| {
|
|
24
|
-
// The maximum amount of times to retry the operation. Default is 10. Seting this to 1 means do it once, then retry it once
|
|
25
|
-
retries?: number;
|
|
26
|
-
// The exponential factor to use. Default is 2.
|
|
27
|
-
factor?: number;
|
|
28
|
-
// The number of milliseconds before starting the first retry. Default is 1000.
|
|
29
|
-
minTimeout?: number;
|
|
30
|
-
// The maximum number of milliseconds between two retries. Default is Infinity.
|
|
31
|
-
maxTimeout?: number;
|
|
32
|
-
// Randomizes the timeouts by multiplying with a factor between 1 to 2. Default is false.
|
|
33
|
-
randomize?: boolean;
|
|
34
|
-
};
|
|
35
|
-
onRetry?(): void;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface CacheManager {
|
|
39
|
-
delete(req: Request): Promise<Boolean>;
|
|
40
|
-
put(req: Request, res: Response): Promise<Response>;
|
|
41
|
-
match(req: Request): Promise<Response | undefined>;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* This is an augmentation of the fetch function types provided by `apollo-server-env`
|
|
46
|
-
* @see: https://git.io/JvBwX
|
|
47
|
-
*/
|
|
48
|
-
export interface Fetcher {
|
|
49
|
-
(input?: RequestInfo, init?: RequestInit & FetcherOptions): Promise<
|
|
50
|
-
Response
|
|
51
|
-
>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let fetch: Fetcher & {
|
|
55
|
-
defaults(opts?: RequestInit & FetcherOptions): typeof fetch;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export default fetch;
|
|
59
|
-
}
|