-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid
TypeError
when querystring is present, but query
missing
The express-graphql reference implementation [provides a check] (https://github.com/graphql/express-graphql/blob/2e27a7335875f23ae5cccc97a9d6926970fb08c3/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](https://github.com/graphql/graphql-js/blob/5fe39262a308df944a87cc85b225228e7556aaa4/src/utilities/getOperationAST.js#L19). A new test file is added for `runHttpQuery`, as one previously did not seem to exist.
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters