{"id":17204502,"url":"https://github.com/bboure/appsync-momento-utils","last_synced_at":"2025-03-25T09:43:18.961Z","repository":{"id":213179497,"uuid":"733242140","full_name":"bboure/appsync-momento-utils","owner":"bboure","description":"Utility functions and type definitions for interacting with Momento's API from AWS AppSync HTTP Resolvers written in JavaScript.","archived":false,"fork":false,"pushed_at":"2023-12-18T22:22:35.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-30T09:12:26.507Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bboure.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-18T22:12:45.000Z","updated_at":"2024-01-10T12:49:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"c30c3717-c84e-4f1a-a834-e43078c98f84","html_url":"https://github.com/bboure/appsync-momento-utils","commit_stats":null,"previous_names":["bboure/appsync-momento-utils"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bboure%2Fappsync-momento-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bboure%2Fappsync-momento-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bboure%2Fappsync-momento-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bboure%2Fappsync-momento-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bboure","download_url":"https://codeload.github.com/bboure/appsync-momento-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245437202,"owners_count":20615231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T02:22:11.758Z","updated_at":"2025-03-25T09:43:18.939Z","avatar_url":"https://github.com/bboure.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"This project contains utility functions and type definitions for interacting with [Momento](https://www.gomomento.com/)'s API from AWS AppSync HTTP Resolvers written in JavaScript (APPSYNC_JS runtime).\n\nUse this library if you want to use Momento as a caching layer for your resolver.\n\n## How to install\n\n```bash\nnpm i appsync-momento-utils\n```\n\n## How it works\n\nTo use this library, you will need:\n\n- An [HTTP resolver](https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-http-resolvers.html), which endpoint is [Momento's endpoint for your region](https://docs.momentohq.com/cache/develop/api-reference/http-api#regions).\n- [A Momento API Key](https://docs.momentohq.com/cache/develop/authentication/api-keys)\n\nTo use this library, you will need to configure pipeline resolvers with 3 (or more) functions. The first function should try to read from cache, if there is a hit, we return the result immediately and skip the other two functions.\n\nWhen the item is not found in the cache (miss), we move forward to resolve the data from the source. Finally, in the last function, we store the item in cache for future requests.\n\n```mermaid\ngraph TD;\n    start([Start]) --\u003e get[GetCacheItem]\n    get --\u003e hit_or_miss{Hit?}\n    hit_or_miss -- miss --\u003e resolver[ResolveData] --\u003e put[PutCacheItem] --\u003e ret\n    hit_or_miss -- hit --\u003e ret\n    ret([End])\n```\n\n## Usage\n\n#### `getRequest(ctx, params)`\n\nGenerates a Momento `GET` request from cache.\n\nPass it the `ctx` object from the response, the following values:\n\n- `apiKey`: your API Key\n- `cacheName`: the name of your Momento cache\n- `key`: the key of the item to retrieve from the cache\n\nExample\n\n```ts\nexport function request(ctx) {\n  return getRequest(ctx, {\n    apiKey: 'eyJlbmRwb...',\n    cacheName: 'my-cache',\n    key: 'my-key',\n  });\n}\n```\n\n#### `getResponse(ctx)`\n\nThis function handles the `GET` response from Momento. It intercepts the cache value (if found), and stores it in the AppSync stash object for later.\n\nYou must pass it the `ctx` object from the response.\n\n```ts\nexport function response(ctx) {\n  return getResponse(ctx);\n}\n```\n\n#### `handleCache(ctx)`\n\nWhen an item is found in the cache, we want to skip the execution of the following functions in the pipeline. To make sure that happens, and that the cache value is returned to the GraphQL request immediately (a.k.a [Early Return](https://docs.aws.amazon.com/appsync/latest/devguide/runtime-utils-js.html)), use this method in the `request` handle of all subsequent functions to the `GET` request.\n\nYou must pass it the `ctx` object from the request.\n\n```ts\nexport function request(ctx) {\n  handleCache(ctx);\n\n  // The default request in case of cache miss. e.g. DynamoDB GetItem\n}\n```\n\n#### `setRequest(ctx, params)`\n\nGenerates a Momento request to store a value into the cache.\n\nPass it the `ctx` object from the response, and the following parameters:\n\n- `apiKey`: your API Key\n- `cacheName`: the name of your Momento cache\n- `key`: the key of the item to retrieve from the cache\n- `value`: The value to store in the cache\n- `ttlSeconds`: (Optional, default is 3600). The TTL of the item in the cache.\n\nNote: This function already calls `handleCache(ctx)` for you.\n\n```ts\nexport function request(ctx) {\n  return setRequest(ctx, {\n    apiKey: 'eyJlbmRwb...',\n    cacheName: 'my-cache',\n    key: 'my-key',\n    value: 'Hello world',\n    ttlSeconds: 30,\n  });\n}\n```\n\n#### `setResponse(ctx)`\n\nThis function handle the response from Momento after setting a value. It makes sure the request executed correctly. Finally, it returns the value that was put into the cache.\n\nPass it the `ctx` object from the response.\n\n```ts\nexport function response(ctx) {\n  return setResponse(ctx);\n}\n```\n\n## References\n\n- [HTTP API Reference for Momento Cache](https://docs.momentohq.com/cache/develop/api-reference/http-api)\n- [AppSync HTTP Resolvers Documentation](https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-http-resolvers.html)\n- [AppSync Pipeline Resolvers Documentation](https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers-js.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbboure%2Fappsync-momento-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbboure%2Fappsync-momento-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbboure%2Fappsync-momento-utils/lists"}