Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Commit

Permalink
feat: export a prepare function (#72)
Browse files Browse the repository at this point in the history
Using `prepare` allows for building a `GraphQLOptions` object that is _not_ a callback.
  • Loading branch information
ecwyne authored and jlengstorf committed Jan 11, 2018
1 parent abf06c2 commit 3bec290
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/gramps.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const mapSourcesToExecutableSchemas = (sources, shouldMock, options) =>
* @param {Object?} config.apollo options for Apollo functions
* @return {Function} req => options for `graphqlExpress()`
*/
export default function gramps({
export function prepare({
dataSources = [],
enableMockData = process.env.GRAMPS_MODE === 'mock',
extraContext = req => ({}), // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -150,10 +150,22 @@ export default function gramps({
}, extra);
};

return req => ({
return {
schema,
context: getContext(req),
context: getContext,
addContext: (req, res, next) => {
req.gramps = getContext(req);
next();
},
// formatError: formatError(logger),
...apolloOptions.graphqlExpress,
};
}

export default function gramps(...args) {
const options = prepare(...args);
return req => ({
...options,
context: options.context(req),
});
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gramps from './gramps';

export { prepare } from './gramps';
export default gramps;
13 changes: 12 additions & 1 deletion test/gramps.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { GraphQLSchema } from 'graphql';
import * as GraphQLTools from 'graphql-tools';
import gramps from '../src';
import gramps, { prepare } from '../src';

describe('GrAMPS', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('prepare()', () => {
it('addContext() adds gramps property to request object', () => {
const { addContext } = prepare();
const next = jest.genMockFn();
const req = {};
addContext(req, {}, next);
expect(req).toEqual({ gramps: {} });
expect(next).toBeCalled();
});
});

describe('gramps()', () => {
it('creates a valid schema and empty context with no arguments', () => {
const getGrampsContext = gramps();
Expand Down

0 comments on commit 3bec290

Please sign in to comment.