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

[apollo-datasource-rest] - Support fetch arraybuffer and blob response types #29

Open
schematis opened this issue Sep 13, 2019 · 1 comment

Comments

@schematis
Copy link

schematis commented Sep 13, 2019

I need to fetch raw image data from a service and then return a base64 encoded version of that data to the front end. After testing, I discovered that the stock node-fetch implementation is mangling the data without the appropriate response type. There doesn't appear to currently be a way with RESTDatasource to resolve the different types that fetch supports (arraybuffer, blob, text, json, etc).

Is this something that can be added in?

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

glasser commented Dec 14, 2022

You can override parseBody() to call arraybuffer() based on headers or whatever if you'd like. (You may want to override the new cloneParsedBody() as well if you're producing something mutable.)

However, you do run into trouble if the body is not a UTF-8 string, because the HTTPCache writes UTF-8 strings to the shared cache (created via JSON.stringify, read with response.text()). This is storing the raw body pre-JSON-parsing, but will mangle the body if it's non-UTF-8. I think the best thing to do would be to allow you to specify that the body should be treated as binary (maybe by a new field under cacheOptions?) and in that case we could base64 the body and store it in a different key in the object written to the cache. (Always base64-ing seems like an unnecessary performance and space usage in the common JSON/text case.)

This is something we'd be happy to review a PR for.

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