apollo-client
Version:
A simple yet functional GraphQL client.
296 lines • 11.8 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var forOwn = require('lodash.forown');
var assign = require('lodash.assign');
var getFromAST_1 = require('./queries/getFromAST');
var printer_1 = require('graphql/language/printer');
var readFromStore_1 = require('./data/readFromStore');
var diffAgainstStore_1 = require('./data/diffAgainstStore');
var queryPrinting_1 = require('./queryPrinting');
var Observable_1 = require('./util/Observable');
var ObservableQuery = (function (_super) {
__extends(ObservableQuery, _super);
function ObservableQuery() {
_super.apply(this, arguments);
}
ObservableQuery.prototype.subscribe = function (observer) {
return _super.prototype.subscribe.call(this, observer);
};
ObservableQuery.prototype.result = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var subscription = _this.subscribe({
next: function (result) {
resolve(result);
setTimeout(function () {
subscription.unsubscribe();
}, 0);
},
error: function (error) {
reject(error);
},
});
});
};
return ObservableQuery;
}(Observable_1.Observable));
exports.ObservableQuery = ObservableQuery;
var QueryManager = (function () {
function QueryManager(_a) {
var _this = this;
var networkInterface = _a.networkInterface, store = _a.store, reduxRootKey = _a.reduxRootKey, dataIdFromObject = _a.dataIdFromObject;
this.idCounter = 0;
this.networkInterface = networkInterface;
this.store = store;
this.reduxRootKey = reduxRootKey;
this.dataIdFromObject = dataIdFromObject;
this.queryListeners = {};
if (this.store['subscribe']) {
this.store['subscribe'](function () {
_this.broadcastQueries();
});
}
}
QueryManager.prototype.broadcastNewStore = function (store) {
this.broadcastQueries();
};
QueryManager.prototype.mutate = function (_a) {
var _this = this;
var mutation = _a.mutation, variables = _a.variables;
var mutationId = this.generateQueryId();
var mutationDef = getFromAST_1.getMutationDefinition(mutation);
var mutationString = printer_1.print(mutation);
var request = {
query: mutationString,
variables: variables,
};
this.store.dispatch({
type: 'MUTATION_INIT',
mutationString: mutationString,
mutation: {
id: 'ROOT_MUTATION',
typeName: 'Mutation',
selectionSet: mutationDef.selectionSet,
},
variables: variables,
mutationId: mutationId,
});
return this.networkInterface.query(request)
.then(function (result) {
_this.store.dispatch({
type: 'MUTATION_RESULT',
result: result,
mutationId: mutationId,
});
return result;
});
};
QueryManager.prototype.watchQuery = function (options) {
var _this = this;
getFromAST_1.getQueryDefinition(options.query);
return new ObservableQuery(function (observer) {
var queryId = _this.startQuery(options, function (queryStoreValue) {
if (!queryStoreValue.loading || queryStoreValue.returnPartialData) {
if (queryStoreValue.graphQLErrors) {
if (observer.next) {
observer.next({ errors: queryStoreValue.graphQLErrors });
}
}
else if (queryStoreValue.networkError) {
if (observer.error) {
observer.error(queryStoreValue.networkError);
}
else {
console.error('Unhandled network error', queryStoreValue.networkError, queryStoreValue.networkError.stack);
}
}
else {
var resultFromStore = readFromStore_1.readSelectionSetFromStore({
store: _this.getApolloState().data,
rootId: queryStoreValue.query.id,
selectionSet: queryStoreValue.query.selectionSet,
variables: queryStoreValue.variables,
returnPartialData: options.returnPartialData,
});
if (observer.next) {
observer.next({ data: resultFromStore });
}
}
}
});
return {
unsubscribe: function () {
_this.stopQuery(queryId);
},
refetch: function (variables) {
if (_this.pollingTimer) {
clearInterval(_this.pollingTimer);
}
variables = variables || options.variables;
_this.fetchQuery(queryId, assign(options, {
forceFetch: true,
variables: variables,
}));
},
stopPolling: function () {
if (_this.pollingTimer) {
clearInterval(_this.pollingTimer);
}
},
startPolling: function (pollInterval) {
_this.pollingTimer = setInterval(function () {
var pollingOptions = assign({}, options);
pollingOptions.forceFetch = true;
_this.fetchQuery(queryId, pollingOptions);
}, pollInterval);
},
};
});
};
QueryManager.prototype.query = function (options) {
if (options.returnPartialData) {
throw new Error('returnPartialData option only supported on watchQuery.');
}
return this.watchQuery(options).result();
};
QueryManager.prototype.fetchQuery = function (queryId, options) {
var _this = this;
var query = options.query, variables = options.variables, _a = options.forceFetch, forceFetch = _a === void 0 ? false : _a, _b = options.returnPartialData, returnPartialData = _b === void 0 ? false : _b;
var queryDef = getFromAST_1.getQueryDefinition(query);
var queryString = printer_1.print(query);
var querySS = {
id: 'ROOT_QUERY',
typeName: 'Query',
selectionSet: queryDef.selectionSet,
};
var minimizedQueryString = queryString;
var minimizedQuery = querySS;
var initialResult;
if (!forceFetch) {
var _c = diffAgainstStore_1.diffSelectionSetAgainstStore({
selectionSet: querySS.selectionSet,
store: this.store.getState()[this.reduxRootKey].data,
throwOnMissingField: false,
rootId: querySS.id,
variables: variables,
dataIdFromObject: this.dataIdFromObject,
}), missingSelectionSets = _c.missingSelectionSets, result = _c.result;
initialResult = result;
if (missingSelectionSets && missingSelectionSets.length) {
var diffedQueryDef = queryPrinting_1.queryDefinition({
missingSelectionSets: missingSelectionSets,
variableDefinitions: queryDef.variableDefinitions,
name: queryDef.name,
});
minimizedQuery = {
id: 'ROOT_QUERY',
typeName: 'Query',
selectionSet: diffedQueryDef.selectionSet,
};
minimizedQueryString = queryPrinting_1.printQueryFromDefinition(diffedQueryDef);
}
else {
minimizedQuery = null;
minimizedQueryString = null;
}
}
var requestId = this.generateRequestId();
this.store.dispatch({
type: 'QUERY_INIT',
queryString: queryString,
query: querySS,
minimizedQueryString: minimizedQueryString,
minimizedQuery: minimizedQuery,
variables: variables,
forceFetch: forceFetch,
returnPartialData: returnPartialData,
queryId: queryId,
requestId: requestId,
});
if (minimizedQuery) {
var request = {
query: minimizedQueryString,
variables: variables,
};
this.networkInterface.query(request)
.then(function (result) {
_this.store.dispatch({
type: 'QUERY_RESULT',
result: result,
queryId: queryId,
requestId: requestId,
});
}).catch(function (error) {
_this.store.dispatch({
type: 'QUERY_ERROR',
error: error,
queryId: queryId,
requestId: requestId,
});
});
}
if (!minimizedQuery || returnPartialData) {
this.store.dispatch({
type: 'QUERY_RESULT_CLIENT',
result: {
data: initialResult,
},
variables: variables,
query: querySS,
complete: !!minimizedQuery,
queryId: queryId,
});
}
};
QueryManager.prototype.getApolloState = function () {
return this.store.getState()[this.reduxRootKey];
};
QueryManager.prototype.startQuery = function (options, listener) {
var _this = this;
var queryId = this.generateQueryId();
this.queryListeners[queryId] = listener;
this.fetchQuery(queryId, options);
if (options.pollInterval) {
this.pollingTimer = setInterval(function () {
var pollingOptions = assign({}, options);
pollingOptions.forceFetch = true;
_this.fetchQuery(queryId, pollingOptions);
}, options.pollInterval);
}
return queryId;
};
QueryManager.prototype.stopQuery = function (queryId) {
delete this.queryListeners[queryId];
if (this.pollingTimer) {
clearInterval(this.pollingTimer);
}
this.store.dispatch({
type: 'QUERY_STOP',
queryId: queryId,
});
};
QueryManager.prototype.broadcastQueries = function () {
var queries = this.getApolloState().queries;
forOwn(this.queryListeners, function (listener, queryId) {
var queryStoreValue = queries[queryId];
listener(queryStoreValue);
});
};
QueryManager.prototype.generateQueryId = function () {
var queryId = this.idCounter.toString();
this.idCounter++;
return queryId;
};
QueryManager.prototype.generateRequestId = function () {
var requestId = this.idCounter;
this.idCounter++;
return requestId;
};
return QueryManager;
}());
exports.QueryManager = QueryManager;
//# sourceMappingURL=QueryManager.js.map