I am stuck on the simple task of uploading a timestamp via AppSync.
I have made this test schema for easy demonstration:
type Mutation {
testCreateCar(input: TestCreateCarInput!): TestCar
@aws_cognito_user_pools
}
type TestCar {
regno: String!
country: String!
timestamp: Int
}
input TestCreateCarInput {
regno: String!
country: String!
timestamp: Int
}
This is my resolver:
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"regno": $util.dynamodb.toDynamoDBJson($ctx.args.regno),
"country": $util.dynamodb.toDynamoDBJson($ctx.args.country),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}
This is the function in my FE that makes the request:
export const createCar = async (carData: Car) => {
try {
await client.graphql({
query: mutations.testCreateCar,
variables: { input: {
regno: carData.regno,
country: carData.country,
timestamp: Date.now()
} }
})
} catch (err) {
throw err;
}
};
When I try and upload a car I get the following error:
"Variable 'timestamp' has an invalid value."
How can this be? The Date.now() returns a number and I have clearly specified that "timestamp" is of type number in my schema.
Much appreciated for your time, thank you!
In your schema, you declare your timestamp is of 'Int' type. Current unix timestamp (which is what Date.now()
returns) as of today is 1708650175742, which is more then an Int value can handle (INT_MAX is 2147483647).
You will probably want to use a custom type for it - don't know what exact graphql you are using, but there are a lot of solutions available (e.g. graphql-iso-date as part of graphql-scalars package). There are some other links in this question (and one of the answers describes how to make your own).
Alternatively, if this is a one-time-demo that you will never need for real... Just have your car be made in Jan 1970 (Date.parse('12 Jan 1970 12:00:00 GMT')
instead of Date.now()
), so that the unix timestamp is small enough to fit into an int:)