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

RESTDataSource, need custom context per Request/Response #31

Open
jounii opened this issue Feb 18, 2020 · 1 comment
Open

RESTDataSource, need custom context per Request/Response #31

jounii opened this issue Feb 18, 2020 · 1 comment

Comments

@jounii
Copy link

jounii commented Feb 18, 2020

I'm currently refactoring some REST API to use RESTDataSource as the fetch implementation. Current implementation uses similar custom rest/fetch implementation, which is fairly okay but does have it's caveats and does not provide the caching features.

However it does provide way to pass custom context on request/response pair. This is used for (in RESTDataSource terms) the parseBody to do more detailed json data denormalization. I was hoping I could implement this just extending the RESTDataSource, but seems it's not that easy as the request/response pair is being detached from each other. The didReceiveResponse does link up a bit, but as how the node-fetch Request and Response are internally initialized, any custom additions to the init: RequestInit parameter are not passed through just with Request and Response objects.

Right now I see only three options for this.

  1. FEATURE REQUEST: RESTDataSource should have way to pass "request context" which is passed through the pipe down to parseBody. This would work then when extending from RESTDataSource
  2. Hackly solution I'm trying is to use local Map to store the data and link by "request-id" which is passed through the request headers and then copied in didReceiveResponse to response headers which can be accessed in parseBody.
  3. Don't try to extend RESTDataSource, just create wrapper class and use RESTDataSource as it-is for any fetches, post process the responses result data in the wrapper

Because lack of direct support for option 1, I'm a bit leaning towards the option 3, although the RESTDataSource code seems to be done extending in mind. So probably have to extend RESTDataSource for some customzation and then wrap it..

Other suggestions?

@glasser glasser transferred this issue from apollographql/apollo-server Oct 11, 2022
@glasser
Copy link
Member

glasser commented Dec 14, 2022

The feature request seems reasonable. Perhaps the best approach here would be a GraphQL-style extensions that you can put on the RequestOptions object which you're welcome to do whatever you want with. I don't plan to implement this myself now but we'd be happy to review a PR.

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

No branches or pull requests

2 participants