diff --git a/packages/apollo-datasource-rest/README.md b/packages/apollo-datasource-rest/README.md index aed614df182..f06df92d3fe 100644 --- a/packages/apollo-datasource-rest/README.md +++ b/packages/apollo-datasource-rest/README.md @@ -43,7 +43,7 @@ class MoviesAPI extends RESTDataSource { ### HTTP Methods -The `get` method on the [RESTDataSource](https://github.com/apollographql/apollo-server/tree/main/packages/apollo-datasource-rest) makes an HTTP `GET` request. Similarly, there are methods built-in to allow for POST, PUT, PATCH, and DELETE requests. +The `get` method on the [RESTDataSource](https://github.com/apollographql/apollo-server/tree/main/packages/apollo-datasource-rest) makes an HTTP `GET` request. Similarly, there are methods built-in to allow for HEAD, POST, PUT, PATCH, and DELETE requests. ```javascript class MoviesAPI extends RESTDataSource { @@ -85,7 +85,7 @@ class MoviesAPI extends RESTDataSource { } ``` -All of the HTTP helper functions (`get`, `put`, `post`, `patch`, and `delete`) accept a third options parameter, which can be used to set things like headers and referrers. For more info on the options available, see MDN's [fetch docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters). +All of the HTTP helper functions (`get`, `head`, `put`, `post`, `patch`, and `delete`) accept a third options parameter, which can be used to set things like headers and referrers. For more info on the options available, see MDN's [fetch docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters). ### Intercepting fetches diff --git a/packages/apollo-datasource-rest/src/RESTDataSource.ts b/packages/apollo-datasource-rest/src/RESTDataSource.ts index ce737ee7184..be6733de0bf 100644 --- a/packages/apollo-datasource-rest/src/RESTDataSource.ts +++ b/packages/apollo-datasource-rest/src/RESTDataSource.ts @@ -161,6 +161,16 @@ export abstract class RESTDataSource extends DataSource { ); } + protected async head( + path: string, + params?: URLSearchParamsInit, + init?: RequestInit, + ): Promise { + return this.fetch( + Object.assign({ method: 'HEAD', path, params }, init), + ); + } + protected async post( path: string, body?: Body, diff --git a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts index e001fcb42dc..643ff158782 100644 --- a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts +++ b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts @@ -355,7 +355,7 @@ describe('RESTDataSource', () => { ); }); - for (const method of ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']) { + for (const method of ['GET', 'HEAD', 'POST', 'PATCH', 'PUT', 'DELETE']) { const dataSource = new (class extends RESTDataSource { baseURL = 'https://api.example.com'; @@ -363,6 +363,10 @@ describe('RESTDataSource', () => { return this.get('foo'); } + headFoo() { + return this.head('foo'); + } + postFoo() { return this.post('foo'); } @@ -383,12 +387,19 @@ describe('RESTDataSource', () => { it(`allows performing ${method} requests`, async () => { dataSource.httpCache = httpCache; - fetch.mockJSONResponseOnce({ foo: 'bar' }); + if (method === 'HEAD') { + fetch.mockResponseOnce(''); + } else { + fetch.mockJSONResponseOnce({ foo: 'bar' }); + } const data = await dataSource[`${method.toLocaleLowerCase()}Foo`](); - expect(data).toEqual({ foo: 'bar' }); - + if (method === 'HEAD') { + expect(data).toEqual(''); + } else { + expect(data).toEqual({ foo: 'bar' }); + } expect(fetch.mock.calls.length).toEqual(1); expect(fetch.mock.calls[0][0].method).toEqual(method); });