Skip to content

Commit

Permalink
Fixes #3131. Adds HEAD method helper to RESTDataSource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Lin committed Nov 16, 2020
1 parent a26c08d commit 88a27e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/apollo-datasource-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
);
}

protected async head<TResult = any>(
path: string,
params?: URLSearchParamsInit,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'HEAD', path, params }, init),
);
}

protected async post<TResult = any>(
path: string,
body?: Body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,18 @@ 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';

getFoo() {
return this.get('foo');
}

headFoo() {
return this.head('foo');
}

postFoo() {
return this.post('foo');
}
Expand All @@ -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);
});
Expand Down

0 comments on commit 88a27e7

Please sign in to comment.