Merge pull request #33 from wistityhq/fix/graphql

Wait GraphQL schemas generation before load koa-graphql middleware
This commit is contained in:
Loïc Saint-Roch 2015-11-16 11:32:42 +01:00
commit afe1c9ae86
3 changed files with 25 additions and 8 deletions

View File

@ -112,10 +112,13 @@ module.exports = function (strapi) {
// Define GraphQL route with modified Waterline models to GraphQL schema
// or disable the global variable
if (strapi.config.graphql.enabled === true) {
strapi.router.get(strapi.config.graphql.route, strapi.middlewares.graphql({
schema: strapi.schemas,
pretty: true
}));
// Wait GraphQL schemas generation
strapi.once('waterline:graphql:ready', function () {
strapi.router.get(strapi.config.graphql.route, strapi.middlewares.graphql({
schema: strapi.schemas,
pretty: true
}));
});
} else {
global.graphql = undefined;
}

View File

@ -194,9 +194,13 @@ module.exports = function (strapi) {
});
// Expose the GraphQL schemas at `strapi.schemas`
strapi.schemas = WaterlineGraphQL.getGraphQLSchema({
WaterlineGraphQL.getGraphQLSchema({
collections: strapi.orm.collections,
usefulFunctions: true
}, function (schemas) {
strapi.schemas = schemas;
strapi.emit('waterline:graphql:ready');
});
}

View File

@ -131,9 +131,19 @@ module.exports = function (strapi) {
loadHook('studio', cb);
},
// Load the router hook.
router: function loadRouterHook(cb) {
if (!hooks.router) {
return cb();
}
prepareHook('router');
applyDefaults(hooks.router);
loadHook('router', cb);
},
// Prepare all other hooks.
prepare: function prepareHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
prepareHook(id);
process.nextTick(cb);
}, cb);
@ -141,7 +151,7 @@ module.exports = function (strapi) {
// Apply the default config for all other hooks.
defaults: function defaultConfigHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
const hook = hooks[id];
applyDefaults(hook);
process.nextTick(cb);
@ -150,7 +160,7 @@ module.exports = function (strapi) {
// Load all other hooks.
load: function loadOtherHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
loadHook(id, cb);
}, cb);
}