apollo-mobx
Version:
HOC for querying graphql server
87 lines (86 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var apollo_client_1 = require("apollo-client");
var ApolloClient = (function (_super) {
tslib_1.__extends(ApolloClient, _super);
function ApolloClient(options) {
var _this = _super.call(this, options) || this;
_this.context = options.context;
_this.loadingComponent = options.loadingComponent;
return _this;
}
ApolloClient.prototype.mutate = function (options) {
var _this = this;
var catchCallback = options.catchCallback, finalCallback = options.finalCallback, mutation = options.mutation, optimisticResponse = options.optimisticResponse, thenCallback = options.thenCallback, updateQueries = options.updateQueries, variables = options.variables;
return new Promise(function (resolve, reject) {
if (!thenCallback && !catchCallback && !finalCallback) {
_super.prototype.mutate.call(_this, options).then(function (result) {
resolve(result);
}).catch(function (error) {
reject(error);
});
return;
}
_super.prototype.mutate.call(_this, {
mutation: mutation,
optimisticResponse: optimisticResponse,
updateQueries: updateQueries,
variables: variables,
})
.then(function (graphQLResult) {
var data = graphQLResult.data;
if (data && thenCallback) {
thenCallback(data, _this.context);
}
if (finalCallback) {
finalCallback(_this.context);
}
resolve(graphQLResult);
})
.catch(function (error) {
if (catchCallback) {
catchCallback(error, _this.context);
}
if (finalCallback) {
finalCallback(_this.context);
}
reject(error);
});
});
};
ApolloClient.prototype.query = function (options) {
var _this = this;
var query = options.query, variables = options.variables, thenCallback = options.thenCallback, catchCallback = options.catchCallback, finalCallback = options.finalCallback;
if (!thenCallback && !catchCallback && !finalCallback) {
return _super.prototype.query.call(this, options);
}
return new Promise(function (resolve, reject) {
_super.prototype.query.call(_this, {
query: query,
variables: variables
})
.then(function (graphQLResult) {
var data = graphQLResult.data;
if (data && thenCallback) {
thenCallback(data, _this.context);
}
if (finalCallback) {
finalCallback(_this.context);
}
resolve(graphQLResult);
})
.catch(function (error) {
if (catchCallback) {
catchCallback(error, _this.context);
}
if (finalCallback) {
finalCallback(_this.context);
}
reject(error);
});
});
};
return ApolloClient;
}(apollo_client_1.default));
exports.ApolloClient = ApolloClient;