From d1be76a325ba8315a67e93ead62c0e496ace0746 Mon Sep 17 00:00:00 2001 From: Peggy Rayzis Date: Thu, 19 Oct 2017 09:22:46 -0400 Subject: [PATCH] fix(client): throw error if constructor not initialized properly (#2332) * fix(client): throw errors if constructor not initialized properly * fix(client): fix link to docs in error msg * chore(changelog): added changes --- package.json | 1 + packages/apollo-client/CHANGELOG.md | 2 ++ packages/apollo-client/package.json | 1 + packages/apollo-client/src/ApolloClient.ts | 9 +++++++ .../src/__tests__/ApolloClient.ts | 24 +++++++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/package.json b/package.json index c628af638f8..291b5b66b4d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/apollo-client/CHANGELOG.md b/packages/apollo-client/CHANGELOG.md index 7013b7f79ae..5a62ab80ca5 100644 --- a/packages/apollo-client/CHANGELOG.md +++ b/packages/apollo-client/CHANGELOG.md @@ -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` diff --git a/packages/apollo-client/package.json b/packages/apollo-client/package.json index c1c00ad8c38..15dc91c0d9f 100644 --- a/packages/apollo-client/package.json +++ b/packages/apollo-client/package.json @@ -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", diff --git a/packages/apollo-client/src/ApolloClient.ts b/packages/apollo-client/src/ApolloClient.ts index f3072ffab4c..f8d21c1fe20 100644 --- a/packages/apollo-client/src/ApolloClient.ts +++ b/packages/apollo-client/src/ApolloClient.ts @@ -91,6 +91,15 @@ export default class ApolloClient 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); diff --git a/packages/apollo-client/src/__tests__/ApolloClient.ts b/packages/apollo-client/src/__tests__/ApolloClient.ts index 9a1ab0d00c1..79b57e62c06 100644 --- a/packages/apollo-client/src/__tests__/ApolloClient.ts +++ b/packages/apollo-client/src/__tests__/ApolloClient.ts @@ -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({