Skip to content

Commit

Permalink
start implementing strict nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Jan 23, 2017
1 parent 12caf3d commit 7488053
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
14 changes: 5 additions & 9 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class ApolloClient {
connectToDevTools?: boolean,
queryDeduplication?: boolean,
} = {}) {
let {
const {
networkInterface,
reduxRootKey,
reduxRootSelector,
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface MutationInitAction {
variables: Object;
operationName: string;
mutationId: string;
optimisticResponse: Object;
optimisticResponse: Object | undefined;
extraReducers?: ApolloReducer[];
updateQueries?: { [queryId: string]: MutationQueryReducer };
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface FetchMoreOptions {
}

export interface UpdateQueryOptions {
variables: Object;
variables?: Object;
}

export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
Expand Down Expand Up @@ -307,7 +307,7 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {

// 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);
}
Expand Down Expand Up @@ -335,7 +335,7 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
...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
Expand Down
29 changes: 17 additions & 12 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
Store,
getDataWithOptimisticResults,
ApolloReducerConfig,
ApolloReducer,
} from '../store';

import {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -257,7 +258,7 @@ export class QueryManager {
type: 'APOLLO_MUTATION_INIT',
mutationString,
mutation,
variables,
variables: variables || {},
operationName: getOperationName(mutation),
mutationId,
optimisticResponse,
Expand All @@ -280,7 +281,7 @@ export class QueryManager {
mutationId,
document: mutation,
operationName: getOperationName(mutation),
variables,
variables: variables || {},
extraReducers: this.getExtraReducers(),
updateQueries,
});
Expand Down Expand Up @@ -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,
);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -694,21 +695,25 @@ 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({
type: 'APOLLO_SUBSCRIPTION_RESULT',
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);
}
});
}
};
Expand Down Expand Up @@ -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 );
}

Expand Down
2 changes: 1 addition & 1 deletion src/data/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7488053

Please sign in to comment.