2018-03-27 17:15:28 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
2018-03-29 14:03:09 +02:00
|
|
|
|
const _ = require('lodash');
|
2018-03-30 17:05:24 +02:00
|
|
|
|
const path = require('path');
|
|
|
|
|
const glob = require('glob');
|
2018-03-27 17:15:28 +02:00
|
|
|
|
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
|
|
|
|
|
|
|
|
|
|
module.exports = strapi => {
|
|
|
|
|
return {
|
2018-03-30 17:05:24 +02:00
|
|
|
|
beforeInitialize: async function() {
|
2018-03-28 20:13:09 +02:00
|
|
|
|
// Try to inject this hook just after the others hooks to skip the router processing.
|
|
|
|
|
strapi.config.hook.load.order = strapi.config.hook.load.order.concat(Object.keys(strapi.hook).filter(hook => hook !== 'graphql'));
|
|
|
|
|
strapi.config.hook.load.order.push('graphql');
|
2018-03-30 17:05:24 +02:00
|
|
|
|
|
|
|
|
|
// Load core utils.
|
|
|
|
|
const utils = require(path.resolve(strapi.config.appPath, 'node_modules', 'strapi', 'lib', 'utils'));
|
|
|
|
|
|
|
|
|
|
// Set '*.graphql' files configurations in the global variable.
|
|
|
|
|
await Promise.all([
|
|
|
|
|
// Load root configurations.
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
glob('./config/**/*.*(graphql)', {
|
|
|
|
|
cwd: strapi.config.appPath
|
|
|
|
|
}, (err, files) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return reject(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils.loadConfig.call(strapi, files, true).then(resolve).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
// Load APIs configurations.
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
glob('./api/*/config/**/*.*(graphql)', {
|
|
|
|
|
cwd: strapi.config.appPath
|
|
|
|
|
}, (err, files) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return reject(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils.loadConfig.call(strapi, files, true).then(resolve).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
// Load plugins configurations.
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
glob('./plugins/*/config/!(generated)/*.*(graphql)', {
|
|
|
|
|
cwd: strapi.config.appPath
|
|
|
|
|
}, (err, files) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return reject(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils.loadConfig.call(strapi, files, true).then(resolve).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
]);
|
2018-03-27 17:15:28 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function(cb) {
|
2018-03-27 19:02:04 +02:00
|
|
|
|
const schema = strapi.plugins.graphql.services.graphql.generateSchema();
|
2018-03-27 17:15:28 +02:00
|
|
|
|
|
2018-03-29 14:03:09 +02:00
|
|
|
|
if (_.isEmpty(schema)) {
|
|
|
|
|
strapi.log.warn(`GraphQL schema has not been generated because it's empty`);
|
|
|
|
|
|
|
|
|
|
return cb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const router = strapi.koaMiddlewares.routerJoi();
|
|
|
|
|
|
2018-03-30 17:05:24 +02:00
|
|
|
|
router.post(strapi.plugins.graphql.config.endpoint, async (ctx, next) => graphqlKoa({ schema, context: ctx })(ctx, next));
|
|
|
|
|
router.get(strapi.plugins.graphql.config.endpoint, async (ctx, next) => graphqlKoa({ schema, context: ctx })(ctx, next));
|
2018-03-27 17:15:28 +02:00
|
|
|
|
|
2018-03-27 19:02:04 +02:00
|
|
|
|
router.get('/graphiql', graphiqlKoa({ endpointURL: strapi.plugins.graphql.config.endpoint }));
|
2018-03-27 17:15:28 +02:00
|
|
|
|
|
|
|
|
|
strapi.app.use(router.middleware());
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|