apollo-server/packages/apollo-server-core/src/runHttpQuery.test.ts
Steve Rice 083a1ee60e Avoid TypeError when querystring is present, but query missing (#964)
The express-graphql reference implementation [provides a check]
(2e27a73358/src/index.js (L201-L208))
to protect against cases where a GET request is made that does not
have a `query` parameter where the GraphQL query would be present.

Without this guard, graphql-js's `parse` will return `undefined`
for the `DocumentNode` since it has no document to read. Subsequently
passing this to `isQueryOperation` results in a `TypeError`, because
we are providing `undefined` where `getOperationAst` [expects to get
a DocumentNode](5fe39262a3/src/utilities/getOperationAST.js (L19)).

A new test file is added for `runHttpQuery`, as one previously did
not seem to exist.
2018-04-18 15:38:57 +03:00

53 lines
1.1 KiB
TypeScript

/* 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.');
});
});
});
});