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

AppSync Simulator: Add support for Long scalar type, which works on AppSync but not on the simulator #3107

Open
2 tasks done
sc0ttdav3y opened this issue Jan 7, 2025 · 2 comments

Comments

@sc0ttdav3y
Copy link

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

  1. Create a Graphql schema as below
  2. Run the simulator (in my case I'm running via npx serverless offline start --noAuth, which uses appsync-simulator via the serverless-appsync-simulator package)
  3. 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:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@ykethan
Copy link
Member

ykethan commented Jan 7, 2025

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-cli Jan 7, 2025
@AnilMaktala AnilMaktala added api-graphql feature-request New feature or request and removed pending-triage labels Jan 7, 2025
@AnilMaktala
Copy link
Member

HI @sc0ttdav3y, Thanks for raising this. we will mark this as a feature request for the team to evaluate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants