From 2f5ced22a29c665753aa9c370c802248ef472ce1 Mon Sep 17 00:00:00 2001 From: Rainer Dreyer Date: Wed, 25 May 2016 23:23:49 -0500 Subject: [PATCH] Allows resolvers to be used with mocks Previously mocks would overwrite any resolvers. Includes test. --- src/apolloServer.js | 1 + test/testApolloServer.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/apolloServer.js b/src/apolloServer.js index f3b068b5730..ec7055b3bc3 100644 --- a/src/apolloServer.js +++ b/src/apolloServer.js @@ -87,6 +87,7 @@ export default function apolloServer(options, ...rest) { addMockFunctionsToSchema({ schema: executableSchema, mocks: myMocks, + preserveResolvers: true, }); } else { // this is just basics, makeExecutableSchema should catch the rest diff --git a/test/testApolloServer.js b/test/testApolloServer.js index 70b20d42422..054efb486d3 100644 --- a/test/testApolloServer.js +++ b/test/testApolloServer.js @@ -209,6 +209,40 @@ describe('ApolloServer', () => { return expect(res.body.data).to.deep.equal(expected); }); }); + it('can mock a schema and use partially implemented resolvers', () => { + const app = express(); + const mockServer = apolloServer({ + schema: ` + type Query { + mocked: String + resolved(name: String): String + } + schema { + query: Query + } + `, + mocks: { + String: () => 'Mocked fallback', + }, + resolvers: { + Query: { + resolved(root, { name }) { + return `Hello, ${name}!`; + }, + }, + }, + }); + app.use('/graphql', mockServer); + const expected = { + mocked: 'Mocked fallback', + resolved: 'Hello, world!', + }; + return request(app).get( + '/graphql?query={mocked resolved(name: "world")}' + ).then((res) => { + return expect(res.body.data).to.deep.equal(expected); + }); + }); it('can mock a schema with unions', () => { const app = express(); const schema = `