From 69617bc776bbc49010d948743027206caa8e9cea Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Wed, 28 Feb 2018 18:06:29 -0500 Subject: [PATCH 1/4] Allow apollo-boost to support headers and credentials --- packages/apollo-boost/CHANGELOG.md | 1 + packages/apollo-boost/src/index.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/apollo-boost/CHANGELOG.md b/packages/apollo-boost/CHANGELOG.md index 9992d6c1c84..bb630114389 100644 --- a/packages/apollo-boost/CHANGELOG.md +++ b/packages/apollo-boost/CHANGELOG.md @@ -2,6 +2,7 @@ ### vNext - Map coverage to original source +- Allow instantiating the client with `headers` and `credentials`. ### 0.1.0 - DEPRECATED: `apollo-client-preset` diff --git a/packages/apollo-boost/src/index.ts b/packages/apollo-boost/src/index.ts index 233e127e400..ed54e029a76 100644 --- a/packages/apollo-boost/src/index.ts +++ b/packages/apollo-boost/src/index.ts @@ -17,6 +17,8 @@ export { gql, InMemoryCache, HttpLink }; export interface PresetConfig { request?: (operation: Operation) => Promise; uri?: string; + credentials?: string; + headers?: any; fetchOptions?: HttpLink.Options; clientState?: ClientStateConfig; onError?: ErrorLink.ErrorHandler; @@ -76,7 +78,8 @@ export default class DefaultClient extends ApolloClient { 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) || {}, }); const link = ApolloLink.from([ From 5828101323ba227bc3282fb1a0e3e46120567b20 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Mon, 4 Jun 2018 10:46:43 -0400 Subject: [PATCH 2/4] Add tests to verify `credentials` / `headers` config override --- packages/apollo-boost/package.json | 1 + packages/apollo-boost/src/__tests__/config.ts | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/packages/apollo-boost/package.json b/packages/apollo-boost/package.json index ae21c63d08b..48a058f5ea0 100644 --- a/packages/apollo-boost/package.json +++ b/packages/apollo-boost/package.json @@ -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", diff --git a/packages/apollo-boost/src/__tests__/config.ts b/packages/apollo-boost/src/__tests__/config.ts index bfdd04d254e..4ef1496c25d 100644 --- a/packages/apollo-boost/src/__tests__/config.ts +++ b/packages/apollo-boost/src/__tests__/config.ts @@ -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({}) }), @@ -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', + }); + }); + }); }); From 65e5e7c2c9ffc49baf33297ec5a38d9955799209 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Mon, 4 Jun 2018 16:26:42 -0400 Subject: [PATCH 3/4] Update CHANGELOG --- packages/apollo-boost/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/apollo-boost/CHANGELOG.md b/packages/apollo-boost/CHANGELOG.md index 74471172725..71953df50e5 100644 --- a/packages/apollo-boost/CHANGELOG.md +++ b/packages/apollo-boost/CHANGELOG.md @@ -2,7 +2,9 @@ ### vNext -- Allow instantiating the client with `headers` and `credentials`. +- 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 From 498168d10cc6c8deedba4ec46ffb45adae663613 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Tue, 5 Jun 2018 06:58:53 -0400 Subject: [PATCH 4/4] `apollo-boost` config options doc updates; Hexo update --- docs/package.json | 4 ++-- docs/source/essentials/get-started.md | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/package.json b/docs/package.json index 5ea98d09fe3..eda2ae73375 100644 --- a/docs/package.json +++ b/docs/package.json @@ -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", @@ -26,4 +26,4 @@ "clean": "hexo clean", "test": "npm run clean; npm run build" } -} +} \ No newline at end of file diff --git a/docs/source/essentials/get-started.md b/docs/source/essentials/get-started.md index 09d685c6525..627e75c2c91 100644 --- a/docs/source/essentials/get-started.md +++ b/docs/source/essentials/get-started.md @@ -158,6 +158,10 @@ Here are the options you can pass to the `ApolloClient` exported from `apollo-bo
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).
`cacheRedirects`: Object
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).
+
`credentials`: string
+
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.
+
`headers`: Object
+
Header key/value pairs to pass along with the request.

Next steps

@@ -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. -