Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo-server-express: Use req.originalUrl inside ApolloServer #2451

Merged
merged 5 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v2.5.0

- `apollo-server-express`: Fix Playground URL when Apollo Server is mounted inside of another Express app by utilizing `req.originalUrl`. [PR #2451](https://github.com/apollographql/apollo-server/pull/2451)
- New plugin package `apollo-server-plugin-response-cache` implementing a full query response cache based on `apollo-cache-control` hints. The implementation added a few hooks and context fields; see the PR for details. There is a slight change to `cacheControl` object: previously, `cacheControl.stripFormattedExtensions` defaulted to false if you did not provide a `cacheControl` option object, but defaulted to true if you provided (eg) `cacheControl: {defaultMaxAge: 10}`. Now `stripFormattedExtensions` defaults to false unless explicitly provided as `true`, or if you use the legacy boolean `cacheControl: true`. [PR #2437](https://github.com/apollographql/apollo-server/pull/2437)
- Allow `GraphQLRequestListener` callbacks in plugins to depend on `this`. [PR #2470](https://github.com/apollographql/apollo-server/pull/2470)
- Move shared TypeScript utility types `WithRequired` and `ValueOrPromise` into `apollo-server-env`. [PR #2415](https://github.com/apollographql/apollo-server/pull/2415) [PR #2417](https://github.com/apollographql/apollo-server/pull/2417)
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class ApolloServer extends ApolloServerBase {

if (prefersHTML) {
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: path,
endpoint: req.originalUrl,
subscriptionEndpoint: this.subscriptionsPath,
...this.playgroundOptions,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,57 @@ describe('apollo-server-express', () => {
});
});

it('renders GraphQL playground using request original url', async () => {
const nodeEnv = process.env.NODE_ENV;
delete process.env.NODE_ENV;
const samplePath = '/innerSamplePath';

const rewiredServer = new ApolloServer({
typeDefs,
resolvers,
});
const innerApp = express();
rewiredServer.applyMiddleware({ app: innerApp });

const outerApp = express();
outerApp.use(samplePath, innerApp);

const httpRewiredServer = await new Promise<http.Server>(resolve => {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not asking but if you're so compelled, a follow-up PR that DRYs up these tests would sure be very much welcomed.

const l = outerApp.listen({ port }, () => resolve(l));
});

const { url } = createServerInfo(rewiredServer, httpRewiredServer);

const paths = url.split('/');
const rewiredEndpoint = `${samplePath}/${paths.pop()}`;

await new Promise<http.Server>((resolve, reject) => {
request(
{
url: paths.join('/') + rewiredEndpoint,
method: 'GET',
headers: {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
},
(error, response, body) => {
process.env.NODE_ENV = nodeEnv;
if (error) {
reject(error);
} else {
expect(body).toMatch(rewiredEndpoint);
expect(body).not.toMatch('settings');
expect(response.statusCode).toEqual(200);
resolve();
}
},
);
});
await rewiredServer.stop();
await httpRewiredServer.close();
});

const playgroundPartialOptionsTest = async () => {
const defaultQuery = 'query { foo { bar } }';
const endpoint = '/fumanchupacabra';
Expand Down