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

Allow apollo-boost to support headers and credentials #3098

Merged
merged 7 commits into from
Jun 5, 2018
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
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"hexo": {
"version": "3.6.0"
"version": "3.7.1"
},
"devDependencies": {
"apollo-hexo-config": "1.0.7",
Expand All @@ -26,4 +26,4 @@
"clean": "hexo clean",
"test": "npm run clean; npm run build"
}
}
}
5 changes: 4 additions & 1 deletion docs/source/essentials/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ Here are the options you can pass to the `ApolloClient` exported from `apollo-bo
<dd>An object representing your configuration for `apollo-link-state`. This is useful if you would like to use the Apollo cache for local state management. Learn more in our [quick start](/docs/link/links/state.html#start).</dd>
<dt>`cacheRedirects`: Object</dt>
<dd>A map of functions to redirect a query to another entry in the cache before a request takes place. This is useful if you have a list of items and want to use the data from the list query on a detail page where you're querying an individual item. More on that [here](../features/performance.html#cache-redirects).</dd>
<dt>`credentials`: string</dt>
<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>
</dl>

<h2 id="next-steps">Next steps</h2>
Expand All @@ -167,4 +171,3 @@ Now that you've learned how to fetch data with Apollo Client, you're ready to di
- [Queries](./queries.html): Learn how to fetch queries with arguments and dive deeper into configuration options. For a full list of options, check out the API reference for `Query`.
- [Mutations](./mutations.html): Learn how to update data with mutations and when you'll need to update the Apollo cache. For a full list of options, check out the API reference for `Mutation` components.
- [Apollo Client API](../api/apollo-client.html): Sometimes, you'll need to access the client directly like we did in our plain JavaScript example above. Visit the API reference for a full list of options.

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 `headers` and `credentials` to be passed in as configuration
parameters to the `apollo-boost` `ApolloClient` constructor.
[PR #3098](https://github.com/apollographql/apollo-client/pull/3098)

### 0.1.7

- No public facing functionality changes.
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-boost/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"apollo-cache": "^1.1.9",
"apollo-utilities": "^1.0.13",
"browserify": "15.2.0",
"fetch-mock": "^6.4.3",
"graphql": "0.13.2",
"jest": "23.0.0",
"lodash": "4.17.10",
Expand Down
75 changes: 75 additions & 0 deletions packages/apollo-boost/src/__tests__/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ApolloClient, { gql } from '../';
import { stripSymbols } from 'apollo-utilities';
import { HttpLink } from 'apollo-link-http';
import * as fetchMock from 'fetch-mock';

global.fetch = jest.fn(() =>
Promise.resolve({ json: () => Promise.resolve({}) }),
Expand Down Expand Up @@ -67,4 +69,77 @@ describe('config', () => {

expect(client.cache.config.cacheRedirects).toEqual(cacheRedirects);
});

const makePromise = res =>
new Promise((resolve, reject) => setTimeout(() => resolve(res)));
const data = { data: { hello: 'world' } };

describe('credentials', () => {
beforeEach(() => {
fetchMock.restore();
fetchMock.post('/graphql', makePromise(data));
});

afterEach(() => {
fetchMock.restore();
});

it('should set `credentials` to `same-origin` by default', () => {
const client = new ApolloClient({});
client.query({ query, errorPolicy: 'ignore' });
const [uri, options] = fetchMock.lastCall();
expect(options.credentials).toEqual('same-origin');
});

it('should set `credentials` to `config.credentials` if supplied', () => {
const client = new ApolloClient({
credentials: 'some-new-value',
});
client.query({ query, errorPolicy: 'ignore' });
const [uri, options] = fetchMock.lastCall();
expect(options.credentials).toEqual('some-new-value');
});
});

describe('headers', () => {
beforeEach(() => {
fetchMock.restore();
fetchMock.post('/graphql', makePromise(data));
});

afterEach(() => {
fetchMock.restore();
});

it(
'should leave existing `headers` in place if no new headers are ' +
'provided',
() => {
const client = new ApolloClient({});
client.query({ query, errorPolicy: 'ignore' });
const [uri, options] = fetchMock.lastCall();
expect(options.headers).toEqual({
accept: '*/*',
'content-type': 'application/json',
});
},
);

it('should add new `config.headers` to existing headers', () => {
const client = new ApolloClient({
headers: {
'new-header1': 'value1',
'new-header2': 'value2',
},
});
client.query({ query, errorPolicy: 'ignore' });
const [uri, options] = fetchMock.lastCall();
expect(options.headers).toEqual({
accept: '*/*',
'content-type': 'application/json',
'new-header1': 'value1',
'new-header2': 'value2',
});
});
});
});
5 changes: 4 additions & 1 deletion packages/apollo-boost/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export { gql, InMemoryCache, HttpLink };
export interface PresetConfig {
request?: (operation: Operation) => Promise<void>;
uri?: string;
credentials?: string;
headers?: any;
fetchOptions?: HttpLink.Options;
clientState?: ClientStateConfig;
onError?: ErrorLink.ErrorHandler;
Expand Down Expand Up @@ -74,7 +76,8 @@ export default class DefaultClient<TCache> extends ApolloClient<TCache> {
const httpLink = new HttpLink({
uri: (config && config.uri) || '/graphql',
fetchOptions: (config && config.fetchOptions) || {},
credentials: 'same-origin',
credentials: (config && config.credentials) || 'same-origin',
headers: (config && config.headers) || {},
Copy link
Contributor

@alexkrolick alexkrolick Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably have been part of fetchOptions, right? That's the way it's documented.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have lost a night trying to figure this out. It is simply in the wrong place...
Since the beginning I was inserting it inside fetchOptions.... 😡

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had this issue, the documentation should be clear to place credentials in the root and not fetchOptions as if placed in fetchOptions it will be overwritten by the root credentials whicih defaults to 'same-origin'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had the same problem today... =/

});

const link = ApolloLink.from([
Expand Down