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

Make "async fetchOptionsExchange" example copy-able #1835

Merged
merged 2 commits into from
Aug 4, 2021

Conversation

44px
Copy link
Contributor

@44px 44px commented Aug 1, 2021

Summary

The current example perfectly suits some use-cases but can't be used "as is". If you copy it into TS project, you'll see a few errors. Fixing these errors requires some understanding of exchange internals and wonka API.

I see two possibilities how to make it easier to get started with the urql here:

  1. Make it able to copy and paste the code example
  2. Add fetchOptionsExhange to the core (as @RodolfoSilva originally suggested here)

I implemented first as a simple solution, but maybe second would be even a better option?

Set of changes

Changed the current example a bit by adding the missing imports and types.

@changeset-bot
Copy link

changeset-bot bot commented Aug 1, 2021

⚠️ No Changeset found

Latest commit: 4cf52e9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

);
};
export const fetchOptionsExchange =
(fn: (fetchOptions: RequestInit) => Promise<RequestInit>): Exchange =>
Copy link
Collaborator

@JoviDeCroock JoviDeCroock Aug 3, 2021

Choose a reason for hiding this comment

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

to avoid issues we initially made this (fetchOptions: RequestInit) => Promise<RequestInit> | RequestInit and had a ternary that did this in the stream:

const result = fn(operation.context.fetchOptions);
return pipe(
  typeof result.then === 'function' ? fromPromise(result) : fromValue(result),
);

This mainly to avoid introducing a footgun 😅 WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, sounds good. Fixed! Thanks for the tip 👍

Copy link
Collaborator

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

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

Awesome thank you! 🙌

@JoviDeCroock JoviDeCroock merged commit a015633 into urql-graphql:main Aug 4, 2021
@44px 44px deleted the patch-1 branch August 4, 2021 13:08
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.

2 participants