127 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
const { ApolloServer } = require('apollo-server-koa');
const depthLimit = require('graphql-depth-limit');
2019-04-09 21:51:28 +02:00
const loadConfigs = require('./load-config');
module.exports = strapi => {
2019-04-09 21:51:28 +02:00
const { appPath, installedPlugins } = strapi.config;
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.
2019-04-09 21:51:28 +02:00
2019-06-10 20:37:13 +02:00
const { api, plugins, extensions } = await loadConfigs({
appPath,
installedPlugins,
});
_.merge(strapi, { api, plugins });
2018-03-31 18:55:08 +02:00
/*
* Create a merge of all the GraphQL configuration.
*/
2019-04-09 21:51:28 +02:00
const apisSchemas = Object.keys(strapi.api || {}).map(key =>
_.get(strapi.api[key], 'config.schema.graphql', {})
);
const pluginsSchemas = Object.keys(strapi.plugins || {}).map(key =>
_.get(strapi.plugins[key], 'config.schema.graphql', {})
);
2019-06-10 20:37:13 +02:00
const extensionsSchemas = Object.keys(extensions || {}).map(key =>
_.get(extensions[key], 'config.schema.graphql', {})
);
2019-04-09 21:51:28 +02:00
// save the final schema in the plugin's config
_.set(
strapi,
['plugins', 'graphql', 'config', '_schema', 'graphql'],
2019-06-10 20:37:13 +02:00
mergeSchemas([...apisSchemas, ...pluginsSchemas, ...extensionsSchemas])
2019-04-09 21:51:28 +02:00
);
},
initialize: function(cb) {
2019-04-09 21:51:28 +02:00
const {
typeDefs,
resolvers,
} = strapi.plugins.graphql.services.schema.generateSchema();
if (_.isEmpty(typeDefs)) {
2019-04-09 21:51:28 +02:00
strapi.log.warn(
'The GraphQL schema has not been generated because it is empty'
);
return cb();
}
const serverParams = {
typeDefs,
resolvers,
context: ({ ctx }) => {
// Initiliase loaders for this request.
2019-04-09 21:51:28 +02:00
// TODO: set loaders in the context not globally
strapi.plugins.graphql.services.loaders.initializeLoader();
return {
context: ctx,
};
},
validationRules: [depthLimit(strapi.plugins.graphql.config.depthLimit)],
tracing: _.get(strapi.plugins.graphql, 'config.tracing', false),
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,
};
2019-01-23 11:14:00 +01:00
serverParams.introspection = true;
}
const server = new ApolloServer(serverParams);
server.applyMiddleware({
app: strapi.app,
path: strapi.plugins.graphql.config.endpoint,
});
cb();
},
};
};
2019-04-09 21:51:28 +02:00
/**
* Merges a list of schemas
* @param {Array<Object>} schemas - The list of schemas to merge
*/
const mergeSchemas = schemas => {
return schemas.reduce((acc, el) => {
const { definition, query, mutation, type, resolver } = el;
return _.merge(acc, {
definition: `${acc.definition || ''} ${definition || ''}`,
query: `${acc.query || ''} ${query || ''}`,
mutation: `${acc.mutation || ''} ${mutation || ''}`,
type,
resolver,
});
}, {});
};