Skip to content

Commit

Permalink
fix(amplify-appsync-simulator): parse AWSTimestamp literals as Ints (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig authored Apr 9, 2021
1 parent 56907eb commit 08750a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ValueNode } from 'graphql';
import { scalars } from '../../schema/appsync-scalars';

describe('AWSTimestamp parseLiteral', () => {
it('Returns literals as integers', () => {
const astNode = { kind: 'IntValue', value: '1234', loc: { start: 68, end: 74 } } as ValueNode;
expect(scalars.AWSTimestamp.parseLiteral(astNode, null)).toEqual(1234);
});

it('Rejects non-integer literals', () => {
const astNode = { kind: 'StringValue', value: '1234', loc: { start: 68, end: 74 } } as ValueNode;

expect(() => {
scalars.AWSTimestamp.parseLiteral(astNode, null);
}).toThrow('Can only validate integers but received: StringValue');
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { URL } from 'url';
import { GraphQLInt, GraphQLScalarType, GraphQLError, Kind, StringValueNode } from 'graphql';
import { GraphQLInt, GraphQLScalarType, GraphQLError, Kind, StringValueNode, ValueNode } from 'graphql';
import { isValidNumber } from 'libphonenumber-js';

import { GraphQLDate, GraphQLTime, GraphQLDateTime } from 'graphql-iso-date';
Expand Down Expand Up @@ -104,8 +104,12 @@ are also accepted and these represent the number of seconds till 1970-01-01T00:0
parseValue(value) {
return GraphQLInt.parseValue(value) ? value : undefined;
},
parseLiteral(value) {
return GraphQLInt.parseLiteral(value, null) ? (value as StringValueNode).value : undefined;
parseLiteral(value: ValueNode) {
if (value.kind !== Kind.INT) {
throw new GraphQLError(`Can only validate integers but received: ${value.kind}`);
}

return Number.parseInt(value.value, 10);
},
});

Expand Down

0 comments on commit 08750a6

Please sign in to comment.