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

fix(amplify-appsync-simulator): appsync scalars #5705

Merged
merged 1 commit into from
Oct 28, 2020
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
2 changes: 1 addition & 1 deletion packages/amplify-appsync-simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"!**/*.d.ts"
],
"testURL": "http://localhost/",
"testRegex": "(src/__tests__/.*.test.*)$",
"testRegex": "(src/__tests__/.*.test.ts)$",
"moduleFileExtensions": [
"ts",
"tsx",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { scalars } from '../../schema/appsync-scalars';

describe('AWSJSON parse', () => {
it('Should reject an invalid json encoded object', () => {
function parse() {
scalars.AWSJSON.parseValue('{Foo: "Not Valid"}');
}
expect(parse).toThrowErrorMatchingSnapshot();
});
it('Should reject a non json string', () => {
function parse() {
scalars.AWSJSON.parseValue('hello world');
}
expect(parse).toThrowErrorMatchingSnapshot();
});

it('Should reject a non json boolean', () => {
function parse() {
scalars.AWSJSON.parseValue(true);
}
expect(parse).toThrowErrorMatchingSnapshot();
});

it('Should reject a non json int', () => {
function parse() {
scalars.AWSJSON.parseValue(1);
}
expect(parse).toThrowErrorMatchingSnapshot();
});

it('Should accept a json encoded object', () => {
expect(scalars.AWSJSON.parseValue('{"Foo": "Bar"}')).toMatchObject({
Foo: 'Bar',
});
});

it('Should accept a json string', () => {
expect(scalars.AWSJSON.parseValue('"Hello world"')).toEqual('Hello world');
});

it('Should accept a json boolean', () => {
expect(scalars.AWSJSON.parseValue('true')).toEqual(true);
});

it('Should accept a json int', () => {
expect(scalars.AWSJSON.parseValue('1')).toEqual(1);
});
});

describe('AWSJSON serialize', () => {
it('Should serialize an obkect', () => {
expect(scalars.AWSJSON.serialize({ foo: 'Bar' })).toEqual('{"foo":"Bar"}');
});

it('Should serialize a boolean', () => {
expect(scalars.AWSJSON.serialize(true)).toEqual('true');
});

it('Should serialize an integer', () => {
expect(scalars.AWSJSON.serialize(1)).toEqual('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AWSJSON parse Should reject a non json boolean 1`] = `"Unable to parse true as valid JSON."`;

exports[`AWSJSON parse Should reject a non json int 1`] = `"Unable to parse 1 as valid JSON."`;

exports[`AWSJSON parse Should reject a non json string 1`] = `"Unable to parse hello world as valid JSON."`;

exports[`AWSJSON parse Should reject an invalid json encoded object 1`] = `"Unable to parse {Foo: \\"Not Valid\\"} as valid JSON."`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLInt, GraphQLScalarType, GraphQLError, Kind, StringValueNode } from 'graphql';
import GraphQLJSON from 'graphql-type-json';
import { isValidNumber } from 'libphonenumber-js';

import { GraphQLDate, GraphQLTime, GraphQLDateTime } from 'graphql-iso-date';
Expand Down Expand Up @@ -139,12 +138,42 @@ const AWSIPAddress = new GraphQLScalarType({
},
});

const parseJson = (value: string) => {
if (typeof value !== "string") {
throw new GraphQLError(`Unable to parse ${value} as valid JSON.`);
}

try {
return JSON.parse(value);
} catch (error) {
throw new TypeError(`Unable to parse ${value} as valid JSON.`);
}
}

const AWSJSON = new GraphQLScalarType({
name: 'AWSJSON',
description: 'The AWSJSON scalar type represents a valid json object serialized as a string.',
serialize(value) {
return JSON.stringify(value);
},
parseValue(value) {
return parseJson(value);
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Unable to parse ${ast.kind} as valid JSON.`);
}

return parseJson(ast.value);
},
});

export const scalars = {
AWSJSON: GraphQLJSON,
AWSJSON,
AWSDate,
AWSTime,
AWSDateTime,
AWSPhone,
AWSPhone: new AWSPhone({ name: 'AWSPhone', description: 'AWSPhone' }),
AWSEmail: EmailAddressResolver,
AWSURL: URLResolver,
AWSTimestamp,
Expand Down
5 changes: 4 additions & 1 deletion packages/amplify-appsync-simulator/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export function generateResolvers(

return makeExecutableSchema({
typeDefs: doc,
resolvers: resolverMapWithAuth,
resolvers: {
...resolverMapWithAuth,
...scalars,
},
schemaDirectives,
});
}
Expand Down