From cc300b1cf6444f705bbef5850e5e88c090fdadda Mon Sep 17 00:00:00 2001 From: Yipeng Zhao Date: Fri, 10 Aug 2018 15:07:33 +0800 Subject: [PATCH 1/3] use playground default settings when possible Notes: Playground can either use external settings or its default settings (browser settings), in incompatible ways. The original implementation of default settings here would always provide 'settings' for Playground, in turn make its own settings mechanism disabled, with consequences such as altering settings in-browser cannot work as expect. Besides, once the default settings here go different with Playground provided, it would cause other problems. The intention here is to use Playground setting as default, while explicit passing Playground settings in Apollo server would still work with defaults defined here merged. --- packages/apollo-server-core/src/playground.ts | 14 ++++-- .../src/__tests__/ApolloServer.test.ts | 44 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/apollo-server-core/src/playground.ts b/packages/apollo-server-core/src/playground.ts index aabf52c8dc1..74c4e955e11 100644 --- a/packages/apollo-server-core/src/playground.ts +++ b/packages/apollo-server-core/src/playground.ts @@ -48,13 +48,19 @@ export function createPlaygroundOptions( const playgroundOverrides = typeof playground === 'boolean' ? {} : playground || {}; + const settingsOverrides = playgroundOverrides.hasOwnProperty('settings') + ? { + settings: { + ...defaultPlaygroundOptions.settings, + ...playgroundOverrides.settings, + }, + } + : { settings: undefined }; + const playgroundOptions: PlaygroundRenderPageOptions = { ...defaultPlaygroundOptions, ...playgroundOverrides, - settings: { - ...defaultPlaygroundOptions.settings, - ...playgroundOverrides.settings, - }, + ...settingsOverrides, }; return playgroundOptions; diff --git a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts index 97eec8fd8bb..6be6f15749e 100644 --- a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts +++ b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts @@ -179,6 +179,50 @@ describe('apollo-server-express', () => { }); }); + it('accepts GraphQL Playground Original Settings if we do not pass any settings', async () => { + const nodeEnv = process.env.NODE_ENV; + delete process.env.NODE_ENV; + + const defaultQuery = 'query { foo { bar } }'; + const endpoint = '/fumanchupacabra'; + const { url } = await createServer( + { + typeDefs, + resolvers, + }, + {}, + ); + + return new Promise((resolve, reject) => { + request( + { + url, + method: 'GET', + headers: { + accept: + 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', + Folo: 'bar', + }, + }, + (error, response, body) => { + process.env.NODE_ENV = nodeEnv; + if (error) { + reject(error); + } else { + console.log('body', body); + expect(body).to.contain('GraphQLPlayground'); + // editor.cursorShape is a setting currently not be setted in ApolloServer, but in GraphQL Playground + expect(body).to.contain(`"editor.cursorShape": "line"`); + expect(body).to.contain(defaultQuery); + expect(body).to.contain(endpoint); + expect(response.statusCode).to.equal(200); + resolve(); + } + }, + ); + }); + }); + it('accepts partial GraphQL Playground Options', async () => { const nodeEnv = process.env.NODE_ENV; delete process.env.NODE_ENV; From a7aa413b1d712002d39c4c2128dd6b76da1d8a64 Mon Sep 17 00:00:00 2001 From: Yipeng Zhao Date: Fri, 10 Aug 2018 15:42:39 +0800 Subject: [PATCH 2/3] #1516 update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 402eb2003f4..7166dba1627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All of the packages in the `apollo-server` repo are released with the same versi - express, koa: remove next after playground [#1436](https://github.com/apollographql/apollo-server/pull/1436) - Hapi: Pass the response toolkit to the context function. [#1407](https://github.com/apollographql/apollo-server/pull/1407) - update apollo-engine-reporting-protobuf to non-beta [#1429](https://github.com/apollographql/apollo-server/pull/1429) +- playground would use its own settings as default [#1516](https://github.com/apollographql/apollo-server/pull/1516) ### rc.10 From 8ae68fb56583418ce822ae849fcda6288ada4493 Mon Sep 17 00:00:00 2001 From: Yipeng Zhao Date: Fri, 10 Aug 2018 19:53:50 +0800 Subject: [PATCH 3/3] more elegant way to test: if the responding html has no explicit settings, the loaded Playground page would directly use its default settings --- .../src/__tests__/ApolloServer.test.ts | 45 +------------------ 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts index 6be6f15749e..1c56aeed8e7 100644 --- a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts +++ b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts @@ -171,6 +171,7 @@ describe('apollo-server-express', () => { reject(error); } else { expect(body).toMatch('GraphQLPlayground'); + expect(body).not.toMatch('settings'); expect(response.statusCode).toEqual(200); resolve(); } @@ -179,50 +180,6 @@ describe('apollo-server-express', () => { }); }); - it('accepts GraphQL Playground Original Settings if we do not pass any settings', async () => { - const nodeEnv = process.env.NODE_ENV; - delete process.env.NODE_ENV; - - const defaultQuery = 'query { foo { bar } }'; - const endpoint = '/fumanchupacabra'; - const { url } = await createServer( - { - typeDefs, - resolvers, - }, - {}, - ); - - return new Promise((resolve, reject) => { - request( - { - url, - method: 'GET', - headers: { - accept: - 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', - Folo: 'bar', - }, - }, - (error, response, body) => { - process.env.NODE_ENV = nodeEnv; - if (error) { - reject(error); - } else { - console.log('body', body); - expect(body).to.contain('GraphQLPlayground'); - // editor.cursorShape is a setting currently not be setted in ApolloServer, but in GraphQL Playground - expect(body).to.contain(`"editor.cursorShape": "line"`); - expect(body).to.contain(defaultQuery); - expect(body).to.contain(endpoint); - expect(response.statusCode).to.equal(200); - resolve(); - } - }, - ); - }); - }); - it('accepts partial GraphQL Playground Options', async () => { const nodeEnv = process.env.NODE_ENV; delete process.env.NODE_ENV;