Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error when graphql integer variable can't be parsed #93

Merged
merged 6 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- template files are loaded once at the start and then cached with their front-matter (see [issue 87](https://github.com/KNowledgeOnWebScale/walder/issues/87))
- error when integer in graphql query variable can't be parsed (see [issue 84](https://github.com/KNowledgeOnWebScale/walder/issues/84))

### Fixed
- html-convertor can't convert a template using a layout that in turn extends another layout
Expand Down
9 changes: 7 additions & 2 deletions lib/handlers/graphql-ld-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,14 @@ module.exports = class GraphQLLDHandler extends Handler {
let newQuery = query;
keys.forEach(key => {
let val = variables[key];

if (definedParameters[key].type === 'integer') {
val = parseInt(val);
const intVal = parseInt(val);
if (isNaN(intVal)) {
const err = new Error(`Could not parse integer "${val}" for variable $${key}`);
err.type = 'GRAPHQL-INTEGERPARSEERROR';
throw err;
}
val = intVal;
} else {
val = `"${val}"`;
}
Expand Down
21 changes: 20 additions & 1 deletion test/handlers/graphql-ld-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('chai').should();
const expect = require('chai').expect;
const GraphQLLDHandler = require('../../lib/handlers/graphql-ld-handler');

describe('GraphQLLDHandler', function () {
Expand All @@ -19,10 +20,28 @@ describe('GraphQLLDHandler', function () {
};
const expectedNewQuery = '{ id @single name @single review @single { rating(value: 1) }}';
const actualNewQuery = handler._substituteVariables(query, variables, definedParameters);

actualNewQuery.should.eql(expectedNewQuery);
});

it('should be able to throw an error when an integer can\'t be parsed', () => {
const handler = new GraphQLLDHandler();
const query = '{ id @single name @single review @single { rating(value: $value) }}';
const variables = {
"value": "b"
};
const definedParameters = {
"value": {
"required": true,
"type": "integer",
"description": "Rating of the tv shows.",
"in": "path"
}
};

expect(() => handler._substituteVariables(query, variables, definedParameters)).to.throw();
});


it('string variable in query', () => {
const handler = new GraphQLLDHandler();
const query = '{ id @single name @single review @single { rating(value: $value) }}';
Expand Down