apollo-client
Version:
A simple yet functional GraphQL client.
112 lines • 4.66 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var apollo_utilities_1 = require("apollo-utilities");
var ts_invariant_1 = require("ts-invariant");
var networkStatus_1 = require("../core/networkStatus");
var arrays_1 = require("../util/arrays");
var QueryStore = (function () {
function QueryStore() {
this.store = {};
}
QueryStore.prototype.getStore = function () {
return this.store;
};
QueryStore.prototype.get = function (queryId) {
return this.store[queryId];
};
QueryStore.prototype.initQuery = function (query) {
var previousQuery = this.store[query.queryId];
ts_invariant_1.invariant(!previousQuery ||
previousQuery.document === query.document ||
apollo_utilities_1.isEqual(previousQuery.document, query.document), 'Internal Error: may not update existing query string in store');
var isSetVariables = false;
var previousVariables = null;
if (query.storePreviousVariables &&
previousQuery &&
previousQuery.networkStatus !== networkStatus_1.NetworkStatus.loading) {
if (!apollo_utilities_1.isEqual(previousQuery.variables, query.variables)) {
isSetVariables = true;
previousVariables = previousQuery.variables;
}
}
var networkStatus;
if (isSetVariables) {
networkStatus = networkStatus_1.NetworkStatus.setVariables;
}
else if (query.isPoll) {
networkStatus = networkStatus_1.NetworkStatus.poll;
}
else if (query.isRefetch) {
networkStatus = networkStatus_1.NetworkStatus.refetch;
}
else {
networkStatus = networkStatus_1.NetworkStatus.loading;
}
var graphQLErrors = [];
if (previousQuery && previousQuery.graphQLErrors) {
graphQLErrors = previousQuery.graphQLErrors;
}
this.store[query.queryId] = {
document: query.document,
variables: query.variables,
previousVariables: previousVariables,
networkError: null,
graphQLErrors: graphQLErrors,
networkStatus: networkStatus,
metadata: query.metadata,
};
if (typeof query.fetchMoreForQueryId === 'string' &&
this.store[query.fetchMoreForQueryId]) {
this.store[query.fetchMoreForQueryId].networkStatus =
networkStatus_1.NetworkStatus.fetchMore;
}
};
QueryStore.prototype.markQueryResult = function (queryId, result, fetchMoreForQueryId) {
if (!this.store || !this.store[queryId])
return;
this.store[queryId].networkError = null;
this.store[queryId].graphQLErrors = arrays_1.isNonEmptyArray(result.errors) ? result.errors : [];
this.store[queryId].previousVariables = null;
this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.ready;
if (typeof fetchMoreForQueryId === 'string' &&
this.store[fetchMoreForQueryId]) {
this.store[fetchMoreForQueryId].networkStatus = networkStatus_1.NetworkStatus.ready;
}
};
QueryStore.prototype.markQueryError = function (queryId, error, fetchMoreForQueryId) {
if (!this.store || !this.store[queryId])
return;
this.store[queryId].networkError = error;
this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.error;
if (typeof fetchMoreForQueryId === 'string') {
this.markQueryResultClient(fetchMoreForQueryId, true);
}
};
QueryStore.prototype.markQueryResultClient = function (queryId, complete) {
var storeValue = this.store && this.store[queryId];
if (storeValue) {
storeValue.networkError = null;
storeValue.previousVariables = null;
if (complete) {
storeValue.networkStatus = networkStatus_1.NetworkStatus.ready;
}
}
};
QueryStore.prototype.stopQuery = function (queryId) {
delete this.store[queryId];
};
QueryStore.prototype.reset = function (observableQueryIds) {
var _this = this;
Object.keys(this.store).forEach(function (queryId) {
if (observableQueryIds.indexOf(queryId) < 0) {
_this.stopQuery(queryId);
}
else {
_this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.loading;
}
});
};
return QueryStore;
}());
exports.QueryStore = QueryStore;
//# sourceMappingURL=queries.js.map
;