2018-03-27 17:15:28 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
|
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
|
|
|
|
|
|
|
|
|
|
module.exports = strapi => {
|
|
|
|
|
return {
|
|
|
|
|
beforeInitialize: 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-27 17:15:28 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function(cb) {
|
|
|
|
|
const router = strapi.koaMiddlewares.routerJoi();
|
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-27 19:02:04 +02:00
|
|
|
|
router.post(strapi.plugins.graphql.config.endpoint, graphqlKoa({ schema }));
|
|
|
|
|
router.get(strapi.plugins.graphql.config.endpoint, graphqlKoa({ schema }));
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|