-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathnockMocks.ts
96 lines (85 loc) · 2.48 KB
/
nockMocks.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
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
import nock from 'nock';
import { MockService } from './networkRequests.test';
import { HEALTH_CHECK_QUERY, SERVICE_DEFINITION_QUERY } from '../..';
import { SUPERGRAPH_SDL_QUERY } from '../../loadSupergraphSdlFromStorage';
import { getTestingSupergraphSdl } from '../../__tests__/execution-utils';
import { print } from 'graphql';
import { fixtures } from 'apollo-federation-integration-testsuite';
export const graphRef = 'federated-service@current';
export const apiKey = 'service:federated-service:DD71EBbGmsuh-6suUVDwnA';
const apiKeyHash = 'dd55a79d467976346d229a7b12b673ce';
export const mockApolloConfig = {
apollo: {
key: apiKey,
keyHash: apiKeyHash,
graphRef,
},
};
// Service mocks
function mockSdlQuery({ url }: MockService) {
return nock(url).post('/', {
query: SERVICE_DEFINITION_QUERY,
});
}
export function mockSdlQuerySuccess(service: MockService) {
return mockSdlQuery(service).reply(200, {
data: { _service: { sdl: print(service.typeDefs) } },
});
}
export function mockServiceHealthCheck({ url }: MockService) {
return nock(url).post('/', {
query: HEALTH_CHECK_QUERY,
});
}
export function mockServiceHealthCheckSuccess(service: MockService) {
return mockServiceHealthCheck(service).reply(200, {
data: { __typename: 'Query' },
});
}
export function mockAllServicesHealthCheckSuccess() {
return fixtures.map((fixture) =>
mockServiceHealthCheck(fixture).reply(200, {
data: { __typename: 'Query' },
}),
);
}
// Supergraph SDL fetching mocks
function gatewayNock(url: Parameters<typeof nock>[0]): nock.Scope {
const { name, version } = require('../../../package.json');
return nock(url, {
reqheaders: {
'apollographql-client-name': name,
'apollographql-client-version': version,
'user-agent': `${name}/${version}`,
'content-type': 'application/json',
},
});
}
export const mockCloudConfigUrl =
'https://example.cloud-config-url.com/cloudconfig/';
export function mockSupergraphSdlRequest() {
return gatewayNock(mockCloudConfigUrl).post('/', {
query: SUPERGRAPH_SDL_QUERY,
variables: {
ref: graphRef,
apiKey: apiKey,
},
});
}
export function mockSupergraphSdlRequestSuccess(
supergraphSdl = getTestingSupergraphSdl(),
id = 'originalId-1234',
) {
return mockSupergraphSdlRequest().reply(
200,
JSON.stringify({
data: {
routerConfig: {
__typename: 'RouterConfigResult',
id,
supergraphSdl,
},
},
}),
);
}