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

Cached REST-Response does not contain URL #35

Closed
marhaupe opened this issue Mar 26, 2020 · 1 comment · Fixed by #105
Closed

Cached REST-Response does not contain URL #35

marhaupe opened this issue Mar 26, 2020 · 1 comment · Fixed by #105

Comments

@marhaupe
Copy link

marhaupe commented Mar 26, 2020

I use apollo-datasource-rest to cache responses from remote REST-APIs. Responses that have a 404 status get cached and thus read from cached on subsequent calls to that same endpoint. This is fine, until the error for the request gets created. The first request - the one that actually hit the remote and not the cache - creates an error like this:

      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "response": {
          "url": "http://localhost:4000/someResource",
          "status": 404,
          "statusText": "Not Found",
          "body": "Not found"
        },
        "exception": {
          "stacktrace": [
            "Error: 404: Not Found",
            "    at ExampleDataSource.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:84:25)",
            "    at Generator.next (<anonymous>)",
            "    at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71",
            "    at new Promise (<anonymous>)",
            "    at __awaiter (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:4:12)",
            "    at ExampleDataSource.errorFromResponse (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:74:16)",
            "    at ExampleDataSource.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:52:34)",
            "    at Generator.next (<anonymous>)",
            "    at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71",
            "    at new Promise (<anonymous>)"
          ]
        }
      }

The important bit is the url-field telling us which exact request failed. The actual issue lies in the second request - the one that hits the cache -, which creates the following error:

      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "response": {
          "status": 404,
          "statusText": "Not Found",
          "body": "Not found"
        },
        "exception": {
          "stacktrace": [
            "Error: 404: Not Found",
            "    at ExampleDataSource.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:84:25)",
            "    at Generator.next (<anonymous>)",
            "    at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71",
            "    at new Promise (<anonymous>)",
            "    at __awaiter (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:4:12)",
            "    at ExampleDataSource.errorFromResponse (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:74:16)",
            "    at ExampleDataSource.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:52:34)",
            "    at Generator.next (<anonymous>)",
            "    at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71",
            "    at new Promise (<anonymous>)"
          ]
        }
      }

As you can see, the url-field is missing. Interestingly enough, this only happens if i set a ttl in the RequestInit object I pass to the datasource. I prepared a small reproduction for this issue: https://codesandbox.io/s/little-microservice-hm0sv

I analyzed the source and believe the line causing the issue is this one: https://github.com/apollographql/apollo-server/blob/c177acd7959aaf516f8ac814f1a6bb2e6d3ed2e6/packages/apollo-datasource-rest/src/HTTPCache.ts#L60
Before spending too much time looking for possible solutions, I wanted to make sure that this is in fact an issue and not something you actually intended because of some piece of information that I'm currently missing.

@glasser glasser transferred this issue from apollographql/apollo-server Oct 11, 2022
glasser added a commit that referenced this issue Dec 3, 2022
Previously, the `url` on the `FetcherResponse` returned from an
HTTPCache would be set properly if this was an actual fetch, but set to
'' if it were read from the cache.

This issue was introduced in
apollographql/apollo-server#1362 (as part of the
initial creation of RESTDataSource) as part of allowing for custom cache
keys.

Fixes #35.
glasser added a commit that referenced this issue Dec 5, 2022
Previously, the `url` on the `FetcherResponse` returned from an
HTTPCache would be set properly if this was an actual fetch, but set to
'' if it were read from the cache.

This issue was introduced in
apollographql/apollo-server#1362 (as part of the
initial creation of RESTDataSource) as part of allowing for custom cache
keys.

Fixes #35.
@marhaupe
Copy link
Author

marhaupe commented Dec 6, 2022

Cheers!

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

Successfully merging a pull request may close this issue.

1 participant