require('graphql');
var { default: graphql } = require('graphql-anywhere');
var { default: gql } = require('graphql-tag');
var minimongo = require("minimongo");
var Promise = require('promise');
var _ = require('lodash');
var LocalDb = minimongo.MemoryDb;
db = new LocalDb();
// Add a collection to the database
db.addCollection("ancient");
function promiseMapper(object, root) {
const keys = Object.keys(object);
const valuesAndPromises = keys.map(name => object[name]);
console.log([object, root]);
return Promise.all(valuesAndPromises).then(
values => values.reduce((resolvedObject, value, i) => {
resolvedObject[keys[i]] = value;
return resolvedObject;
}, Object.create(null)),
);
};
function generateMongoSelector(selector) {
var newSelector = {};
for (var key in selector) {
if (key == 'or') {
} else if (key == 'and') {
}
}
return newSelector;
};
function resolver(name, root, args) {
return new Promise((resolve, reject) => {
if (name == 'insert') {
db.ancient.upsert(args || {}, (doc) => {
resolve({
id: `ancient/${doc._id}`,
doc: doc,
});
});
} else if (name == 'find') {
db.ancient.find(generateMongoSelector(args)).fetch((docs, error) => {
resolve({
count: docs.length,
docs: docs.map((doc) => ({ id: doc._id, data: doc })),
});
});
} else if (name == 'save') {
if (!root.id) {
resolve({
error: "not-founded",
});
} else {
resolve({
result: true,
});
}
} else if (name == 'docs') {
root.then((root) => {
console.warn(root[name]);
resolve(root[name] || null);
});
} else {
root.then((root) => {
resolve(root[name] || null);
});
}
});
};
function execute(query, variables, context) {
return graphql(resolver, query, null, context, variables, { resultMapper: promiseMapper });
}
exports.default = execute;
db.ancient