From 412be208c1b60c2f77333eec2840b64990ea7935 Mon Sep 17 00:00:00 2001 From: "Jim (_nderscore)" <_@nderscore.com> Date: Tue, 14 Aug 2018 13:43:52 -0400 Subject: [PATCH] Fixed GraphQL Playground with custom configuration in production. (#1495) * Fixed support for GraphQL Playground with custom configuration in production environments. * Updated logic in `createPlaygroundOptions` to cast the `playground` option to a boolean if it is defined. Fall back to `isDev` logic only if undefined. * Updated unit test for partial graphql playground options to emulate a 'production' environment, mimicking the scenario that was broken before. * Add PR #1495 to CHANGELOG. * Restore partial playground options test when no `NODE_ENV` --- CHANGELOG.md | 1 + packages/apollo-server-core/src/playground.ts | 3 ++- .../src/__tests__/ApolloServer.test.ts | 24 +++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a96b24bea65..03b4e21d7d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All of the packages in the `apollo-server` repo are released with the same versi - 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) - Lambda: Look in event.path first when picking endpoint for GraphQL Playground [#1527](https://github.com/apollographql/apollo-server/pull/1527) +- Fix to allow enabling GraphQL Playground in production with custom config [#1495](https://github.com/apollographql/apollo-server/pull/1495) ### rc.10 diff --git a/packages/apollo-server-core/src/playground.ts b/packages/apollo-server-core/src/playground.ts index aabf52c8dc1..3187ab791c2 100644 --- a/packages/apollo-server-core/src/playground.ts +++ b/packages/apollo-server-core/src/playground.ts @@ -39,7 +39,8 @@ export function createPlaygroundOptions( playground: PlaygroundConfig = {}, ): PlaygroundRenderPageOptions | undefined { const isDev = process.env.NODE_ENV !== 'production'; - const enabled: boolean = typeof playground === 'boolean' ? playground : isDev; + const enabled: boolean = + typeof playground !== 'undefined' ? !!playground : isDev; if (!enabled) { return undefined; diff --git a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts index 97eec8fd8bb..cde6547012d 100644 --- a/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts +++ b/packages/apollo-server-express/src/__tests__/ApolloServer.test.ts @@ -179,10 +179,7 @@ describe('apollo-server-express', () => { }); }); - it('accepts partial GraphQL Playground Options', async () => { - const nodeEnv = process.env.NODE_ENV; - delete process.env.NODE_ENV; - + const playgroundPartialOptionsTest = async () => { const defaultQuery = 'query { foo { bar } }'; const endpoint = '/fumanchupacabra'; const { url } = await createServer( @@ -220,7 +217,6 @@ describe('apollo-server-express', () => { }, }, (error, response, body) => { - process.env.NODE_ENV = nodeEnv; if (error) { reject(error); } else { @@ -234,8 +230,26 @@ describe('apollo-server-express', () => { }, ); }); + }; + + it('accepts partial GraphQL Playground Options in production', async () => { + const nodeEnv = process.env.NODE_ENV; + process.env.NODE_ENV = 'production'; + await playgroundPartialOptionsTest(); + process.env.NODE_ENV = nodeEnv; }); + it( + 'accepts partial GraphQL Playground Options when an environment is ' + + 'not specified', + async () => { + const nodeEnv = process.env.NODE_ENV; + delete process.env.NODE_ENV; + await playgroundPartialOptionsTest(); + process.env.NODE_ENV = nodeEnv; + }, + ); + it('accepts playground options as a boolean', async () => { const nodeEnv = process.env.NODE_ENV; delete process.env.NODE_ENV;