diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f5e1fa170c..181ece9df67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change log ### vNEXT +* Add argument validation in `addMockFunctionsToSchema` for 'schema' property in parameter object [PR #321](https://github.com/apollographql/graphql-tools/pull/321) ### v0.11.0 * Remove dependency on `graphql-subscription` and use an interface for PubSub [PR #295](https://github.com/apollographql/graphql-tools/pull/295) diff --git a/src/mock.ts b/src/mock.ts index 4f9a25d6399..27a512e6e77 100644 --- a/src/mock.ts +++ b/src/mock.ts @@ -43,9 +43,11 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals } if (!schema) { - // XXX should we check that schema is an instance of GraphQLSchema? throw new Error('Must provide schema to mock'); } + if (!(schema instanceof GraphQLSchema)) { + throw new Error('Value at "schema" must be of type GraphQLSchema'); + } if (!isObject(mocks)) { throw new Error('mocks must be of type Object'); } diff --git a/src/test/testMocking.ts b/src/test/testMocking.ts index 21189ad9c49..b8cdefd9401 100644 --- a/src/test/testMocking.ts +++ b/src/test/testMocking.ts @@ -90,6 +90,11 @@ describe('Mock', () => { .to.throw('Must provide schema to mock'); }); + it('throws an error if the property "schema" on the first argument is not of type GraphQLSchema', () => { + expect(() => ( addMockFunctionsToSchema)({ schema: {}})) + .to.throw('Value at "schema" must be of type GraphQLSchema'); + }); + it('throws an error if second argument is not a Map', () => { const jsSchema = buildSchemaFromTypeDefinitions(shorthand); expect(() => ( addMockFunctionsToSchema)({ schema: jsSchema, mocks: ['a'] }))