From 08750a6f7f9e9326a81b5d45a7ef31bd9f30595f Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Fri, 9 Apr 2021 18:50:46 -0400 Subject: [PATCH] fix(amplify-appsync-simulator): parse AWSTimestamp literals as Ints (#6979) --- .../src/__tests__/scalars/AWSTimestamp.test.ts | 17 +++++++++++++++++ .../src/schema/appsync-scalars/index.ts | 10 +++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 packages/amplify-appsync-simulator/src/__tests__/scalars/AWSTimestamp.test.ts diff --git a/packages/amplify-appsync-simulator/src/__tests__/scalars/AWSTimestamp.test.ts b/packages/amplify-appsync-simulator/src/__tests__/scalars/AWSTimestamp.test.ts new file mode 100644 index 00000000000..9e60bf5049c --- /dev/null +++ b/packages/amplify-appsync-simulator/src/__tests__/scalars/AWSTimestamp.test.ts @@ -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'); + }); +}); diff --git a/packages/amplify-appsync-simulator/src/schema/appsync-scalars/index.ts b/packages/amplify-appsync-simulator/src/schema/appsync-scalars/index.ts index 1e566f8eb82..8cd35d099ab 100644 --- a/packages/amplify-appsync-simulator/src/schema/appsync-scalars/index.ts +++ b/packages/amplify-appsync-simulator/src/schema/appsync-scalars/index.ts @@ -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'; @@ -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); }, });