diff --git a/CHANGELOG.md b/CHANGELOG.md index 9597064aaf7..7c16c299c4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All of the packages in the `apollo-server` repo are released with the same versi * `apollo-server-adonis`: The `Content-type` of an operation response will now be correctly set to `application/json`. [PR #842](https://github.com/apollographql/apollo-server/pull/842) [PR #910](https://github.com/apollographql/apollo-server/pull/910) * `apollo-server-azure-functions`: Fix non-functional Azure Functions implementation and update examples in Azure Functions' `README.md`. [PR #753](https://github.com/apollographql/apollo-server/pull/753) [Issue #684](https://github.com/apollographql/apollo-server/issues/684) +* `apollo-server-core`: Fix `TypeError` on GET requests with missing `query` parameter ### v1.3.4 diff --git a/packages/apollo-server-core/src/runHttpQuery.test.ts b/packages/apollo-server-core/src/runHttpQuery.test.ts new file mode 100644 index 00000000000..0098a6228f6 --- /dev/null +++ b/packages/apollo-server-core/src/runHttpQuery.test.ts @@ -0,0 +1,53 @@ +/* tslint:disable:no-unused-expression */ +import { expect } from 'chai'; +import { stub } from 'sinon'; +import 'mocha'; + +import { + GraphQLSchema, + GraphQLObjectType, + GraphQLString, + GraphQLInt, +} from 'graphql'; + +import { runHttpQuery, HttpQueryError } from './runHttpQuery'; + +const queryType = new GraphQLObjectType({ + name: 'QueryType', + fields: { + testString: { + type: GraphQLString, + resolve() { + return 'it works'; + }, + }, + }, +}); + +const schema = new GraphQLSchema({ + query: queryType, +}); + +describe('runHttpQuery', () => { + describe('handling a GET query', () => { + const mockQueryRequest = { + method: 'GET', + query: { + query: '{ testString }', + }, + options: { + schema, + }, + }; + + it('raises a 400 error if the query is missing', () => { + const noQueryRequest = Object.assign({}, mockQueryRequest, { + query: 'foo', + }); + return runHttpQuery([], noQueryRequest).catch(err => { + expect(err.statusCode).to.equal(400); + expect(err.message).to.equal('Must provide query string.'); + }); + }); + }); +}); diff --git a/packages/apollo-server-core/src/runHttpQuery.ts b/packages/apollo-server-core/src/runHttpQuery.ts index 73528afff90..fe94bd9cb27 100644 --- a/packages/apollo-server-core/src/runHttpQuery.ts +++ b/packages/apollo-server-core/src/runHttpQuery.ts @@ -105,6 +105,8 @@ export async function runHttpQuery( if (typeof query === 'string') { // preparse the query incase of GET so we can assert the operation. query = parse(query); + } else if (!query) { + throw new HttpQueryError(400, 'Must provide query string.'); } if (!isQueryOperation(query, requestParams.operationName)) { diff --git a/test/tests.js b/test/tests.js index 37c8ffc9e90..8f731091908 100644 --- a/test/tests.js +++ b/test/tests.js @@ -4,6 +4,7 @@ const NODE_MAJOR_REVISION = parseInt(NODE_VERSION[1]); process.env.NODE_ENV = 'test'; require('../packages/apollo-server-core/dist/runQuery.test.js'); +require('../packages/apollo-server-core/dist/runHttpQuery.test.js'); require('../packages/apollo-server-module-operation-store/dist/operationStore.test'); NODE_MAJOR_VERSION >= 7 && require('../packages/apollo-server-adonis/dist/adonisApollo.test');