react-transporter
Advanced tools
Comparing version 0.2.8 to 0.3.0
@@ -26,2 +26,3 @@ 'use strict'; | ||
this.network = config.network; | ||
this.initialData = config.initialData; | ||
} | ||
@@ -52,3 +53,6 @@ | ||
value: function reducer() { | ||
return _reducers2.default; | ||
var roots = this.initialData && this.initialData.roots ? this.initialData.roots : {}; | ||
var entities = this.initialData && this.initialData.entities ? this.initialData.entities : {}; | ||
return (0, _reducers2.default)(roots, entities); | ||
} | ||
@@ -55,0 +59,0 @@ }, { |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.withQuery = exports.Provider = exports.createTransporterSelector = exports.TransporterClient = exports.TransporterNetwork = undefined; | ||
exports.load = exports.Provider = exports.createTransporterSelector = exports.TransporterClient = exports.TransporterNetwork = undefined; | ||
@@ -25,5 +25,5 @@ var _Network = require('./core/Network'); | ||
var _withQuery = require('./react/withQuery'); | ||
var _load = require('./react/load'); | ||
var _withQuery2 = _interopRequireDefault(_withQuery); | ||
var _load2 = _interopRequireDefault(_load); | ||
@@ -36,2 +36,2 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.Provider = _Provider2.default; | ||
exports.withQuery = _withQuery2.default; | ||
exports.load = _load2.default; |
@@ -13,11 +13,15 @@ 'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function withQuery(requestFunc) { | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var mapStateToProps = function mapStateToProps(state, props) { | ||
var request = requestFunc(props); | ||
var mapStateToProps = function mapStateToProps(props, state) { | ||
var request = requestFunc(props, state); | ||
var requestName = request.name || (0, _utils.getRequestName)(request.schema); | ||
var requests = state.transporter.requests; | ||
return { | ||
request: request, | ||
loading: requests[requestName] ? requests[requestName].loading : undefined, | ||
@@ -28,8 +32,5 @@ error: requests[requestName] ? requests[requestName].error : undefined | ||
var mapDispatchToProps = function mapDispatchToProps(dispatch, props) { | ||
if (typeof window === 'undefined') { | ||
dispatch(requestFunc(props)); | ||
} | ||
var mapDispatchToProps = function mapDispatchToProps(dispatch) { | ||
return { | ||
dispatch: dispatch | ||
// TODO | ||
@@ -41,6 +42,17 @@ // refetch: refetchQuery => dispatch(Object.assign({}, refetchQuery, query)), | ||
var mergeProps = function mergeProps(stateProps, dispatchProps, ownProps) { | ||
var mergeProps = function mergeProps(_ref, _ref2, ownProps) { | ||
var dispatch = _ref2.dispatch, | ||
dispatchProps = _objectWithoutProperties(_ref2, ['dispatch']); | ||
var request = _ref.request, | ||
stateProps = _objectWithoutProperties(_ref, ['request']); | ||
// dispatch query on client | ||
if (typeof window !== 'undefined') { | ||
dispatch(request); | ||
} | ||
var queryProps = Object.assign({}, stateProps, dispatchProps); | ||
var tempProps = Object.assign({}, _defineProperty({}, config.name || 'query', queryProps), ownProps); | ||
return config.props ? config.props(tempProps) : tempProps; | ||
var finalProps = Object.assign({}, _defineProperty({}, config.name || 'query', queryProps), ownProps); | ||
return config.props ? config.props(finalProps) : finalProps; | ||
}; | ||
@@ -47,0 +59,0 @@ |
@@ -6,23 +6,26 @@ 'use strict'; | ||
}); | ||
exports.default = createReducer; | ||
var _redux = require('redux'); | ||
var _entitiesReducer = require('./entitiesReducer'); | ||
var _createEntitiesReducer = require('./createEntitiesReducer'); | ||
var _entitiesReducer2 = _interopRequireDefault(_entitiesReducer); | ||
var _createEntitiesReducer2 = _interopRequireDefault(_createEntitiesReducer); | ||
var _aliasesReducer = require('./aliasesReducer'); | ||
var _createRootsReducer = require('./createRootsReducer'); | ||
var _aliasesReducer2 = _interopRequireDefault(_aliasesReducer); | ||
var _createRootsReducer2 = _interopRequireDefault(_createRootsReducer); | ||
var _requestsReducer = require('./requestsReducer'); | ||
var _createRequestsReducer = require('./createRequestsReducer'); | ||
var _requestsReducer2 = _interopRequireDefault(_requestsReducer); | ||
var _createRequestsReducer2 = _interopRequireDefault(_createRequestsReducer); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = (0, _redux.combineReducers)({ | ||
entities: _entitiesReducer2.default, | ||
aliases: _aliasesReducer2.default, | ||
requests: _requestsReducer2.default | ||
}); | ||
function createReducer(roots, entities) { | ||
return (0, _redux.combineReducers)({ | ||
roots: (0, _createRootsReducer2.default)(roots), | ||
entities: (0, _createEntitiesReducer2.default)(entities), | ||
requests: (0, _createRequestsReducer2.default)() | ||
}); | ||
} |
@@ -8,52 +8,102 @@ 'use strict'; | ||
var _Response = require('./response/Response'); | ||
var _ActionCollector = require('./ActionCollector'); | ||
var _Response2 = _interopRequireDefault(_Response); | ||
var _ActionCollector2 = _interopRequireDefault(_ActionCollector); | ||
var _integrateResponse = require('./integrateResponse'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _integrateResponse2 = _interopRequireDefault(_integrateResponse); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _utils = require('../utils'); | ||
// TODO | ||
// parse a real graphql schema | ||
var parseSchema = function parseSchema(schema) { | ||
return schema; | ||
}; | ||
var getRequestName = function getRequestName(schema) { | ||
return schema; | ||
}; | ||
var getRootNames = function getRootNames(schema) { | ||
return [schema]; | ||
}; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var createOptimistcResponse = function createOptimistcResponse(rootNames) { | ||
return { | ||
roots: rootNames.map(function (rootName) { | ||
return _defineProperty({}, rootName, { | ||
linked: null | ||
}); | ||
}) | ||
}; | ||
}; | ||
function createRequest(request, fetch) { | ||
var requestName = request.name || (0, _utils.getRequestName)(request.schema); | ||
var schema = parseSchema(request.schema); | ||
var requestName = getRequestName(schema); | ||
var rootNames = getRootNames(schema); | ||
return function (dispatch) { | ||
// init request status | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_START', | ||
name: requestName | ||
}); | ||
// init request and apply optimistc response if set | ||
var optimisticActions = new _ActionCollector2.default(); | ||
// add optimistic response for mutations | ||
if (request.type === 'TRANSPORTER_MUTATION' && request.optimisticUpdater) { | ||
optimisticActions.applyResponse(createOptimistcResponse(rootNames)); | ||
optimisticActions.applyUpdater(request.optimisticUpdater); | ||
} | ||
try { | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_START', | ||
name: requestName, | ||
actions: optimisticActions.getActions() | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error.message); | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_ERROR', | ||
name: requestName, | ||
error: error | ||
}); | ||
return; | ||
} | ||
// immediately stop request on server for now | ||
// TODO | ||
// collect all requests on server, then send ONE request to server | ||
if (typeof window === 'undefined') { | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_COMPLETED', | ||
name: requestName | ||
name: requestName, | ||
actions: [] | ||
}); | ||
} | ||
// pseudo fetch data on client for now | ||
// TODO | ||
if (typeof window !== 'undefined') { | ||
// apply optimistic response on mutations | ||
if (request.type === 'TRANSPORTER_MUTATION' && request.optimisticResponse) { | ||
var optimisticResponse = request.optimisticResponse(new _Response2.default()); | ||
// dispatch query | ||
fetch(request.schema, request.variables).then(function (response) { | ||
// integrate response into store on success | ||
var actions = new _ActionCollector2.default(); | ||
if (response) actions.applyResponse(response); | ||
if (request.updater) actions.applyUpdater(request.updater, response); | ||
(0, _integrateResponse2.default)(dispatch, request.integration, optimisticResponse); | ||
} | ||
try { | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_COMPLETED', | ||
name: requestName, | ||
actions: actions.getActions() | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
// dispatch query | ||
fetch(request.schema, request.variables).then(function () { | ||
// update request status on success | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_COMPLETED', | ||
name: requestName | ||
}); | ||
// TODO | ||
// integrateResponse(dispatch, request.integration, response); | ||
dispatch({ | ||
type: 'TRANSPORTER_REQUEST_ERROR', | ||
name: requestName, | ||
error: error | ||
}); | ||
} | ||
}, function (error) { | ||
@@ -60,0 +110,0 @@ // update request status on error |
@@ -39,3 +39,3 @@ 'use strict'; | ||
var updater = new _Updater2.default(dispatch); | ||
integration(updater, { root: root, trash: trash }); | ||
integration(updater, { root: root, trash: trash }, dispatch); | ||
} | ||
@@ -42,0 +42,0 @@ } |
@@ -9,11 +9,17 @@ 'use strict'; | ||
var _utils = require('../utils'); | ||
var _hasManyEntities = require('../utils/hasManyEntities'); | ||
var _formatData = require('./formatData'); | ||
var _hasManyEntities2 = _interopRequireDefault(_hasManyEntities); | ||
var _compareValues = require('./utils/compareValues'); | ||
var _compareValues2 = _interopRequireDefault(_compareValues); | ||
var _formatData = require('./utils/formatData'); | ||
var _formatData2 = _interopRequireDefault(_formatData); | ||
var _getChildrenData = require('./getChildrenData'); | ||
var _getRelationData = require('./utils/getRelationData'); | ||
var _getChildrenData2 = _interopRequireDefault(_getChildrenData); | ||
var _getRelationData2 = _interopRequireDefault(_getRelationData); | ||
@@ -28,7 +34,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
this.hasMany = (0, _utils.hasMany)(data); | ||
this.hasManyEntities = (0, _hasManyEntities2.default)(data); | ||
this.data = this.hasMany ? data.map(function (id) { | ||
return (0, _formatData2.default)(id, state.entities); | ||
}) : (0, _formatData2.default)(data, state.entities); | ||
this.data = this.hasManyEntities ? data.map(function (typeId) { | ||
return (0, _formatData2.default)(typeId[0], typeId[1], state.entities); | ||
}).filter(function (item) { | ||
return item !== undefined; | ||
}) : (0, _formatData2.default)(data[0], data[1], state.entities); | ||
@@ -44,10 +52,10 @@ this.state = state; | ||
if (!_utils.hasMany) { | ||
if (!(0, _utils.compareValues)(this.data[attribute], operator, value)) { | ||
if (!_hasManyEntities2.default) { | ||
if (!(0, _compareValues2.default)(this.data[attribute], operator, value)) { | ||
this.data = null; | ||
} | ||
} | ||
if (_utils.hasMany) { | ||
if (_hasManyEntities2.default) { | ||
this.data = this.data.filter(function (data) { | ||
return (0, _utils.compareValues)(data[attribute], operator, value); | ||
return (0, _compareValues2.default)(data[attribute], operator, value); | ||
}); | ||
@@ -75,33 +83,24 @@ } | ||
value: function shallowJoin(name) { | ||
var _this = this; | ||
if (this.hasMany) { | ||
this.data.forEach(function (attributes, key) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
var id = [_this.data[key].__typename, _this.data[key].id]; | ||
_this.data[key][name] = (0, _getChildrenData2.default)(_this.state, id, name, null, true); | ||
}); | ||
} else { | ||
// eslint-disable-next-line no-underscore-dangle | ||
var id = [this.data.__typename, this.data.id]; | ||
this.data[name] = (0, _getChildrenData2.default)(this.state, id, name, null, true); | ||
} | ||
return this; | ||
return this.join(name, null, true); | ||
} | ||
}, { | ||
key: 'join', | ||
value: function join(name, constraints) { | ||
var _this2 = this; | ||
value: function join(name) { | ||
var _this = this; | ||
if (this.hasMany) { | ||
var constraints = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var shallow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if (this.hasManyEntities) { | ||
this.data.forEach(function (attributes, key) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
var id = [_this2.data[key].__typename, _this2.data[key].id]; | ||
_this2.data[key][name] = (0, _getChildrenData2.default)(_this2.state, id, name, constraints); | ||
if (_this.data[key]) { | ||
_this.data[key][name] = (0, _getRelationData2.default)( | ||
// eslint-disable-next-line no-underscore-dangle | ||
_this.data[key].__typename, _this.data[key].id, name, _this.state, constraints, shallow); | ||
} | ||
}); | ||
} else { | ||
} else if (this.data) { | ||
this.data[name] = (0, _getRelationData2.default)( | ||
// eslint-disable-next-line no-underscore-dangle | ||
var id = [this.data.__typename, this.data.id]; | ||
this.data[name] = (0, _getChildrenData2.default)(this.state, id, name, constraints); | ||
this.data.__typename, this.data.id, name, this.state, constraints, shallow); | ||
} | ||
@@ -108,0 +107,0 @@ |
@@ -13,2 +13,4 @@ 'use strict'; | ||
var _handleErrors = require('./utils/handleErrors'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -27,26 +29,26 @@ | ||
key: 'select', | ||
value: function select(ids) { | ||
return new _Selector2.default(this.state, ids); | ||
value: function select(type, id) { | ||
return new _Selector2.default(this.state, [type, id]); | ||
} | ||
}, { | ||
key: 'selectByAlias', | ||
value: function selectByAlias(name) { | ||
if (!this.state.aliases[name]) { | ||
throw new Error('Cannot find alias \'' + name + '\'.'); | ||
key: 'selectFromRoot', | ||
value: function selectFromRoot(name) { | ||
if (!this.state.roots[name]) { | ||
(0, _handleErrors.throwSelectRootError)(name); | ||
} | ||
var aliasIds = this.state.aliases[name]; | ||
var rootIds = this.state.roots[name]; | ||
return new _Selector2.default(this.state, aliasIds); | ||
return new _Selector2.default(this.state, rootIds); | ||
} | ||
}, { | ||
key: 'selectChildren', | ||
value: function selectChildren(id, name) { | ||
if (!this.state.entities[id[0]] || !this.state.entities[id[0]][id[1]]) { | ||
throw new Error('Cannot find entity [' + id[0] + ', ' + id[1] + '].'); | ||
key: 'selectFromRelation', | ||
value: function selectFromRelation(type, id, name) { | ||
if (!this.state.entities[type] || !this.state.entities[type][id]) { | ||
(0, _handleErrors.throwSelectEntityError)(type, id); | ||
} | ||
var childrenIds = !this.state.entities[id[0]][id[1]][name] || !this.state.entities[id[0]][id[1]][name].connection ? [] : this.state.entities[id[0]][id[1]][name].connection; | ||
var childrenTypeIds = !this.state.entities[type][id][name] || !this.state.entities[type][id][name].linked ? [] : this.state.entities[type][id][name].linked; | ||
return new _Selector2.default(this.state, childrenIds); | ||
return new _Selector2.default(this.state, childrenTypeIds); | ||
} | ||
@@ -53,0 +55,0 @@ }]); |
{ | ||
"name": "react-transporter", | ||
"version": "0.2.8", | ||
"version": "0.3.0", | ||
"main": "./lib/index.js", | ||
@@ -25,5 +25,5 @@ "description": "React.js Redux GraphQL client", | ||
"eslint": "4.9.0", | ||
"react": "15.6.1", | ||
"react-dom": "15.6.1", | ||
"react-kickstarter": "0.2.23", | ||
"react": "16.1.0", | ||
"react-dom": "16.1.0", | ||
"react-kickstarter": "0.3.0", | ||
"react-redux": "5.0.6", | ||
@@ -30,0 +30,0 @@ "redux": "3.7.2", |
Sorry, the diff of this file is not supported yet
308957
71
2271