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

feat(context): change model --> context #32

Merged
merged 2 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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