Skip to content

Commit

Permalink
Queries fully-functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
kennetpostigo committed Jan 30, 2016
1 parent ebb619e commit d77d3e9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 142 deletions.
62 changes: 30 additions & 32 deletions dist/reachGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
};
}();
2 changes: 1 addition & 1 deletion dist/reachWithDispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
73 changes: 21 additions & 52 deletions dist/utils/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
// });
// }
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.2.3",
"version": "0.2.6",
"description": "A small library for react to communicate with GraphQL",
"main": "dist/index.js",
"scripts": {
Expand Down
14 changes: 7 additions & 7 deletions src/reachGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion src/reachWithDispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
70 changes: 22 additions & 48 deletions src/utils/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

0 comments on commit d77d3e9

Please sign in to comment.