diff --git a/dist/reachGraphQL.js b/dist/reachGraphQL.js index e8e7fc2..be1ec9e 100644 --- a/dist/reachGraphQL.js +++ b/dist/reachGraphQL.js @@ -9,7 +9,7 @@ var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); Object.defineProperty(exports, "__esModule", { - value: true + value: true }); exports.reachGraphQL = undefined; @@ -26,35 +26,33 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de */ var reachGraphQL = exports.reachGraphQL = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(path, query) { - var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; - var response; - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - _context.prev = 0; - _context.next = 3; - return (0, _transport.transport)(path, query, queryParams); - - case 3: - response = _context.sent; - return _context.abrupt('return', response); - - case 7: - _context.prev = 7; - _context.t0 = _context['catch'](0); - - console.log(_context.t0); - - case 10: - case 'end': - return _context.stop(); - } - } - }, _callee, this, [[0, 7]]); - })); - return function reachGraphQL(_x, _x2, _x3) { - return ref.apply(this, arguments); - }; + var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(path, query) { + var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _context.prev = 0; + _context.next = 3; + return (0, _transport.transport)(path, query, queryParams); + + case 3: + return _context.abrupt('return', _context.sent); + + case 6: + _context.prev = 6; + _context.t0 = _context['catch'](0); + + console.log(_context.t0); + + case 9: + case 'end': + return _context.stop(); + } + } + }, _callee, this, [[0, 6]]); + })); + return function reachGraphQL(_x, _x2, _x3) { + return ref.apply(this, arguments); + }; }(); diff --git a/dist/reachWithDispatch.js b/dist/reachWithDispatch.js index 33000ea..f3bd940 100644 --- a/dist/reachWithDispatch.js +++ b/dist/reachWithDispatch.js @@ -42,7 +42,7 @@ var reachWithDispatch = exports.reachWithDispatch = function () { case 3: response = _context.sent; - dispatch(actionCreator(response.data)); + dispatch(actionCreator(response)); _context.next = 10; break; diff --git a/dist/utils/transport.js b/dist/utils/transport.js index ea5be49..0e5cf68 100644 --- a/dist/utils/transport.js +++ b/dist/utils/transport.js @@ -4,12 +4,8 @@ var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); -var _promise = require('babel-runtime/core-js/promise'); - -var _promise2 = _interopRequireDefault(_promise); - Object.defineProperty(exports, "__esModule", { - value: true + value: true }); exports.transport = transport; @@ -28,51 +24,24 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de */ function transport(path, query) { - var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; - - return new _promise2.default(function (resolve, reject) { - return (0, _isomorphicFetch2.default)(path, { - method: 'POST', - mode: 'cors', - headers: { - 'Accept': 'application/json', - 'content-type': 'application/json' - }, - body: (0, _stringify2.default)({ - query: query, - queryParams: queryParams - }) - }).then(function (res) { - return res.json(); - }).then(function (response) { - if (response.errors) { - return error(response.errors); - } - return resolve(response.data); - }).catch(error); - }); + var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + + return (0, _isomorphicFetch2.default)(path, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'content-type': 'application/json' + }, + body: (0, _stringify2.default)({ + query: query, + queryParams: queryParams + }) + }).then(function (response) { + return response.json(); + }).then(function (responseBody) { + if (responseBody && responseBody.errors) { + throw new Error(responseBody.errors); + } + return responseBody.data; + }); } - -// Previous Version -// export function transport (path, query, queryParams = {}) { -// return fetch(path, { -// method: 'POST', -// headers: { -// 'Accept': 'application/json', -// 'content-type': 'application/json' -// }, -// body: JSON.stringify({ -// query, -// queryParams -// }) -// }) -// .then((response) => { -// return response.json(); -// }) -// .then((responseBody) => { -// if (responseBody && responseBody.errors) { -// throw new Error(responseBody.errors); -// } -// return responseBody.data; -// }); -// } diff --git a/package.json b/package.json index 9586789..b8f7dbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-reach", - "version": "0.2.3", + "version": "0.2.6", "description": "A small library for react to communicate with GraphQL", "main": "dist/index.js", "scripts": { diff --git a/src/reachGraphQL.js b/src/reachGraphQL.js index 5dd0dcb..3cc2194 100644 --- a/src/reachGraphQL.js +++ b/src/reachGraphQL.js @@ -7,11 +7,11 @@ import { transport } from './utils/transport.js'; * @param {[object]} queryParams = {} [Should contain object with different query params] * @return {[Object]} [Data that was queried or mutated] */ -export async function reachGraphQL (path, query, queryParams = {}) { - try{ - let response = await transport(path, query, queryParams); - return response; - } catch (error) { - console.log(error) + + export async function reachGraphQL (path, query, queryParams = {}) { + try { + return await transport(path, query, queryParams); + } catch (error) { + console.log(error) + } } -} diff --git a/src/reachWithDispatch.js b/src/reachWithDispatch.js index 3eacb76..aabe7f1 100644 --- a/src/reachWithDispatch.js +++ b/src/reachWithDispatch.js @@ -11,7 +11,7 @@ import { transport } from './utils/transport.js'; export async function reachWithDispatch (path, query, queryParams = {}, actionCreator) { try{ let response = await transport(path, query, queryParams); - dispatch(actionCreator(response.data)); + dispatch(actionCreator(response)); } catch (error) { console.log(error); } diff --git a/src/utils/transport.js b/src/utils/transport.js index 50b0207..71ade22 100644 --- a/src/utils/transport.js +++ b/src/utils/transport.js @@ -7,51 +7,25 @@ import fetch from 'isomorphic-fetch'; * @return {[Promise]} [Promise containing payload] */ - export function transport (path, query, queryParams = {}) { - return new Promise ((resolve, reject) => { - return fetch(path, { - method: 'POST', - mode: 'cors', - headers: { - 'Accept': 'application/json', - 'content-type': 'application/json' - }, - body: JSON.stringify({ - query, - queryParams - }) - }) - .then(res => res.json()) - .then(response => { - if(response.errors) { - return error(response.errors); - } - return resolve(response.data); - }) - .catch(error); - }); - } - -// Previous Version -// export function transport (path, query, queryParams = {}) { -// return fetch(path, { -// method: 'POST', -// headers: { -// 'Accept': 'application/json', -// 'content-type': 'application/json' -// }, -// body: JSON.stringify({ -// query, -// queryParams -// }) -// }) -// .then((response) => { -// return response.json(); -// }) -// .then((responseBody) => { -// if (responseBody && responseBody.errors) { -// throw new Error(responseBody.errors); -// } -// return responseBody.data; -// }); -// } +export function transport (path, query, queryParams = {}) { + return fetch(path, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'content-type': 'application/json' + }, + body: JSON.stringify({ + query, + queryParams + }) + }) + .then((response) => { + return response.json(); + }) + .then((responseBody) => { + if (responseBody && responseBody.errors) { + throw new Error(responseBody.errors); + } + return responseBody.data; + }); +}