7

Looking at the documentation here it appears there is no way of getting the requestTime from the context variable.

Is there any other way, apart from using a lambda resolver, to get that value?

I know it is possible when using API Gateway, so surely there is a way. Am I looking at the wrong thing?

Thanks

Julien

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Julienmachon
  • 171
  • 1
  • 7

1 Answers1

15

(I work on the AWS AppSync team)

You're right, we do not yet expose the request time inside the mapping template.

May I ask what your use case is?

This is valuable feedback, I'm going to make sure this gets seen by the team. I will update this thread as we have more information.

UPDATE: We now support extracting the current timestamp via mapping template helper methods. Here are a few functions that can help you achieve what you need:

$util.time.nowISO8601()                                            : 2018-02-06T19:01:35.749Z
$util.time.nowEpochSeconds()                                       : 1517943695
$util.time.nowEpochMilliSeconds()                                  : 1517943695750
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ")                    : 2018-02-06 19:01:35+0000
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ", "+08:00")          : 2018-02-07 03:01:35+0800
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ", "Australia/Perth") : 2018-02-07 03:01:35+0800

The complete reference is available on the Resolver Mapping Template Utility Reference page.

Tinou
  • 5,908
  • 4
  • 21
  • 24
  • Hi, thanks for your quick answer. I just want to add a `createdAt` and `updatedAt` attributes. The mapping alone is sufficient in most cases (basic CRUD operation). But for me, not being able to get the time means I have to use a lamdba for my write operation (which isn't too bad, let's be honest). Thanks – Julienmachon Jan 05 '18 at 09:00
  • 1
    @Julienmachon you could also pass the timestamp through as an argument for your GraphQL mutations and then the Mapping Template can grab that from $context.arguments and store it directly in DynamoDB – Richard Jan 07 '18 at 05:54
  • 2
    Hey @Richard. I guess I don't necessarily want to add yet another argument to my mutation input. Plus, I would never trust a date coming from the client without validation. Also comes issues like time zone, etc... So unfortunately, this would not work for me. – Julienmachon Jan 09 '18 at 18:57
  • @Tinou you guys should update the appsync documentation, it doesn't include the time variable. – Munib Mar 18 '19 at 20:47
  • 1
    @Munib I updated my answer with the correct link to the time helpers. Thanks! – Tinou Mar 19 '19 at 00:05
  • @Tinou Thanks for quick reply, I appreciate it. I can't wrap my head around how would I go about getting the exact date for the end of the current week (7 Days)? Say that today is March 27, 2019, I would like to calculate the value of April 2, 2019 (Including the current day). Given the util time functions, I can't see how one would go about doing this. – Munib Mar 27 '19 at 21:45
  • 1
    If you need to add days you could work with the epoch seconds and add the days in seconds. If you’re looking for the utility to tell you when the end of the week is, then we don’t offer these utilities. It’s good feedback though and I’ll make sure the team sees this. – Tinou Mar 29 '19 at 06:52