Skip to content

Commit

Permalink
enable loading custom plugins (#335)
Browse files Browse the repository at this point in the history
* enable loading plugins

* update
  • Loading branch information
yingwang-us authored May 13, 2020
1 parent cd90cc9 commit 99e8a77
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
26 changes: 24 additions & 2 deletions app/apollo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,32 @@ const buildCommonApolloContext = async ({ models, req, res, connection, logger }
return context;
};

const loadCustomPlugins = () => {
if (process.env.GRAPHQL_CUSTOM_PLUGINS) {
try {
const pluginStrs = process.env.GRAPHQL_CUSTOM_PLUGINS.split(';');
return pluginStrs.map( str => {
logger.info('Loading custom plugin: ' + str);
return require(str);
});
} catch (err) {
logger.error(err, 'Error encountered when loading custom plugin.');
process.exit(1);
}
}
return [];
};

const createApolloServer = () => {
const customPlugins = loadCustomPlugins();
if (process.env.GRAPHQL_ENABLE_TRACING === 'true') {
logger.info('Adding metrics plugin: apollo-metrics');
customPlugins.push(apolloMetricsPlugin);
}
logger.info(customPlugins, 'Apollo server custom plugin are loaded.');
const server = new ApolloServer({
introspection: true,
plugins: (process.env.GRAPHQL_ENABLE_TRACING === 'true' ? [apolloMetricsPlugin] : []),
introspection: process.env.NODE_ENV !== 'production',
plugins: customPlugins,
tracing: process.env.GRAPHQL_ENABLE_TRACING === 'true',
playground: process.env.NODE_ENV !== 'production',
typeDefs,
Expand Down
2 changes: 1 addition & 1 deletion app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const channelResolvers = {
return (version.name == name);
});

if(versionNameExists && versionNameExists.length > 0) {
if(versionNameExists) {
throw `The version name ${name} already exists`;
}

Expand Down
5 changes: 2 additions & 3 deletions app/apollo/resolvers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const resourceResolvers = {
const { models, me, req_id, logger } = context;
logger.debug( {req_id, user: whoIs(me), org_id, filter, fromDate, toDate, limit, queryFields }, `${queryName} enter`);

limit = _.clamp(limit, 20, 500);
limit = _.clamp(limit, 1, 500);

await validAuth(me, org_id, ACTIONS.READ, TYPES.RESOURCE, queryName, context);

Expand All @@ -166,7 +166,7 @@ const resourceResolvers = {
const { models, me, req_id, logger } = context;
logger.debug( {req_id, user: whoIs(me), org_id, filter, limit, queryFields }, `${queryName} enter`);

limit = _.clamp(limit, 20, 500);
limit = _.clamp(limit, 1, 500);

await validAuth(me, org_id, ACTIONS.READ, TYPES.RESOURCE, queryName, context);
let searchFilter = {
Expand Down Expand Up @@ -229,7 +229,6 @@ const resourceResolvers = {
(parent, args, context) => {
const topic = getStreamingTopic(EVENTS.RESOURCE.UPDATED, args.org_id);
context.logger.debug({args, me: context.me, topic}, 'withFilter asyncIteratorFn');
// TODO: in future probably we should valid authorization here
return pubSubPlaceHolder.pubSub.asyncIterator(topic);
},
async (parent, args, context) => {
Expand Down

0 comments on commit 99e8a77

Please sign in to comment.