Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepting custom fetch in ApolloBoost #3590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
[@JakeDawkins](https://github.com/JakeDawkins) in [#3642](https://github.com/apollographql/apollo-client/pull/3642)
[@hwillson](https://github.com/hwillson) in [#3644](https://github.com/apollographql/apollo-client/pull/3644)

### Apollo Boost (vNext)

- Allow `fetch` to be given as a configuration option to `ApolloBoost`.
[@mbaranovski](https://github.com/mbaranovski) in [#3590](https://github.com/apollographql/apollo-client/pull/3590)

## 2.3.5 (June 19, 2018)

### Apollo Client (2.3.5)
Expand Down
2 changes: 2 additions & 0 deletions docs/source/essentials/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Here are the options you can pass to the `ApolloClient` exported from `apollo-bo
<dd>Is set to `same-origin` by default. This option can be used to indicate whether the user agent should send cookies with requests. See [Request.credentials](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials) for more details.</dd>
<dt>`headers`: Object</dt>
<dd>Header key/value pairs to pass along with the request.</dd>
<dt>`fetch`: GlobalFetch['fetch']</dt>
<dd>A [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) compatible API for making a request.</dd>
</dl>

<h2 id="next-steps">Next steps</h2>
Expand Down
6 changes: 6 additions & 0 deletions packages/apollo-boost/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

### vNext

- Allow `fetch` to be given as a configuration option to ApolloBoost.
[Issue #3578](https://github.com/apollographql/apollo-client/issues/3578)
[PR #3590](https://github.com/apollographql/apollo-client/pull/3590)

### 0.1.10

- No changes.
Expand Down
18 changes: 17 additions & 1 deletion packages/apollo-boost/src/__tests__/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ApolloClient, { gql, InMemoryCache } from '../';
import { stripSymbols } from 'apollo-utilities';
import { HttpLink } from 'apollo-link-http';
import * as fetchMock from 'fetch-mock';

global.fetch = jest.fn(() =>
Expand All @@ -22,6 +21,23 @@ describe('config', () => {
},
};

it('allows you to pass in a custom fetcher', () => {
const customFetcher = jest.fn(() =>
Promise.resolve({
text: () => Promise.resolve('{"data": {"foo": "bar" }}'),
}),
);

const client = new ApolloClient({
fetch: customFetcher,
});

client.query({ query }).then(({ data }) => {
expect(customFetcher).toHaveBeenCalledTimes(1);
expect(stripSymbols(data)).toEqual({ foo: 'bar' });
});
});

it('allows you to pass in a request handler', () => {
let requestCalled;

Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-boost/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface PresetConfig {
uri?: string;
credentials?: string;
headers?: any;
fetch?: GlobalFetch['fetch'];
fetchOptions?: HttpLink.Options;
clientState?: ClientStateConfig;
onError?: ErrorLink.ErrorHandler;
Expand All @@ -34,6 +35,7 @@ export default class DefaultClient<TCache> extends ApolloClient<TCache> {
uri,
credentials,
headers,
fetch,
fetchOptions,
clientState,
cacheRedirects,
Expand Down Expand Up @@ -104,6 +106,7 @@ export default class DefaultClient<TCache> extends ApolloClient<TCache> {

const httpLink = new HttpLink({
uri: uri || '/graphql',
fetch,
fetchOptions: fetchOptions || {},
credentials: credentials || 'same-origin',
headers: headers || {},
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-utilities/src/storeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export function valueToObjectRepresentation(
} else {
throw new Error(
`The inline argument "${name.value}" of kind "${(value as any).kind}"` +
'is not supported. Use variables instead of inline arguments to ' +
'overcome this limitation.'
'is not supported. Use variables instead of inline arguments to ' +
'overcome this limitation.',
);
}
}
Expand Down