UNPKG

apollo-mantra

Version:

Tools for integration of apollo with Mantra

52 lines (51 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var config_1 = require("./config"); function default_1(_a) { var context = _a.context, query = _a.query, name = _a.name, variables = _a.variables, updateQueries = _a.updateQueries, optimisticCallback = _a.optimisticCallback, thenCallback = _a.thenCallback, catchCallback = _a.catchCallback, finalCallback = _a.finalCallback, optimisticResponse = _a.optimisticResponse; var state = context ? context.state : null; if (optimisticCallback) { optimisticCallback(state, context); } var optimistic = null; if (optimisticResponse) { if (!name) { throw new Error('apollo-mantra: Please specify mutation name if you want to use optimistic callbacks!'); } optimistic = { __typename: 'Mutation' }; optimistic[name] = optimisticResponse; } return new Promise(function (resolve, reject) { config_1.default.apolloClient .mutate({ mutation: query, variables: variables, optimisticResponse: optimistic, updateQueries: updateQueries }) .then(function (graphQLResult) { var data = graphQLResult.data; if (data && thenCallback) { thenCallback(data, state, context); } if (finalCallback) { finalCallback(state, context); } resolve(graphQLResult); }) .catch(function (error) { // showMessage('Error', error.message ? (error.message + error.stack) : error); // log(error); // context.Utils.log.error(error); if (catchCallback) { catchCallback(error, state, context); } if (finalCallback) { finalCallback(state, context); } reject(error); }); }); } exports.default = default_1; ;