Skip to content
This repository was archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14 from gramps-graphql/feat/head-support
Browse files Browse the repository at this point in the history
feat(GraphQLConnector): add support for HEAD requests
  • Loading branch information
loganmccaul authored May 20, 2019
2 parents d9e0f94 + 1d55198 commit 8f35939
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/GraphQLConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ export default class GraphQLConnector {
});
}

/**
* Configures and sends a HEAD request to a REST API endpoint.
* @param {string} endpoint the API endpoint to send the request to
* @param {object} options optional configuration for request-promise
* @return {Promise} Promise that resolves with the request result
*/
head(endpoint, options = {}) {
return this.mutation(endpoint, 'HEAD', {
...options,
});
}

createLoader() {
// We can enable batched queries later on, which may be more performant.
this.loader = new DataLoader(this.load, {
Expand Down
30 changes: 30 additions & 0 deletions test/GraphQLConnector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,34 @@ describe('GraphQLConnector', () => {
);
});
});

describe('head()', () => {
it('sends a properly configured HEAD request', () => {
const tc = new TestConnector();

tc.apiBaseUri = 'https://example.com';
tc.head('/test/head');

expect(tc.request).toHaveBeenCalledWith({
uri: 'https://example.com/test/head',
json: true,
resolveWithFullResponse: true,
headers: {},
method: 'HEAD',
});
});

it('adds custom options as expected', () => {
const tc = new TestConnector();

tc.apiBaseUri = 'https://example.com';
tc.head('/test/head', { custom: 'option' });

expect(tc.request).toHaveBeenCalledWith(
expect.objectContaining({
custom: 'option',
}),
);
});
});
});

0 comments on commit 8f35939

Please sign in to comment.