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

Commit

Permalink
feat(context): change model --> context
Browse files Browse the repository at this point in the history
- Change `model` to `context` for clarity
- Refactor for clarity
- Update tests
- Fix broken build step

re #26
  • Loading branch information
jlengstorf committed Nov 21, 2017
1 parent a3f50f2 commit b7ea9b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"prepush": "npm test",
"prebuild": "del-cli ./dist",
"build": "babel src -d dist && cpy \"**/*.graphql\" \"../dist/\" --cwd=src --parents",
"build": "babel src -d dist",
"lint": "eslint src/**/*.js",
"test": "npm run lint --silent && npm run test:unit --silent",
"test:unit": "cross-env NODE_ENV=test LOG4JS_LEVEL='OFF' jest --coverage",
Expand Down
39 changes: 23 additions & 16 deletions src/gramps.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,35 @@ const checkTypeDefs = ({ schema, typeDefs, namespace }) => {
};

/**
* Maps data sources and returns array of executable schema
* @param {Array} sources data sources to combine
* @param {Boolean} mock whether or not to mock resolvers
* @param {Object} options additional apollo options
* @return {Array} list of executable schemas
*/
const mapSourcesToExecutableSchemas = (sources, mock, options) =>
* Maps data sources and returns array of executable schema
* @param {Array} sources data sources to combine
* @param {Boolean} shouldMock whether or not to mock resolvers
* @param {Object} options additional apollo options
* @return {Array} list of executable schemas
*/
const mapSourcesToExecutableSchemas = (sources, shouldMock, options) =>
sources
.map(({ schema, typeDefs, resolvers, mocks, namespace }) => {
typeDefs = checkTypeDefs({ schema, typeDefs, namespace });
if (!typeDefs) {
const sourceTypeDefs = checkTypeDefs({ schema, typeDefs, namespace });

if (!sourceTypeDefs) {
return null;
}

const executableSchema = makeExecutableSchema({
typeDefs,
typeDefs: sourceTypeDefs,
resolvers: mapResolvers(namespace, resolvers),
...options.makeExecutableSchema,
});
if (mock) {

if (shouldMock) {
addMockFunctionsToSchema({
schema: executableSchema,
mocks,
...options.addMockFunctionsToSchema,
});
}

return executableSchema;
})
.filter(schema => schema instanceof GraphQLSchema);
Expand Down Expand Up @@ -134,12 +138,15 @@ export default function gramps(
});

const getContext = req =>
sources.reduce((models, source) => {
const model =
typeof source.model === 'function' ? source.model(req) : source.model;
sources.reduce((allContext, source) => {
const sourceContext =
typeof source.context === 'function'
? source.context(req)
: source.context;

return {
...models,
[source.namespace]: model,
...allContext,
[source.namespace]: sourceContext,
};
}, extraContext(req));

Expand Down
9 changes: 0 additions & 9 deletions src/rootSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@ const typeDefs = `
grampsVersion: String!
}
type Mutation {
# Returns a charming message from GrAMPS.
grampsPing: String!
}
schema {
query: Query
mutation: Mutation
}
`;

const resolvers = {
Query: {
grampsVersion: /* istanbul ignore next */ () => pkg.version,
},
Mutation: {
grampsPing: /* istanbul ignore next */ () => 'GET OFF MY LAWN',
},
};

export default { typeDefs, resolvers };
8 changes: 5 additions & 3 deletions test/gramps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ describe('GrAMPS', () => {
},
},
];

gramps({ dataSources });

return expect(console.warn).toBeCalled();
});

it('properly combines contexts', () => {
const dataSources = [
{ namespace: 'Foo', model: { foo: 'test' } },
{ namespace: 'Bar', model: { bar: 'test' } },
{ namespace: 'Foo', context: { foo: 'test' } },
{ namespace: 'Bar', context: { bar: 'test' } },
{
namespace: 'Baz',
typeDefs: 'type User { name: String } type Query { me: User }',
model: req => ({ baz: 'test' }),
context: req => ({ baz: 'test' }),
stitching: {
linkTypeDefs: 'extend type User { age: Int }',
resolvers: mergeInfo => ({
Expand Down

0 comments on commit b7ea9b5

Please sign in to comment.