Skip to content

Commit

Permalink
Bundle size tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Apr 23, 2019
1 parent c292dad commit 21916e0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 88 deletions.
6 changes: 1 addition & 5 deletions packages/apollo-client/src/core/LocalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ export class LocalState<TCacheShape> {
return forceResolvers;
}

public shouldForceResolver(field: FieldNode) {
return this.shouldForceResolvers(field);
}

// Query the cache and return matching data.
private buildRootValueFromCache(
document: DocumentNode,
Expand Down Expand Up @@ -384,7 +380,7 @@ export class LocalState<TCacheShape> {
// `@client(always: true)`), then we'll skip running non-forced resolvers.
if (
!execContext.onlyRunForcedResolvers ||
this.shouldForceResolver(field)
this.shouldForceResolvers(field)
) {
const resolverType =
rootValue.__typename || execContext.defaultOperationType;
Expand Down
33 changes: 15 additions & 18 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class QueryManager<TStore> {
cancel,
}));

this.invalidate(true, fetchMoreForQueryId);
this.invalidate(fetchMoreForQueryId);

this.queryStore.initQuery({
queryId,
Expand Down Expand Up @@ -433,7 +433,8 @@ export class QueryManager<TStore> {
} else {
if (requestId >= this.getQuery(queryId).lastRequestId) {
this.queryStore.markQueryError(queryId, error, fetchMoreForQueryId);
this.invalidate(true, queryId, fetchMoreForQueryId);
this.invalidate(queryId);
this.invalidate(fetchMoreForQueryId);
this.broadcastQueries();
}
throw new ApolloError({ networkError: error });
Expand All @@ -454,7 +455,8 @@ export class QueryManager<TStore> {
// If there is no part of the query we need to fetch from the server (or,
// fetchPolicy is cache-only), we just write the store result as the final result.
this.queryStore.markQueryResultClient(queryId, !shouldFetch);
this.invalidate(true, queryId, fetchMoreForQueryId);
this.invalidate(queryId);
this.invalidate(fetchMoreForQueryId);

if (this.transform(query).hasForcedResolvers) {
return this.localState.runResolvers({
Expand Down Expand Up @@ -531,7 +533,7 @@ export class QueryManager<TStore> {
newData?: Cache.DiffResult<T>,
) => {
// we're going to take a look at the data, so the query is no longer invalidated
this.invalidate(false, queryId);
this.invalidate(queryId, false);

// The query store value can be undefined in the event of a store
// reset.
Expand Down Expand Up @@ -799,7 +801,7 @@ export class QueryManager<TStore> {
private stopQueryInStoreNoBroadcast(queryId: string) {
this.stopPollingQuery(queryId);
this.queryStore.stopQuery(queryId);
this.invalidate(true, queryId);
this.invalidate(queryId);
}

public addQueryListener(queryId: string, listener: QueryListener) {
Expand Down Expand Up @@ -908,7 +910,7 @@ export class QueryManager<TStore> {
}

this.setQuery(queryId, () => ({ newData: null }));
this.invalidate(true, queryId);
this.invalidate(queryId);
}
});

Expand Down Expand Up @@ -1075,11 +1077,8 @@ export class QueryManager<TStore> {
}

const { variables, query } = observableQuery.options;

const { data } = this.getCurrentQueryResult(observableQuery, false);

return {
previousResult: data,
previousResult: this.getCurrentQueryResult(observableQuery, false).data,
variables,
document: query,
};
Expand Down Expand Up @@ -1236,7 +1235,8 @@ export class QueryManager<TStore> {
fetchMoreForQueryId,
);

this.invalidate(true, queryId, fetchMoreForQueryId);
this.invalidate(queryId);
this.invalidate(fetchMoreForQueryId);

this.broadcastQueries();
}
Expand Down Expand Up @@ -1316,14 +1316,11 @@ export class QueryManager<TStore> {
}

private invalidate(
invalidated: boolean,
queryId?: string,
fetchMoreForQueryId?: string,
queryId: string | undefined,
invalidated = true,
) {
if (queryId) this.setQuery(queryId, () => ({ invalidated }));

if (fetchMoreForQueryId) {
this.setQuery(fetchMoreForQueryId, () => ({ invalidated }));
if (queryId) {
this.setQuery(queryId, () => ({ invalidated }));
}
}

Expand Down
18 changes: 6 additions & 12 deletions packages/apollo-client/src/data/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@ export class MutationStore {

public markMutationError(mutationId: string, error: Error) {
const mutation = this.store[mutationId];

if (!mutation) {
return;
if (mutation) {
mutation.loading = false;
mutation.error = error;
}

mutation.loading = false;
mutation.error = error;
}

public markMutationResult(mutationId: string) {
const mutation = this.store[mutationId];

if (!mutation) {
return;
if (mutation) {
mutation.loading = false;
mutation.error = null;
}

mutation.loading = false;
mutation.error = null;
}

public reset() {
Expand Down
48 changes: 18 additions & 30 deletions packages/apollo-client/src/data/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocumentNode, GraphQLError, ExecutionResult } from 'graphql';
import { isEqual } from 'apollo-utilities';
import { InvariantError } from 'ts-invariant';
import { invariant } from 'ts-invariant';
import { NetworkStatus } from '../core/networkStatus';
import { isNonEmptyArray } from '../util/arrays';

Expand Down Expand Up @@ -37,18 +37,15 @@ export class QueryStore {
}) {
const previousQuery = this.store[query.queryId];

if (
previousQuery &&
previousQuery.document !== query.document &&
!isEqual(previousQuery.document, query.document)
) {
// XXX we're throwing an error here to catch bugs where a query gets overwritten by a new one.
// we should implement a separate action for refetching so that QUERY_INIT may never overwrite
// an existing query (see also: https://github.com/apollostack/apollo-client/issues/732)
throw new InvariantError(
'Internal Error: may not update existing query string in store',
);
}
// XXX we're throwing an error here to catch bugs where a query gets overwritten by a new one.
// we should implement a separate action for refetching so that QUERY_INIT may never overwrite
// an existing query (see also: https://github.com/apollostack/apollo-client/issues/732)
invariant(
!previousQuery ||
previousQuery.document === query.document ||
isEqual(previousQuery.document, query.document),
'Internal Error: may not update existing query string in store',
);

let isSetVariables = false;

Expand Down Expand Up @@ -168,22 +165,13 @@ export class QueryStore {
}

public reset(observableQueryIds: string[]) {
// keep only the queries with query ids that are associated with observables
this.store = Object.keys(this.store)
.filter(queryId => {
return observableQueryIds.indexOf(queryId) > -1;
})
.reduce(
(res, key) => {
// XXX set loading to true so listeners don't trigger unless they want results with partial data
res[key] = {
...this.store[key],
networkStatus: NetworkStatus.loading,
};

return res;
},
{} as { [queryId: string]: QueryStoreValue },
);
Object.keys(this.store).forEach(queryId => {
if (observableQueryIds.indexOf(queryId) < 0) {
this.stopQuery(queryId);
} else {
// XXX set loading to true so listeners don't trigger unless they want results with partial data
this.store[queryId].networkStatus = NetworkStatus.loading;
}
});
}
}
44 changes: 21 additions & 23 deletions packages/apollo-client/src/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,27 @@ export class DataStore<TSerialized> {
}) {
// Incorporate the result from this mutation into the store
if (!graphQLResultHasError(mutation.result)) {
const cacheWrites: Cache.WriteOptions[] = [];
cacheWrites.push({
const cacheWrites: Cache.WriteOptions[] = [{
result: mutation.result.data,
dataId: 'ROOT_MUTATION',
query: mutation.document,
variables: mutation.variables,
});

if (mutation.updateQueries) {
Object.keys(mutation.updateQueries)
.filter(id => mutation.updateQueries[id])
.forEach(queryId => {
const { query, updater } = mutation.updateQueries[queryId];
// Read the current query result from the store.
const { result: currentQueryResult, complete } = this.cache.diff({
query: query.document,
variables: query.variables,
returnPartialData: true,
optimistic: false,
});

if (!complete) {
return;
}
}];

const { updateQueries } = mutation;
if (updateQueries) {
Object.keys(updateQueries).forEach(id => {
const { query, updater } = updateQueries[id];

// Read the current query result from the store.
const { result: currentQueryResult, complete } = this.cache.diff({
query: query.document,
variables: query.variables,
returnPartialData: true,
optimistic: false,
});

if (complete) {
// Run our reducer using the current query result and the mutation result.
const nextQueryResult = tryFunctionOrLogError(() =>
updater(currentQueryResult, {
Expand All @@ -160,7 +156,8 @@ export class DataStore<TSerialized> {
variables: query.variables,
});
}
});
}
});
}

this.cache.performTransaction(c => {
Expand All @@ -184,8 +181,9 @@ export class DataStore<TSerialized> {
mutationId: string;
optimisticResponse?: any;
}) {
if (!optimisticResponse) return;
this.cache.removeOptimistic(mutationId);
if (optimisticResponse) {
this.cache.removeOptimistic(mutationId);
}
}

public markUpdateQueryResult(
Expand Down

0 comments on commit 21916e0

Please sign in to comment.