Skip to content

Undocumented and non-configurable memoization feature on apollo-datasource-rest #6603

@Jaxolotl

Description

@Jaxolotl

Package name and version

apollo-datasource-rest V3.6.1

Expected behavior

  • I'm able to control whether to enable memoization or not
  • If I opt for not using memoization then all my GET requests are executed
  • I expect to find the behavior documented somewhere (on the readme file and/or on the apollo server "datasources" documentation
  • I am able to specifically decide how to cache the requests (e.g headers, redis, etc)

Actual behavior

  • All GET requests will be memoized no matter you want it or not
  • I'm unable to opt out from memoization
  • No documentation is available, you have to dig into the source code to find it or accidentally find a clue on a Khalil Stemmler's blog
  • The package is adding a hidden non configurable caching layer bypassing any other standard caching mechanism

Description of the case

The datasource will keep a memoized version of all the GET requests until a POST request is made using the same URI or you programmatically delete the entry key (this.memoizedResults.delete(<MY_KEY>)) or clear all the entries (this.memoizedResults.clear()).

Not being documented and not having and opt-out configuration for the feature will cause a data inconsistency problem whenever you only make get requests to a system you have just read-only access (if another system updates the data you will never get the new data)

temporary workaround

You can always do the following to workaround the problem on your datasource derived class but is not a solution

export abstract class MyExtendedRestDataSource extends RESTDataSource {
  constructor() {
    super();
  }

  override willSendRequest(request: RequestOptions) {
    this.memoizedResults.clear(); // brute-force clearing the entries before every request
  }
}

Steps to repro

The package code is simple enough and self explanatory to be able to repro with a unit test.

Extra notes

I saw a couple tickets that might be related to this problem but they were described as generic caching issues without providing a detailed explanation of the problem. Hope you find it useful

apollographql/datasource-rest#72
apollographql/datasource-rest#46

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions