Skip to content

Commit

Permalink
Updated async function implementation. Should be functioning now
Browse files Browse the repository at this point in the history
  • Loading branch information
kennetpostigo committed Jan 30, 2016
1 parent a8278a6 commit 78dff6e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 65 deletions.
27 changes: 15 additions & 12 deletions dist/reachGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reachGraphQL = reachGraphQL;
exports.reachGraphQL = undefined;

var _transport = require('./utils/transport.js');

Expand All @@ -24,12 +24,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @param {[object]} queryParams = {} [Should contain object with different query params]
* @return {[Object]} [Data that was queried or mutated]
*/
function reachGraphQL(path, query) {
var _this = this;

var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
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) {
Expand All @@ -41,19 +39,24 @@ function reachGraphQL(path, query) {

case 3:
response = _context.sent;
return _context.abrupt('return', response.data);

case 7:
_context.prev = 7;
console.log(response); //heres where its returning undefined
return _context.abrupt('return', response);

case 8:
_context.prev = 8;
_context.t0 = _context['catch'](0);

console.log(_context.t0);

case 10:
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, _this, [[0, 7]]);
}, _callee, this, [[0, 8]]);
}));
}
return function reachGraphQL(_x, _x2, _x3) {
return ref.apply(this, arguments);
};
}();
73 changes: 52 additions & 21 deletions dist/utils/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ 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;

Expand All @@ -22,25 +26,52 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @param {[Object]} queryParams = {} [Params to pass into query]
* @return {[Promise]} [Promise containing payload]
*/

function transport(path, query) {
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;
});
var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

return new _promise2.default(function (resolve, reject) {
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 (res) {
return res.json();
}).then(function (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;
// });
// }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-reach",
"version": "0.1.14",
"version": "0.2.0",
"description": "A small library for react to communicate with GraphQL",
"main": "dist/index.js",
"scripts": {
Expand Down
16 changes: 7 additions & 9 deletions src/reachGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +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 function reachGraphQL (path, query, queryParams = {}) {
return async () => {
try{
let response = await transport(path, query, queryParams);
return response.data;
} catch (error) {
console.log(error)
}
}
export async function reachGraphQL (path, query, queryParams = {}) {
try{
let response = await transport(path, query, queryParams);
return response;
} catch (error) {
console.log(error)
}
}
70 changes: 48 additions & 22 deletions src/utils/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,51 @@ import fetch from 'isomorphic-fetch';
* @param {[Object]} queryParams = {} [Params to pass into query]
* @return {[Promise]} [Promise containing payload]
*/
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 new Promise ((resolve, reject) => {
return fetch(path, {
method: 'POST',
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;
// });
// }

0 comments on commit 78dff6e

Please sign in to comment.