graphql-anywhere promises test
require('graphql');
var { default: graphql } = require('graphql-anywhere');
var { default: gql } = require('graphql-tag');
var Promise = require('promise');
var _ = require('lodash');
function objectToPromise(object) {
var keys = _.keys(object);
var array = _.map(keys, (key) => {
return object[key];
});
return Promise.all(array).then((values) => {
return _.reduce(values, (object, value, k) => {
object[keys[k]] = value;
return object;
}, Object.create(null));
});
};
function objectMapper(object, root) {
return objectToPromise(object);
};
var counter = 0;
function resolver(name, root, args, context, info) {
return new Promise((resolve) => {
setTimeout(() => {
Promise.resolve(root).then((value) => {
if (name == 'getObject') {
return resolve({});
} else if (name == 'getArray') {
return resolve([{ getString: 'abc' }]);
} else if (name == 'getString') {
return resolve('something');
}
});
}, 10);
});
};
function execute(query, variables, context) {
return graphql(
resolver, query, null, context, variables,
{
resultMapper: objectMapper,
}
);
};
await execute(gql`{
getObject {
getArray {
getString
}
}
}`).then()
no comments