Add indentation groups & color escape sequences to cb & resolver logs

This commit is contained in:
Luca Hagel 2018-01-06 13:40:40 +01:00
parent 54149fb9e1
commit bdc05e1bc1
2 changed files with 37 additions and 33 deletions

View file

@ -4,7 +4,7 @@ Default list, single, and total resolvers
*/ */
import { Utils, debug } from 'meteor/vulcan:core'; import { Utils, debug, debugGroup, debugGroupEnd } from 'meteor/vulcan:core';
import { createError } from 'apollo-errors'; import { createError } from 'apollo-errors';
const defaultOptions = { const defaultOptions = {
@ -22,12 +22,12 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
name: `${collectionName}List`, name: `${collectionName}List`,
description: `A list of ${collectionName} documents matching a set of query terms`, description: `A list of ${collectionName} documents matching a set of query terms`,
async resolver(root, {terms = {}, enableCache = false}, context, { cacheControl }) {
debug(`//--------------- start ${collectionName} list resolver ---------------//`); async resolver(root, { terms = {}, enableCache = false }, context, { cacheControl }) {
debug(resolverOptions); debug('')
debug(terms); debugGroup(`--------------- start \x1b[35m${collectionName} list\x1b[0m resolver ---------------`);
debug(`Options: ${resolverOptions}`);
debug(`Terms: ${terms}`);
if (cacheControl && enableCache) { if (cacheControl && enableCache) {
const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge; const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge;
@ -41,7 +41,7 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
const collection = context[collectionName]; const collection = context[collectionName];
// get selector and options from terms and perform Mongo query // get selector and options from terms and perform Mongo query
let {selector, options} = await collection.getParameters(terms, {}, context); let { selector, options } = await collection.getParameters(terms, {}, context);
options.skip = terms.offset; options.skip = terms.offset;
debug({ selector, options }); debug({ selector, options });
@ -50,15 +50,17 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
// if collection has a checkAccess function defined, remove any documents that doesn't pass the check // if collection has a checkAccess function defined, remove any documents that doesn't pass the check
const viewableDocs = collection.checkAccess ? _.filter(docs, doc => collection.checkAccess(currentUser, doc)) : docs; const viewableDocs = collection.checkAccess ? _.filter(docs, doc => collection.checkAccess(currentUser, doc)) : docs;
// take the remaining documents and remove any fields that shouldn't be accessible // take the remaining documents and remove any fields that shouldn't be accessible
const restrictedDocs = Users.restrictViewableFields(currentUser, collection, viewableDocs); const restrictedDocs = Users.restrictViewableFields(currentUser, collection, viewableDocs);
// prime the cache // prime the cache
restrictedDocs.forEach(doc => collection.loader.prime(doc._id, doc)); restrictedDocs.forEach(doc => collection.loader.prime(doc._id, doc));
debug(`// ${restrictedDocs.length} documents returned`); debug(`\x1b[33m=> ${restrictedDocs.length} documents returned\x1b[0m`);
debug(`//--------------- end ${collectionName} list resolver ---------------//`); debugGroupEnd();
debug(`--------------- end \x1b[35m${collectionName} list\x1b[0m resolver ---------------`);
debug('')
// return results // return results
return restrictedDocs; return restrictedDocs;
@ -69,16 +71,16 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
// resolver for returning a single document queried based on id or slug // resolver for returning a single document queried based on id or slug
single: { single: {
name: `${collectionName}Single`, name: `${collectionName}Single`,
description: `A single ${collectionName} document fetched by ID or slug`, description: `A single ${collectionName} document fetched by ID or slug`,
async resolver(root, {documentId, slug, enableCache = false}, context, { cacheControl }) { async resolver(root, { documentId, slug, enableCache = false }, context, { cacheControl }) {
debug('')
debug(`//--------------- start ${collectionName} single resolver ---------------//`); debugGroup(`--------------- start \x1b[35m${collectionName} single\x1b[0m resolver ---------------`);
debug(resolverOptions); debug(`Options: ${resolverOptions}`);
debug(documentId); debug(`DocumentId: ${documentId}`);
if (cacheControl && enableCache) { if (cacheControl && enableCache) {
const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge; const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge;
@ -89,11 +91,11 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
const collection = context[collectionName]; const collection = context[collectionName];
// don't use Dataloader if doc is selected by slug // don't use Dataloader if doc is selected by slug
const doc = documentId ? await collection.loader.load(documentId) : (slug ? collection.findOne({slug}) : collection.findOne()); const doc = documentId ? await collection.loader.load(documentId) : (slug ? collection.findOne({ slug }) : collection.findOne());
if (!doc) { if (!doc) {
const MissingDocumentError = createError('app.missing_document', {message: 'app.missing_document'}); const MissingDocumentError = createError('app.missing_document', { message: 'app.missing_document' });
throw new MissingDocumentError({data: {documentId, slug}}); throw new MissingDocumentError({ data: { documentId, slug } });
} }
// if collection has a checkAccess function defined, use it to perform a check on the current document // if collection has a checkAccess function defined, use it to perform a check on the current document
@ -104,24 +106,26 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
const restrictedDoc = Users.restrictViewableFields(currentUser, collection, doc); const restrictedDoc = Users.restrictViewableFields(currentUser, collection, doc);
debug(`//--------------- end ${collectionName} single resolver ---------------//`); debugGroupEnd();
debug(`--------------- end \x1b[35m${collectionName} single\x1b[0m resolver ---------------`);
debug('')
// filter out disallowed properties and return resulting document // filter out disallowed properties and return resulting document
return restrictedDoc; return restrictedDoc;
}, },
}, },
// resolver for returning the total number of documents matching a set of query terms // resolver for returning the total number of documents matching a set of query terms
total: { total: {
name: `${collectionName}Total`, name: `${collectionName}Total`,
description: `The total count of ${collectionName} documents matching a set of query terms`, description: `The total count of ${collectionName} documents matching a set of query terms`,
async resolver(root, {terms, enableCache}, context, { cacheControl }) { async resolver(root, { terms, enableCache }, context, { cacheControl }) {
if (cacheControl && enableCache) { if (cacheControl && enableCache) {
const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge; const maxAge = resolverOptions.cacheMaxAge || defaultOptions.cacheMaxAge;
cacheControl.setCacheHint({ maxAge }); cacheControl.setCacheHint({ maxAge });
@ -129,11 +133,11 @@ export const getDefaultResolvers = (collectionName, resolverOptions = defaultOpt
const collection = context[collectionName]; const collection = context[collectionName];
const {selector} = await collection.getParameters(terms, {}, context); const { selector } = await collection.getParameters(terms, {}, context);
return collection.find(selector).count(); return collection.find(selector).count();
}, },
} }
} }

View file

@ -71,12 +71,12 @@ export const runCallbacks = function () {
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
return callbacks.reduce(function(accumulator, callback) { return callbacks.reduce(function (accumulator, callback) {
debug(`// Running callback [${callback.name}] on hook [${hook}]`); debug(`\x1b[32m>> Running callback [${callback.name}] on hook [${hook}]\x1b[0m`);
const newArguments = [accumulator].concat(args); const newArguments = [accumulator].concat(args);
try { try {
const result = callback.apply(this, newArguments); const result = callback.apply(this, newArguments);
@ -94,7 +94,7 @@ export const runCallbacks = function () {
} }
} catch (error) { } catch (error) {
console.log(`// error at callback [${callback.name}] in hook [${hook}]`); console.log(`\x1b[31m// error at callback [${callback.name}] in hook [${hook}]\x1b[0m`);
console.log(error); console.log(error);
if (error.break || error.data && error.data.break) { if (error.break || error.data && error.data.break) {
throw error; throw error;
@ -128,8 +128,8 @@ export const runCallbacksAsync = function () {
// use defer to avoid holding up client // use defer to avoid holding up client
Meteor.defer(function () { Meteor.defer(function () {
// run all post submit server callbacks on post object successively // run all post submit server callbacks on post object successively
callbacks.forEach(function(callback) { callbacks.forEach(function (callback) {
debug(`// Running async callback [${callback.name}] on hook [${hook}]`); debug(`\x1b[32m>> Running async callback [${callback.name}] on hook [${hook}]\x1b[0m`);
callback.apply(this, args); callback.apply(this, args);
}); });
}); });