graphql-modules
Version:
Create reusable, maintainable, testable and extendable GraphQL modules
116 lines (115 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.flatten = flatten;
exports.isDefined = isDefined;
exports.isNil = isNil;
exports.isObject = isObject;
exports.isPrimitive = isPrimitive;
exports.isAsyncIterable = isAsyncIterable;
exports.tapAsyncIterator = tapAsyncIterator;
exports.once = once;
exports.share = share;
exports.uniqueId = uniqueId;
exports.isNotSchema = isNotSchema;
exports.merge = merge;
const graphql_1 = require("graphql");
function flatten(arr) {
return Array.prototype.concat(...arr);
}
function isDefined(val) {
return !isNil(val);
}
function isNil(val) {
return val === null || typeof val === 'undefined';
}
function isObject(val) {
return Object.prototype.toString.call(val) === '[object Object]';
}
function isPrimitive(val) {
return ['number', 'string', 'boolean', 'symbol', 'bigint'].includes(typeof val);
}
function isAsyncIterable(obj) {
return obj && typeof obj[Symbol.asyncIterator] === 'function';
}
function tapAsyncIterator(iterable, doneCallback) {
const iteratorMethod = iterable[Symbol.asyncIterator];
const iterator = iteratorMethod.call(iterable);
function mapResult(result) {
if (result.done) {
doneCallback();
}
return result;
}
return {
async next() {
try {
let result = await iterator.next();
return mapResult(result);
}
catch (error) {
doneCallback();
throw error;
}
},
async return(value) {
try {
const result = await iterator.return(value);
return mapResult(result);
}
catch (error) {
doneCallback();
throw error;
}
},
throw(error) {
doneCallback();
return iterator.throw(error);
},
[Symbol.asyncIterator]() {
return this;
},
};
}
function once(cb) {
let called = false;
return () => {
if (!called) {
called = true;
cb();
}
};
}
function share(factory) {
let cached = null;
return (arg) => {
if (!cached) {
cached = factory(arg);
}
return cached;
};
}
function uniqueId(isNotUsed) {
let id;
while (!isNotUsed((id = Math.random().toString(16).substr(2)))) { }
return id;
}
function isNotSchema(obj) {
return obj instanceof graphql_1.GraphQLSchema === false;
}
function merge(source, target) {
const result = {
...source,
...target,
};
function attachSymbols(obj) {
const symbols = Object.getOwnPropertySymbols(obj);
for (const symbol of symbols) {
result[symbol] = obj[symbol];
}
}
if (source) {
attachSymbols(source);
}
attachSymbols(target);
return result;
}