mirror of
https://github.com/wassname/talk.git
synced 2026-08-13 12:40:11 +08:00
Added docs and added new context based plugins.
This commit is contained in:
@@ -1,6 +1,32 @@
|
||||
const loaders = require('./loaders');
|
||||
const mutators = require('./mutators');
|
||||
|
||||
const plugins = require('../plugins');
|
||||
const debug = require('debug')('talk:graph:context');
|
||||
|
||||
/**
|
||||
* Contains the array of plugins that provide context to the server, these top
|
||||
* level functions all need the context reference.
|
||||
* @type {Array}
|
||||
*/
|
||||
const contextPlugins = plugins.get('server', 'context').map(({plugin, context}) => {
|
||||
debug(`added plugin '${plugin.name}'`);
|
||||
return {context};
|
||||
});
|
||||
|
||||
/**
|
||||
* This should itterate over the passed in plugins and load them all with the
|
||||
* current graph context.
|
||||
* @return {Object} the saturated plugins object
|
||||
*/
|
||||
const decorateContextPlugins = (context, contextPlugins) => contextPlugins.reduce((acc, plugin) => {
|
||||
Object.keys(plugin.context).forEach((service) => {
|
||||
acc[service] = plugin.context[service](context);
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
/**
|
||||
* Stores the request context.
|
||||
*/
|
||||
@@ -17,6 +43,9 @@ class Context {
|
||||
|
||||
// Create the mutators.
|
||||
this.mutators = mutators(this);
|
||||
|
||||
// Decorate the plugin context.
|
||||
this.plugins = decorateContextPlugins(this, contextPlugins);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-3
@@ -99,14 +99,31 @@ const decorateWithHooks = (schema, hooks) => forEachField(schema, (field, typeNa
|
||||
field.resolve = async (obj, args, context, info) => {
|
||||
|
||||
// Issue all pre hooks before we resolve the field.
|
||||
await Promise.all(pre.map(async (pre) => await pre(obj, args, context, info)));
|
||||
await Promise.all(pre.map((pre) => pre(obj, args, context, info)));
|
||||
|
||||
// Resolve the field.
|
||||
let result = await resolve(obj, args, context, info);
|
||||
let result = resolve(obj, args, context, info);
|
||||
|
||||
// Insure all post hooks after we've resolved the field with the result
|
||||
// passed in as the fifth argument.
|
||||
return await post.reduce(async (result, post) => await post(obj, args, context, info, result), result);
|
||||
return await post.reduce(async (result, post) => {
|
||||
|
||||
// Wait for the accumulator to resolve before we continue.
|
||||
result = await result;
|
||||
|
||||
// Check to see if this post function accepts a result, if it does, we
|
||||
// expect that it modifies the result, otherwise, just fire the post hook,
|
||||
// wait till it's done, then move onto the next hook.
|
||||
if (post.length === 5) {
|
||||
return await post(obj, args, context, info, result);
|
||||
}
|
||||
|
||||
// Wait for the post hook to finish.
|
||||
await post(obj, args, context, info);
|
||||
|
||||
// Return the result, which we already awaited for before.
|
||||
return result;
|
||||
}, result);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user