Skip to content

Commit

Permalink
warn when user try to use apollo-boost as an apollo-client
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin- committed Jun 6, 2018
1 parent ffea402 commit 2ac275e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
10 changes: 10 additions & 0 deletions packages/apollo-boost/src/__tests__/__snapshots__/config.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`config warns about unsupported parameter 1`] = `
Array [
Array [
"ApolloBoot client was initialized with unsupported options: link
https://github.com/apollographql/apollo-client/tree/master/packages/apollo-boost#apollo-boost-options",
],
]
`;
10 changes: 10 additions & 0 deletions packages/apollo-boost/src/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ describe('config', () => {
},
};

it('warns about unsupported parameter', () => {
jest.spyOn(global.console, 'warn');

const client = new ApolloClient({
link: [],
});

expect(global.console.warn.mock.calls).toMatchSnapshot();
});

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

Expand Down
68 changes: 50 additions & 18 deletions packages/apollo-boost/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,39 @@ export interface PresetConfig {
cacheRedirects?: CacheResolverMap;
}

const PRESENT_CONFIG_KEYS = [
'request',
'uri',
'credentials',
'headers',
'fetchOptions',
'clientState',
'onError',
'cacheRedirects',
];

function include<T>(b: T, a: Array<T>): boolean {
return a.indexOf(b) >= 0;
}

function difference<T>(a: Array<T>, b: Array<T>): Array<T> {
return a.filter(x => !include(x, b));
}

export default class DefaultClient<TCache> extends ApolloClient<TCache> {
constructor(config: PresetConfig) {
if (config) {
const diff = difference(Object.keys(config), PRESENT_CONFIG_KEYS);

if (diff.length > 0) {
// prettier-ignore
console.warn(
`ApolloBoot client was initialized with unsupported options: ${diff.join(' ')}\n` +
`https://github.com/apollographql/apollo-client/tree/master/packages/apollo-boost#apollo-boost-options`,
);
}
}

const cache =
config && config.cacheRedirects
? new InMemoryCache({ cacheRedirects: config.cacheRedirects })
Expand All @@ -52,24 +83,25 @@ export default class DefaultClient<TCache> extends ApolloClient<TCache> {

const requestHandler =
config && config.request
? new ApolloLink((operation, forward) =>
new Observable(observer => {
let handle: any;
Promise.resolve(operation)
.then(oper => config.request(oper))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));

return () => {
if (handle) handle.unsubscribe;
};
})
? new ApolloLink(
(operation, forward) =>
new Observable(observer => {
let handle: any;
Promise.resolve(operation)
.then(oper => config.request(oper))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));

return () => {
if (handle) handle.unsubscribe;
};
}),
)
: false;

Expand Down

0 comments on commit 2ac275e

Please sign in to comment.