diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e535a7c0e6..326c2313773 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 - 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 diff --git a/packages/apollo-server-cloud-function/package.json b/packages/apollo-server-cloud-function/package.json index 23bd636d330..1cc978f3bbb 100644 --- a/packages/apollo-server-cloud-function/package.json +++ b/packages/apollo-server-cloud-function/package.json @@ -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": { diff --git a/packages/apollo-server-cloud-function/src/ApolloServer.ts b/packages/apollo-server-cloud-function/src/ApolloServer.ts index 13d3a4f96a0..34f1c9916f3 100644 --- a/packages/apollo-server-cloud-function/src/ApolloServer.ts +++ b/packages/apollo-server-cloud-function/src/ApolloServer.ts @@ -83,6 +83,11 @@ export class ApolloServer extends ApolloServerBase { } return (req: Request, res: Response) => { + if (req.url !== '') { + res.status(404).end(); + return; + } + if (cors) { if (typeof cors.origin === 'string') { res.set('Access-Control-Allow-Origin', cors.origin); @@ -108,7 +113,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.includes('text/html')) { const playgroundRenderPageOptions: PlaygroundRenderPageOptions = { endpoint: req.get('referer'), ...this.playgroundOptions, diff --git a/packages/apollo-server-cloud-function/src/__tests__/googleCloudApollo.test.ts b/packages/apollo-server-cloud-function/src/__tests__/googleCloudApollo.test.ts new file mode 100644 index 00000000000..3e23915dbfa --- /dev/null +++ b/packages/apollo-server-cloud-function/src/__tests__/googleCloudApollo.test.ts @@ -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 * as express from 'express'; +import * as bodyParser from '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); +}); diff --git a/packages/apollo-server-cloud-function/src/googleCloudApollo.test.ts b/packages/apollo-server-cloud-function/src/googleCloudApollo.test.ts deleted file mode 100644 index df78d8fc2f3..00000000000 --- a/packages/apollo-server-cloud-function/src/googleCloudApollo.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Request, Response } from 'express'; -import { ApolloServer } from './ApolloServer'; -import testSuite, { - schema as Schema, - CreateAppOptions, -} from 'apollo-server-integration-testsuite'; -import { Config } from 'apollo-server-core'; -import 'mocha'; - -const createCloudFunction = (options: CreateAppOptions = {}) => { - const server = new ApolloServer( - (options.graphqlOptions as Config) || { schema: Schema }, - ); - - const handler = server.createHandler(); - - return (req: Request, res: Response) => { - // return 404 if path is /bogus-route to pass the test, lambda doesn't have paths - if (req.url.includes('/bogus-route')) { - res.statusCode = 404; - return res.end(); - } - - return handler(req, res); - }; -}; - -describe('integration:CloudFunction', () => { - testSuite(createCloudFunction); -}); diff --git a/packages/apollo-server-cloud-function/src/googleCloudApollo.ts b/packages/apollo-server-cloud-function/src/googleCloudApollo.ts index f824c961952..3cf2493333d 100644 --- a/packages/apollo-server-cloud-function/src/googleCloudApollo.ts +++ b/packages/apollo-server-cloud-function/src/googleCloudApollo.ts @@ -26,7 +26,10 @@ export function graphqlCloudFunction(options: GraphQLOptions): any { runHttpQuery([req, res], { method: req.method, options: options, - query: req.method === 'POST' ? req.body : (req.query as any), + query: + req.method === 'POST' && Object.keys(req.body).length > 0 + ? req.body + : (req.query as any), request: { url: req.url, method: req.method, @@ -40,14 +43,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) diff --git a/packages/apollo-server-cloud-function/tsconfig.json b/packages/apollo-server-cloud-function/tsconfig.json index 5ac3c46b1f6..b8b81721105 100644 --- a/packages/apollo-server-cloud-function/tsconfig.json +++ b/packages/apollo-server-cloud-function/tsconfig.json @@ -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__"] }