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

Pass fetchOptions to fetch call to match HttpLink behavior #302

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

TrevinAvery
Copy link

HttpLink allows fetchOptions to be passed in the context of a query's options. This is then passed to the fetch call. This allows for finer control of the fetch, such as attaching an AbortController so the it can be cancelled.

This PR adds support for this same option to match this behavior. For values in fetchOptions that can be passed in the link context or the query directive (method, headers, body, and credentials), the values in fetchOptions take precedence.

@apollo-cla
Copy link

@TrevinAvery: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@fbartho
Copy link
Collaborator

fbartho commented Nov 15, 2022

@TrevinAvery is this PR a duplicate of #296 ? -- If so, which one should I go with? Also, I can't proceed unless you sign the Apollo Contributor License agreement, please let me know i you want to continue!

@TrevinAvery
Copy link
Author

@fbartho I have signed the CLA.

#296 does appear to add support for the abort controller, however, this PR supports all fetch options as overrides. I would take this one as it is more comprehensive. That said, I did not include a test that actually aborts a fetch and confirms it was cancelled. It may be worth at least pulling in that test from the other PR.

@@ -2497,6 +2695,111 @@ describe('Query options', () => {
]);
});
});
describe('body', () => {
it('prioritizes fetchOptions body over directive body', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does fetchOptions body come from? What is it for?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of interplaying features, and I'm not super comfortable with blindly accepting a body via a context parameter without understanding the spec.

The abortcontroller is interesting and makes sense to me, but I don't think the body should be passed through?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the body option is necessarily the most useful thing, but I included it for completeness. I was copying the behavior in the HTTP Link, which simply passes all fetch options to the fetch function. I think it makes sense to include this option so no one tries to use it and gets confused by it not working (which is the state I was in when I tried to use an abort controller). It should not break any existing behavior.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that we can't guarantee other Links aren't accidentally injecting a body, and if other links do inject a body, we can't parse the results or otherwise handle it.

I get why AbortController makes sense, but if we use the body, we're actually building apollo-link-rest into a really inefficient clone of HTTPLink that doesn't do the right thing?

Maybe I'm confused as to how this body is used normally and when it would be expected to be passed to this link.

@TrevinAvery
Copy link
Author

TrevinAvery commented Nov 16, 2022 via email

@fbartho
Copy link
Collaborator

fbartho commented Nov 16, 2022

I agree with you @TrevinAvery -- we don't know how likely it is for somebody to use it, but I bet it would initially be an indication of a bug.

I'd be comfortable if you followed path 1, remove/disable the body option. And I'm fine if you log a warning. I don't think we should go with option 2, until and unless somebody specifically makes a case for this feature being very useful. -- Because then we can write tests and docs that explain the use!

You also said:

It may be worth at least pulling in that test from the other PR.

And I agree here too, if you pull in that test, and make the change (1) you proposed, then I'd be inclined to build a new release that includes this feature. Just let me know!

@TrevinAvery
Copy link
Author

TrevinAvery commented Feb 14, 2023

@fbartho Sorry this took so long. I have had some serious deadlines at work. I have made the changes we discussed.

@TrevinAvery
Copy link
Author

@fbartho Can you please review this PR and let me know if it is acceptable?

@TrevinAvery
Copy link
Author

@fbartho Are you still out there? Can we merge this in please?

@TrevinAvery
Copy link
Author

@fbartho Although this branch is now out of date, I still think it would be a valuable addition to the project. I would be happy to update the branch so it can be merged if there is any interest in adding this feature. Please let me know if we can move this forward, or if this pull request should instead be closed.

@fbartho
Copy link
Collaborator

fbartho commented Mar 23, 2024

hey @TrevinAvery - I missed that you had updated the commits in this PR after my last comment, and I haven’t been actively developing on this library so it hasn’t been top of mind. I’ll try to take a look soon! I generally am positive on the purpose of this change, just not sure what it’ll take to get this PR safe to merge!

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 this pull request may close these issues.

None yet

3 participants