189 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
const path = require('path');
const glob = require('glob');
const { ApolloServer } = require('apollo-server-koa');
const depthLimit = require('graphql-depth-limit');
module.exports = strapi => {
return {
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.
if (!_.get(strapi.config.hook.load, 'after')) {
_.set(strapi.config.hook.load, 'after', []);
}
strapi.config.hook.load.after.push('graphql');
// 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/*.graphql',
{
cwd: strapi.config.appPath,
},
(err, files) => {
if (err) {
return reject(err);
}
utils.loadConfig
.call(strapi, files, true)
.then(resolve)
.catch(reject);
},
);
}),
]);
2018-03-31 18:55:08 +02:00
/*
* Create a merge of all the GraphQL configuration.
*/
// Set path with initial state.
_.set(strapi.plugins.graphql, 'config._schema.graphql', {
definition: '',
query: '',
mutation: '',
type: {},
resolver: {},
});
2018-03-31 18:55:08 +02:00
// Merge user API.
Object.keys(strapi.api || {}).reduce((acc, current) => {
const { definition, query, mutation, type, resolver } = _.get(
strapi.api[current],
'config.schema.graphql',
{},
);
2018-03-31 18:55:08 +02:00
acc.definition += definition || '';
acc.query += query || '';
acc.mutation += mutation || '';
2018-03-31 18:55:08 +02:00
return _.merge(acc, {
type,
resolver,
2018-03-31 18:55:08 +02:00
});
}, strapi.plugins.graphql.config._schema.graphql);
// Merge plugins API.
Object.keys(strapi.plugins || {}).reduce((acc, current) => {
const { definition, query, mutation, type, resolver } = _.get(
strapi.plugins[current],
'config.schema.graphql',
{},
);
2018-03-31 18:55:08 +02:00
acc.definition += definition || '';
acc.query += query || '';
acc.mutation += mutation || '';
2018-03-31 18:55:08 +02:00
return _.merge(acc, {
type,
resolver,
2018-03-31 18:55:08 +02:00
});
}, strapi.plugins.graphql.config._schema.graphql);
},
initialize: function(cb) {
const {
typeDefs,
resolvers,
} = strapi.plugins.graphql.services.schema.generateSchema();
if (_.isEmpty(typeDefs)) {
strapi.log.warn(
'GraphQL schema has not been generated because it\'s empty',
);
return cb();
}
const serverParams = {
typeDefs,
resolvers,
context: async ({ ctx }) => ({
context: ctx,
}),
validationRules: [depthLimit(strapi.plugins.graphql.config.depthLimit)],
playground: false,
};
// Disable GraphQL Playground in production environment.
if (
strapi.config.environment !== 'production' ||
strapi.plugins.graphql.config.playgroundAlways
) {
serverParams.playground = {
endpoint: strapi.plugins.graphql.config.endpoint,
};
}
const server = new ApolloServer(serverParams);
server.applyMiddleware({
app: strapi.app,
path: strapi.plugins.graphql.config.endpoint,
});
cb();
},
};
};