From 7488053bce6a9b01f5336de7c120793f62841448 Mon Sep 17 00:00:00 2001 From: Caleb Meredith Date: Fri, 20 Jan 2017 18:38:59 -0500 Subject: [PATCH] start implementing strict nullability --- src/ApolloClient.ts | 14 +++++--------- src/actions.ts | 2 +- src/core/ObservableQuery.ts | 6 +++--- src/core/QueryManager.ts | 29 +++++++++++++++++------------ src/data/readFromStore.ts | 2 +- tsconfig.json | 3 ++- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index ea43366ff81..969c5f41c64 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -91,10 +91,10 @@ export default class ApolloClient { public queryManager: QueryManager; public reducerConfig: ApolloReducerConfig; public addTypename: boolean; - public resultTransformer: ResultTransformer; - public resultComparator: ResultComparator; + public resultTransformer: ResultTransformer | undefined; + public resultComparator: ResultComparator | undefined; public shouldForceFetch: boolean; - public dataId: IdGetter; + public dataId: IdGetter | undefined; public fieldWithArgs: (fieldName: string, args?: Object) => string; public version: string; public queryDeduplication: boolean; @@ -148,7 +148,7 @@ export default class ApolloClient { connectToDevTools?: boolean, queryDeduplication?: boolean, } = {}) { - let { + const { networkInterface, reduxRootKey, reduxRootSelector, @@ -227,11 +227,7 @@ export default class ApolloClient { !isProduction() && typeof window !== 'undefined' && (!(window as any).__APOLLO_CLIENT__); - if (typeof connectToDevTools === 'undefined') { - connectToDevTools = defaultConnectToDevTools; - } - - if (connectToDevTools) { + if (typeof connectToDevTools === 'undefined' ? defaultConnectToDevTools : connectToDevTools) { (window as any).__APOLLO_CLIENT__ = this; } diff --git a/src/actions.ts b/src/actions.ts index 2c7559a87aa..27454b6d063 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -83,7 +83,7 @@ export interface MutationInitAction { variables: Object; operationName: string; mutationId: string; - optimisticResponse: Object; + optimisticResponse: Object | undefined; extraReducers?: ApolloReducer[]; updateQueries?: { [queryId: string]: MutationQueryReducer }; } diff --git a/src/core/ObservableQuery.ts b/src/core/ObservableQuery.ts index 6cf604d5022..760f1fc7c69 100644 --- a/src/core/ObservableQuery.ts +++ b/src/core/ObservableQuery.ts @@ -49,7 +49,7 @@ export interface FetchMoreOptions { } export interface UpdateQueryOptions { - variables: Object; + variables?: Object; } export class ObservableQuery extends Observable> { @@ -307,7 +307,7 @@ export class ObservableQuery extends Observable> { // If forceFetch went from false to true or noFetch went from true to false const tryFetch: boolean = (!oldOptions.forceFetch && opts.forceFetch) - || (oldOptions.noFetch && !opts.noFetch); + || (oldOptions.noFetch && !opts.noFetch) || false; return this.setVariables(this.options.variables, tryFetch); } @@ -335,7 +335,7 @@ export class ObservableQuery extends Observable> { ...variables, }; - const nullPromise = new Promise((resolve) => resolve(null)); + const nullPromise = new Promise((resolve) => resolve()); if (isEqual(newVariables, this.variables) && !tryFetch) { // If we have no observers, then we don't actually want to make a network diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index 043ced3227e..6c247b6c28b 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -33,6 +33,7 @@ import { Store, getDataWithOptimisticResults, ApolloReducerConfig, + ApolloReducer, } from '../store'; import { @@ -121,8 +122,8 @@ export class QueryManager { private networkInterface: NetworkInterface; private deduplicator: Deduplicator; private reduxRootSelector: ApolloStateSelector; - private resultTransformer: ResultTransformer; - private resultComparator: ResultComparator; + private resultTransformer: ResultTransformer | undefined; + private resultComparator: ResultComparator | undefined; private reducerConfig: ApolloReducerConfig; private queryDeduplication: boolean; @@ -257,7 +258,7 @@ export class QueryManager { type: 'APOLLO_MUTATION_INIT', mutationString, mutation, - variables, + variables: variables || {}, operationName: getOperationName(mutation), mutationId, optimisticResponse, @@ -280,7 +281,7 @@ export class QueryManager { mutationId, document: mutation, operationName: getOperationName(mutation), - variables, + variables: variables || {}, extraReducers: this.getExtraReducers(), updateQueries, }); @@ -358,7 +359,7 @@ export class QueryManager { /* tslint:disable-next-line */ console.info( 'An unhandled error was thrown because no error handler is registered ' + - 'for the query ' + options.query.loc.source, + 'for the query ' + queryStoreValue.queryString, ); } } @@ -492,7 +493,7 @@ export class QueryManager { }); // If we're in here, only fetch if we have missing fields - needToFetch = isMissing; + needToFetch = isMissing || false; storeResult = result; } @@ -694,7 +695,9 @@ export class QueryManager { const handler = (error: Error, result: any) => { if (error) { observers.forEach((obs) => { - obs.error(error); + if (obs.error) { + obs.error(error); + } }); } else { this.store.dispatch({ @@ -702,13 +705,15 @@ export class QueryManager { document: transformedDoc, operationName: getOperationName(transformedDoc), result: { data: result }, - variables, + variables: variables || {}, subscriptionId: subId, extraReducers: this.getExtraReducers(), }); // It's slightly awkward that the data for subscriptions doesn't come from the store. observers.forEach((obs) => { - obs.next(result); + if (obs.next) { + obs.next(result); + } }); } }; @@ -851,18 +856,18 @@ export class QueryManager { }; } - private getExtraReducers() { + private getExtraReducers(): ApolloReducer[] { return Object.keys(this.observableQueries).map( obsQueryId => { const queryOptions = this.observableQueries[obsQueryId].observableQuery.options; if (queryOptions.reducer) { return createStoreReducer( queryOptions.reducer, queryOptions.query, - queryOptions.variables, + queryOptions.variables || {}, this.reducerConfig, ); } - return null; + return null as never; }).filter( reducer => reducer !== null ); } diff --git a/src/data/readFromStore.ts b/src/data/readFromStore.ts index f5afd25a4f1..e1bd3dae6f8 100644 --- a/src/data/readFromStore.ts +++ b/src/data/readFromStore.ts @@ -256,7 +256,7 @@ export function diffQueryAgainstStore({ // Global settings store, returnPartialData, - customResolvers: config && config.customResolvers, + customResolvers: (config && config.customResolvers) || {}, // Flag set during execution hasMissingField: false, diff --git a/tsconfig.json b/tsconfig.json index 29b8708907b..047470d84cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,8 @@ "removeComments": true, "noImplicitAny": true, "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "strictNullChecks": true }, "files": [ "node_modules/typescript/lib/lib.es2015.d.ts",