Skip to content

Commit

Permalink
feat: create context extender api
Browse files Browse the repository at this point in the history
Allow the graphql context to be extended by a user.
The implementor can use this api to add required
stuff to the context available to all resolvers.

Co-Authored-By: Jonas Holland <mail@jhio.de>
  • Loading branch information
2 people authored and herschel666 committed Nov 25, 2019
1 parent dc39b9e commit 47d8841
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/apollo-mock-server/context-extender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = context => context;
4 changes: 3 additions & 1 deletion packages/apollo-mock-server/lib/mock-server-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import express from 'express';
import hopsConfig from 'hops-config';
// eslint-disable-next-line node/no-extraneous-import
import schema from 'hops-apollo-mock-server/schema';
// eslint-disable-next-line node/no-extraneous-import
import extendContext from 'hops-apollo-mock-server/context-extender';

const apolloAppPromise = Promise.resolve(
typeof schema === 'function' ? schema() : schema
Expand All @@ -18,7 +20,7 @@ const apolloAppPromise = Promise.resolve(
'request.credentials': 'same-origin',
},
},
context: context => ({ ...context, config: hopsConfig }),
context: context => extendContext({ ...context, config: hopsConfig }),
});

server.applyMiddleware({
Expand Down
6 changes: 6 additions & 0 deletions packages/apollo-mock-server/mixin.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class GraphQLMixin extends Mixin {
] = require.resolve(this.config.graphqlMockSchemaFile);
}

if (exists(this.config.graphqlMockContextExtenderFile)) {
webpackConfig.resolve.alias[
'hops-apollo-mock-server/context-extender'
] = require.resolve(this.config.graphqlMockContextExtenderFile);
}

if (target === 'graphql-mock-server') {
webpackConfig.externals.push(
'apollo-server-express',
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-mock-server/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ module.exports = {
graphqlMockServerPath: {
type: 'string',
},
graphqlMockContextExtenderFile: {
type: 'string',
},
},
};

0 comments on commit 47d8841

Please sign in to comment.