feat: use new services endpoint in SDK and add redirect for legacy endpoint (#862)

This commit is contained in:
Laurin Quast 2022-12-22 12:14:23 +01:00 committed by GitHub
parent 706b1d61c4
commit d2aa98a574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'@graphql-hive/client': minor
---
Use new CDN endpoint for retrieving the service list

View file

@ -30,7 +30,7 @@ function createFetcher<T>({ endpoint, key }: SchemaFetcherOptions & ServicesFetc
}
return axios
.get(endpoint + '/schema', {
.get(endpoint + '/services', {
headers,
responseType: 'json',
})

View file

@ -19,12 +19,12 @@ test('createServicesFetcher without ETag', async () => {
};
const key = 'secret-key';
nock('http://localhost')
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.reply(() => [200, [schema]])
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
@ -65,20 +65,20 @@ test('createServicesFetcher with ETag', async () => {
};
const key = 'secret-key';
nock('http://localhost')
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.reply(200, [schema], {
ETag: 'first',
})
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.matchHeader('If-None-Match', 'first')
.reply(304)
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
@ -130,12 +130,12 @@ test('createSchemaFetcher without ETag (older versions)', async () => {
};
const key = 'secret-key';
nock('http://localhost')
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.reply(() => [200, schema])
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
@ -174,20 +174,20 @@ test('createSchemaFetcher with ETag', async () => {
};
const key = 'secret-key';
nock('http://localhost')
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.reply(200, schema, {
ETag: 'first',
})
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')
.matchHeader('If-None-Match', 'first')
.reply(304)
.get('/schema')
.get('/services')
.once()
.matchHeader('X-Hive-CDN-Key', key)
.matchHeader('accept', 'application/json')

View file

@ -33,6 +33,7 @@ const ParamsModel = zod.object({
zod.literal('sdl.graphql'),
zod.literal('sdl.graphqls'),
zod.literal('services'),
zod.literal('schema'),
zod.literal('supergraph'),
]),
});
@ -72,6 +73,16 @@ export const createArtifactRequestHandler = (deps: ArtifactRequestHandler) => {
const params = parseResult.data;
/** Legacy handling for old client SDK versions. */
if (params.artifactType === 'schema') {
return new Response('Found.', {
status: 301,
headers: {
Location: request.url.replace('/schema', '/services'),
},
});
}
const maybeResponse = await authenticate(request, params.targetId);
if (maybeResponse !== null) {