2018-03-27 19:02:04 +02:00

38 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* Module dependencies
*/
// Public node modules.
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
module.exports = strapi => {
return {
beforeInitialize: function() {
// Try to inject this middleware just after the parser to skip the router processing.
const index = strapi.config.middleware.load.after.indexOf('parser');
if (index !== -1) {
strapi.config.middleware.load.after.splice(index + 1, 0, 'graphql');
} else {
strapi.config.middleware.load.after.push('graphql');
}
},
initialize: function(cb) {
const router = strapi.koaMiddlewares.routerJoi();
const schema = strapi.plugins.graphql.services.graphql.generateSchema();
router.post(strapi.plugins.graphql.config.endpoint, graphqlKoa({ schema }));
router.get(strapi.plugins.graphql.config.endpoint, graphqlKoa({ schema }));
router.get('/graphiql', graphiqlKoa({ endpointURL: strapi.plugins.graphql.config.endpoint }));
strapi.app.use(router.middleware());
cb();
}
};
};