Skip to content

Commit

Permalink
Centralize playgroundVersion in ApolloServerBase and remove pass-thro…
Browse files Browse the repository at this point in the history
…ugh of gui options
  • Loading branch information
martijnwalraven committed Jun 29, 2018
1 parent 67c4812 commit 84233d2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
5 changes: 5 additions & 0 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export class ApolloServerBase {
protected subscriptionServerOptions?: SubscriptionServerOptions;
protected uploadsConfig?: FileUploadOptions;

// This specifies the version of GraphQL Playground that will be served
// from graphql-playground-html, and is passed to renderPlaygroundPage
// by the integration subclasses
protected playgroundVersion = '1.7.1';

// set by installSubscriptionHandlers.
private subscriptionServer?: SubscriptionServer;

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"apollo-upload-server": "^5.0.0",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"graphql-playground-middleware-express": "^1.7.1",
"graphql-playground-html": "^1.6.0",
"graphql-subscriptions": "^0.5.8",
"graphql-tools": "^3.0.4",
"type-is": "^1.6.16"
Expand Down
19 changes: 13 additions & 6 deletions packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as express from 'express';
import * as corsMiddleware from 'cors';
import { json, OptionsJson } from 'body-parser';
import playgroundMiddleware from 'graphql-playground-middleware-express';
import { MiddlewareOptions as PlaygroundMiddlewareOptions } from 'graphql-playground-html';
import {
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
} from '@apollographql/graphql-playground-html';
import { ApolloServerBase, formatApolloErrors } from 'apollo-server-core';
import * as accepts from 'accepts';
import * as typeis from 'type-is';
Expand All @@ -27,7 +29,7 @@ export interface ServerRegistration {
bodyParserConfig?: OptionsJson | boolean;
onHealthCheck?: (req: express.Request) => Promise<any>;
disableHealthCheck?: boolean;
gui?: boolean | PlaygroundMiddlewareOptions;
gui?: boolean;
}

const fileUploadMiddleware = (
Expand Down Expand Up @@ -154,12 +156,17 @@ export class ApolloServer extends ApolloServerBase {
) === 'text/html';

if (prefersHTML) {
const middlewareOptions = {
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: path,
subscriptionEndpoint: this.subscriptionsPath,
...(typeof gui === 'boolean' ? {} : gui),
version: this.playgroundVersion,
};
return playgroundMiddleware(middlewareOptions)(req, res, next);
res.setHeader('Content-Type', 'text/html');
const playground = renderPlaygroundPage(playgroundRenderPageOptions);
res.write(playground);
res.end();
next();
return;
}
}
return graphqlExpress(this.createGraphQLServerOptions.bind(this))(
Expand Down
11 changes: 5 additions & 6 deletions packages/apollo-server-hapi/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApolloServerBase } from 'apollo-server-core';
import { parseAll } from 'accept';
import {
renderPlaygroundPage,
MiddlewareOptions as PlaygroundMiddlewareOptions,
RenderPageOptions as PlaygroundRenderPageOptions,
} from 'graphql-playground-html';
import { processRequest as processFileUploads } from 'apollo-upload-server';

Expand Down Expand Up @@ -81,15 +81,14 @@ export class ApolloServer extends ApolloServerBase {
) === 'text/html';

if (prefersHTML) {
const middlewareOptions = {
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: path,
subscriptionEndpoint: this.subscriptionsPath,
version: '1.7.0',
...(typeof gui === 'boolean' ? {} : gui),
version: this.playgroundVersion,
};

return h
.response(renderPlaygroundPage(middlewareOptions))
.response(renderPlaygroundPage(playgroundRenderPageOptions))
.type('text/html')
.takeover();
}
Expand Down Expand Up @@ -144,7 +143,7 @@ export interface ServerRegistration {
cors?: boolean | hapi.RouteOptionsCors;
onHealthCheck?: (request: hapi.Request) => Promise<any>;
disableHealthCheck?: boolean;
gui?: boolean | PlaygroundMiddlewareOptions;
gui?: boolean;
uploads?: boolean | Record<string, any>;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { ApolloServerBase } from 'apollo-server-core';
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
import { GraphQLOptions } from 'apollo-server-core';
import {
MiddlewareOptions as PlaygroundMiddlewareOptions,
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
} from 'graphql-playground-html';

import { graphqlLambda } from './lambdaApollo';

export interface CreateHandlerOptions {
gui?: boolean | PlaygroundMiddlewareOptions;
gui?: boolean;
cors?: {
origin?: boolean | string | string[];
methods?: string | string[];
Expand Down Expand Up @@ -104,8 +103,7 @@ export class ApolloServer extends ApolloServerBase {
if (acceptHeader && acceptHeader.includes('text/html')) {
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: event.requestContext.path,
...(typeof gui === 'boolean' ? {} : gui),
version: '1.7.0',
version: this.playgroundVersion,
};

return callback(null, {
Expand Down

1 comment on commit 84233d2

@ashtonsix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.