Skip to content

Commit

Permalink
fix(client): throw error if constructor not initialized properly (#2332)
Browse files Browse the repository at this point in the history
* fix(client): throw errors if constructor not initialized properly

* fix(client): fix link to docs in error msg

* chore(changelog): added changes
  • Loading branch information
peggyrayzis authored and James Baxley committed Oct 19, 2017
1 parent eeb27f3 commit d1be76a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"license": "MIT",
"scripts": {
"bootstrap": "npm i && lerna bootstrap",
"postinstall": "lerna bootstrap",
"postbootstrap": "npm run build",
"build": "lerna run -- build",
"test": "lerna run -- test",
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### vNext
- Define and expose `ApolloClientOptions`, Type of an object that represents ApolloClient's constructor argument.
- Expose `ApolloCurrentResult`
- Throw an error if cache or data are not supplied to the `ApolloClient` constructor
- Add `graphql` as a dev dependency

### 2.0.0-rc.3
- Only include `data` on subscriptionData when using `subscribeToMore`
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"bundlesize": "0.15.3",
"danger": "1.1.0",
"flow-bin": "0.57.2",
"graphql": "^0.11.0",
"graphql-tag": "2.4.2",
"isomorphic-fetch": "2.2.1",
"jest": "20.0.4",
Expand Down
9 changes: 9 additions & 0 deletions packages/apollo-client/src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
defaultOptions,
} = options;

if (!link || !cache) {
throw new Error(`
In order to initialize Apollo Client, you must specify link & cache properties on the config object.
For more information, please visit:
https://apollographql.com/docs/react/setup
to help you get started.
`);
}

this.link = link;
this.cache = cache;
this.store = new DataStore(cache);
Expand Down
24 changes: 24 additions & 0 deletions packages/apollo-client/src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ import { withWarning } from '../util/wrap';
import ApolloClient from '../';

describe('ApolloClient', () => {
describe('constructor', () => {
it('will throw an error if link is not passed in', () => {
expect(() => {
const client = new ApolloClient({ cache: new InMemoryCache() });
}).toThrowError(`
In order to initialize Apollo Client, you must specify link & cache properties on the config object.
For more information, please visit:
https://apollographql.com/docs/react/setup
to help you get started.
`);
});

it('will throw an error if cache is not passed in', () => {
expect(() => {
const client = new ApolloClient({ link: new ApolloLink.empty() });
}).toThrowError(`
In order to initialize Apollo Client, you must specify link & cache properties on the config object.
For more information, please visit:
https://apollographql.com/docs/react/setup
to help you get started.
`);
});
});

describe('readQuery', () => {
it('will read some data from the store', () => {
const client = new ApolloClient({
Expand Down

0 comments on commit d1be76a

Please sign in to comment.