Skip to content

Commit

Permalink
Fix Google Cloud Functions tests (#1616)
Browse files Browse the repository at this point in the history
Closes #1601.
  • Loading branch information
reaktivo authored and martijnwalraven committed Sep 5, 2018
1 parent b80a8f0 commit 1348933
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All of the packages in the `apollo-server` repo are released with the same versi
- Update `graphql-playground-html` to 1.7.4 [#1586](https://github.com/apollographql/apollo-server/pull/1586)
- Add support for `graphql-js` v14 by augmenting typeDefs with the `@cacheControl` directive so SDL validation doesn't fail [#1595](https://github.com/apollographql/apollo-server/pull/1595)
- Add `node-fetch` extensions typing to `RequestInit` [#1602](https://github.com/apollographql/apollo-server/pull/1602)
- Fix `apollo-server-cloud-functions` tests [#1611](https://github.com/apollographql/apollo-server/pull/1611/)

### v2.0.5

Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-cloud-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"dependencies": {
"@apollographql/graphql-playground-html": "^1.6.0",
"apollo-server-core": "2.0.0",
"apollo-server-env": "2.0.0",
"apollo-server-core": "file:../apollo-server-core",
"apollo-server-env": "file:../apollo-server-env",
"graphql-tools": "^3.0.4"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion packages/apollo-server-cloud-function/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export class ApolloServer extends ApolloServerBase {
}

return (req: Request, res: Response) => {
// Handle both the root of the GCF endpoint and /graphql
if (!['', '/', '/graphql'].includes(req.path)) {
res.status(404).end();
return;
}

if (cors) {
if (typeof cors.origin === 'string') {
res.set('Access-Control-Allow-Origin', cors.origin);
Expand All @@ -108,7 +114,8 @@ export class ApolloServer extends ApolloServerBase {
}

if (this.playgroundOptions && req.method === 'GET') {
if (req.accepts('text/html')) {
const acceptHeader = req.headers['accept'] as string;
if (acceptHeader && acceptHeader.includes('text/html')) {
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: req.get('referer'),
...this.playgroundOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ApolloServer } from '../ApolloServer';
import testSuite, {
schema as Schema,
CreateAppOptions,
} from 'apollo-server-integration-testsuite';
import { Config } from 'apollo-server-core';
import express = require('express');
import bodyParser = require('body-parser');

const createCloudFunction = (options: CreateAppOptions = {}) => {
const handler = new ApolloServer(
(options.graphqlOptions as Config) || { schema: Schema },
).createHandler();

// We use Express to simulate the Google Cloud
// Function like environment
const app = express();
app.use(bodyParser.json());
app.use(handler);
return app;
};

describe('integration:CloudFunction', () => {
testSuite(createCloudFunction);
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ export function graphqlCloudFunction(options: GraphQLOptions): any {
}

const graphqlHandler: any = (req: Request, res: Response): void => {
if (req.method === 'POST' && !req.body) {
const hasPostBody = req.body && Object.keys(req.body).length > 0;
if (req.method === 'POST' && !hasPostBody) {
res.status(500).send('POST body missing.');
return;
}

runHttpQuery([req, res], {
method: req.method,
options: options,
query: req.method === 'POST' ? req.body : (req.query as any),
query: hasPostBody ? req.body : (req.query as any),
request: {
url: req.url,
method: req.method,
Expand All @@ -40,14 +41,10 @@ export function graphqlCloudFunction(options: GraphQLOptions): any {
.send(graphqlResponse);
},
(error: HttpQueryError) => {
console.log('Error!');
console.log(JSON.stringify(error));
if ('HttpQueryError' !== error.name) {
res.status(500).send(error);
return;
}
console.log('other error');
console.log(JSON.stringify(error));
res
.status(error.statusCode)
.set(error.headers)
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-server-cloud-function/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"lib": ["es2017", "esnext.asynciterable", "dom"]
"outDir": "./dist"
},
"exclude": ["node_modules", "dist"]
"include": ["src/**/*"],
"exclude": ["**/__tests__", "**/__mocks__"]
}

0 comments on commit 1348933

Please sign in to comment.