amplify-category-api
amplify-category-api copied to clipboard
AppSync Simulator: Add support for Long scalar type, which works on AppSync but not on the simulator
How did you install the Amplify CLI?
npm, via Serverless Framework
If applicable, what version of Node.js are you using?
No response
Amplify CLI Version
N/A (but problem exists in 2.16.6 of appsync-simulator)
What operating system are you using?
Mac
Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.
No manual changes
Describe the bug
AppSync at AWS has supported the Long scalar type since at least 2020, even though it does not appear in its official docs.
I have recently had cause to use this due to an Int growing out of range in my project, and found it works fine.
However, appsync-simulator does not support this type, causing local development to crash.
I've found it trivial to add it locally into the appsync codebase, and I've shared my solution here.
Expected behavior
I expect the simulator to try to behave as closely as possible to the production system.
The following schema works on AWS AppSync, and should be supported in the simulator:
type ExampleSchema {
id: Long
}
Reproduction steps
- Create a Graphql schema as below
- Run the simulator (in my case I'm running via
npx serverless offline start --noAuth, which usesappsync-simulatorvia theserverless-appsync-simulatorpackage) - You will get the error below when it initialises (you don't even need to hit the endpoint)
Schema
type Query {
run(): SampleSchema
}
type SampleSchema {
id: Long
name: String
}
Project Identifier
No response
Log output
The following error is returned in the CLI when the simulator starts up.
Error: Unknown type "Long". Did you mean "Config"?
Unknown type "Long". Did you mean "Config"?
at assertValidSDL (graphql/validation/validate.js:108:11)
at Object.buildASTSchema (graphql/utilities/buildASTSchema.js:71:34)
at generateDefaultSubscriptions (amplify-appsync-simulator/lib/schema/index.js:148:28)
at Object.generateResolvers (amplify-appsync-simulator/lib/schema/index.js:125:32)
at AmplifyAppSyncSimulator.init (amplify-appsync-simulator/lib/index.js:129:37)
....
This is because amplify-appsync-simulator/lib/schema/appsync-scalars/index.js and index.d.ts are missing the Long scalar type.
Additional information
Adding the following fixes it.
appsync-scalars/index.js:
// This adds the Long scalar type
var Long = new graphql_1.GraphQLScalarType({
name: 'Long',
description: 'A large 64-bit integer.',
serialize: function (value) {
return value;
},
parseValue: function (value) {
return value;
},
parseLiteral: function (ast) {
if (ast.kind !== graphql_1.Kind.INT) {
throw new graphql_1.GraphQLError("Can only validate ints as Longs but got a: " + ast.kind);
}
return value;
},
});
exports.scalars = {
AWSJSON: AWSJSON,
AWSDate: AWSDate,
AWSTime: AWSTime,
AWSDateTime: AWSDateTime,
AWSPhone: new AWSPhone({ name: 'AWSPhone', description: 'AWSPhone' }),
AWSEmail: AWSEmail,
AWSURL: AWSURL,
AWSTimestamp: AWSTimestamp,
AWSIPAddress: AWSIPAddress,
Long: Long, // <--- this exports it
};
appsync-scalars/index.d.ts:
export declare const scalars: {
AWSJSON: GraphQLScalarType;
AWSDate: GraphQLScalarType;
AWSTime: GraphQLScalarType;
AWSDateTime: GraphQLScalarType;
AWSPhone: AWSPhone;
AWSEmail: GraphQLScalarType;
AWSURL: GraphQLScalarType;
AWSTimestamp: GraphQLScalarType;
AWSIPAddress: GraphQLScalarType;
Long: GraphQLScalarType; // <--- add this here
};
This ninja-edit was made in the compiled source to demonstrate how easy it is to fix, and the line within the actual project is here: https://github.com/aws-amplify/amplify-cli/blob/9ebe9903042e30028c610e4f0641a9d9ebf8dfb9/packages/amplify-appsync-simulator/src/schema/appsync-scalars/index.ts#L224
Before submitting, please confirm:
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.