Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Searches with ellipses #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ client.query(`

> We can also use fragments inside fragments as well. Lokka will resolve fragments in nested fashion.

> To deactivate fragments, simply add ```no_fragments:true``` to the vars options
### Mutations

GraphQL Swapi API, does not have mutations. If we had mutations we could invoke them like this:
Expand Down
222 changes: 111 additions & 111 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.Cache = undefined;

Expand All @@ -20,118 +20,118 @@ var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Cache = exports.Cache = function () {
function Cache() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
(0, _classCallCheck3.default)(this, Cache);

this.items = {};
this.cacheExpirationTimeout = options.cacheExpirationTimeout || 1000 * 60;
}

(0, _createClass3.default)(Cache, [{
key: "_ensureItem",
value: function _ensureItem(query, vars) {
var key = this._generateKey(query, vars);
if (!this.items[key]) {
this.items[key] = {
query: query,
vars: vars,
payload: undefined,
callbacks: []
};
}

return this.items[key];
}
}, {
key: "_generateKey",
value: function _generateKey(query, vars) {
var varsJson = (0, _stringify2.default)(vars || {});
return query + "::" + varsJson;
function Cache() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
(0, _classCallCheck3.default)(this, Cache);

this.items = {};
this.cacheExpirationTimeout = options.cacheExpirationTimeout || 1000 * 60;
}
}, {
key: "watchItem",
value: function watchItem(query, vars, watcher) {
var _this = this;

var item = this._ensureItem(query, vars);
var key = this._generateKey(query, vars);

if (item.payload !== undefined) {
watcher(null, item.payload);
}

if (item.expireHandler) {
clearTimeout(item.expireHandler);
item.expireHandler = null;
}

item.callbacks.push(watcher);

// Return a callback to stop watching
return function () {
var index = item.callbacks.indexOf(watcher);
item.callbacks.splice(index, 1);
// We don't need to keep a reference for not watching items
if (item.callbacks.length === 0) {
item.expireHandler = setTimeout(function () {
delete _this.items[key];
}, _this.cacheExpirationTimeout);

(0, _createClass3.default)(Cache, [{
key: "_ensureItem",
value: function _ensureItem(query, vars) {
var key = this._generateKey(query, vars);
if (!this.items[key]) {
this.items[key] = {
query: query,
vars: vars,
payload: undefined,
callbacks: []
};
}

return this.items[key];
}
};
}
}, {
key: "getItemPayload",
value: function getItemPayload(query, vars) {
var key = this._generateKey(query, vars);
var item = this.items[key];
if (item) {
return this._clone(item.payload);
}
}
}, {
key: "setItemPayload",
value: function setItemPayload(query, vars, payload) {
var _this2 = this;

var item = this._ensureItem(query, vars);
item.payload = this._clone(payload);
item.callbacks.forEach(function (c) {
return c(null, _this2._clone(payload));
});
}
}, {
key: "fireError",
value: function fireError(query, vars, error) {
var key = this._generateKey(query, vars);
var item = this.items[key];
if (!item) {
return;
}

item.callbacks.forEach(function (c) {
return c(error);
});
}
}, {
key: "removeItem",
value: function removeItem(query, vars) {
var key = this._generateKey(query, vars);
delete this.items[key];
}
}, {
key: "getItem",
value: function getItem(query, vars) {
var key = this._generateKey(query, vars);
return this.items[key];
}
}, {
key: "_clone",
value: function _clone(payload) {
return JSON.parse((0, _stringify2.default)(payload));
}
}]);
return Cache;
}, {
key: "_generateKey",
value: function _generateKey(query, vars) {
var varsJson = (0, _stringify2.default)(vars || {});
return query + "::" + varsJson;
}
}, {
key: "watchItem",
value: function watchItem(query, vars, watcher) {
var _this = this;

var item = this._ensureItem(query, vars);
var key = this._generateKey(query, vars);

if (item.payload !== undefined) {
watcher(null, item.payload);
}

if (item.expireHandler) {
clearTimeout(item.expireHandler);
item.expireHandler = null;
}

item.callbacks.push(watcher);

// Return a callback to stop watching
return function () {
var index = item.callbacks.indexOf(watcher);
item.callbacks.splice(index, 1);
// We don't need to keep a reference for not watching items
if (item.callbacks.length === 0) {
item.expireHandler = setTimeout(function () {
delete _this.items[key];
}, _this.cacheExpirationTimeout);
}
};
}
}, {
key: "getItemPayload",
value: function getItemPayload(query, vars) {
var key = this._generateKey(query, vars);
var item = this.items[key];
if (item) {
return this._clone(item.payload);
}
}
}, {
key: "setItemPayload",
value: function setItemPayload(query, vars, payload) {
var _this2 = this;

var item = this._ensureItem(query, vars);
item.payload = this._clone(payload);
item.callbacks.forEach(function (c) {
return c(null, _this2._clone(payload));
});
}
}, {
key: "fireError",
value: function fireError(query, vars, error) {
var key = this._generateKey(query, vars);
var item = this.items[key];
if (!item) {
return;
}

item.callbacks.forEach(function (c) {
return c(error);
});
}
}, {
key: "removeItem",
value: function removeItem(query, vars) {
var key = this._generateKey(query, vars);
delete this.items[key];
}
}, {
key: "getItem",
value: function getItem(query, vars) {
var key = this._generateKey(query, vars);
return this.items[key];
}
}, {
key: "_clone",
value: function _clone(payload) {
return JSON.parse((0, _stringify2.default)(payload));
}
}]);
return Cache;
}();

exports.default = Cache;
58 changes: 33 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var _cache = require('./cache');

var _cache2 = _interopRequireDefault(_cache);

var use_fragments=true;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Lokka = function () {
Expand Down Expand Up @@ -76,31 +78,34 @@ var Lokka = function () {
}, {
key: '_findFragments',
value: function _findFragments(queryOrFragment) {
var _this = this;

var fragmentsMap = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var matched = queryOrFragment.match(/\.\.\.[A-Za-z0-9]+/g);
if (matched) {
var fragmentNames = matched.map(function (name) {
return name.replace('...', '');
});
fragmentNames.forEach(function (name) {
var fragment = _this._fragments[name];
if (!fragment) {
throw new Error('There is no such fragment: ' + name);
}

fragmentsMap[name] = fragment;
_this._findFragments(fragment, fragmentsMap);
});
}

var fragmentsArray = (0, _keys2.default)(fragmentsMap).map(function (key) {
return fragmentsMap[key];
});

return fragmentsArray;
if(use_fragments)
{
var fragmentsMap = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var matched = queryOrFragment.match(/\.\.\.[A-Za-z0-9]+/g);
if (matched) {
var fragmentNames = matched.map(function (name) {
return name.replace('...', '');
});
fragmentNames.forEach(function (name) {
var fragment = _this._fragments[name];
if (!fragment) {
throw new Error('There is no such fragment: ' + name);
}

fragmentsMap[name] = fragment;
_this._findFragments(fragment, fragmentsMap);
});
}

var fragmentsArray = (0, _keys2.default)(fragmentsMap).map(function (key) {
return fragmentsMap[key];
});
return fragmentsArray;
}
else{
return [];
}
}
}, {
key: 'query',
Expand All @@ -109,6 +114,9 @@ var Lokka = function () {
throw new Error('query is required!');
}

if(vars && vars.no_fragments)
use_fragments=false;

// XXX: Validate query against the schema
var fragments = this._findFragments(_query);
var queryWithFragments = _query + '\n' + fragments.join('\n');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lokka",
"version": "1.7.0",
"version": "1.8.0",
"description": "Simple JavaScript client for GraphQL",
"types": "types.d.ts",
"repository": {
Expand Down Expand Up @@ -43,4 +43,4 @@
"babel-runtime": "6.x.x",
"uuid": "2.x.x"
}
}
}