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

feat(graphiql-toolkit)!: accept HeadersInit #3854

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

jasonkuhrt
Copy link

@jasonkuhrt jasonkuhrt commented Jan 17, 2025

This PR enhances graphiql-toolkit package fetcher utility to accept HeadersInit type instead of Record<string, string> in places where headers are accepted.

This is a backwards compatible change because Record<string, string> is a sub-type of HeadersInit. As well, care in this PR has been taken to preserve the way in which same-name headers from different sources would overwrite, not merge, together.

The benefit of this change is that consumers can now pass values of Headers or [string,string][] type which permits the possibility of a header repeated multiple, which HTTP supports.

In practice a place this issue became relevant was in my work on Hive Console's Laboratory Preflight Script feature:

graphql-hive/console#6378 (comment)

-- edit: fwiw I've since realized we're not blocked by this issue.

BREAKING CHANGE:

graphiql-toolkit functions now accept HeadersInit where previously a partially wider type of Record<string, unknown> was accepted. Therefore you may find for example that your project is no longer type checking after this upgrade. At runtime, nothing should change since if you weren't already putting in strings they were being coerced implicitly anyways. Therefore, in practice, this should only serve to improve your code with trivial effort.

Copy link

changeset-bot bot commented Jan 17, 2025

🦋 Changeset detected

Latest commit: b1ce926

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@graphiql/toolkit Minor
@graphiql/react Patch

Not sure what this means? Click here to learn what changesets are.

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

@jasonkuhrt
Copy link
Author

I will add a changeset once I receive feedback that the changes will be accepted.

packages/graphiql-toolkit/src/create-fetcher/lib.ts Outdated Show resolved Hide resolved
Comment on lines 213 to 220
const fetcherOptsHeaders = new Headers(fetcherOpts?.headers ?? {});
// @ts-expect-error: Current TS Config target does not support `Headers.entries()` method.
// However it is reported as "widely available" and so should be fine to use. This could
// would be more complicated without it.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
const fetcherOptsHeadersEntries: [string, string][] = [
...fetcherOptsHeaders.entries(),
];
Copy link
Author

Choose a reason for hiding this comment

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

Ideally this isn't needed. However I don't want to open a pandora's box by changing the tsconfig.json file.

@jasonkuhrt
Copy link
Author

Hey @acao @dimaMachina it looks like you're the current active maintainers. Could you let me know if you'll have time to look at this PR in the coming days?

Copy link
Collaborator

@dimaMachina dimaMachina left a comment

Choose a reason for hiding this comment

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

is it possible to add some test to avoid regression in the future?

@jasonkuhrt
Copy link
Author

I assume so, I haven't looked at the existing test suit yet. I wanted to get a general thumbs up first. From what you see, this makes sense and could get merged?

@dimaMachina
Copy link
Collaborator

I assume so, I haven't looked at the existing test suit yet. I wanted to get a general thumbs up first. From what you see, this makes sense and could get merged?

in my perspective yes once you add a test which fix current behaviour

@jasonkuhrt
Copy link
Author

@dimaMachina thanks for confirming. I will proceed this or next week with adding a test and fixing anything else CI is failing on.

@jasonkuhrt jasonkuhrt changed the title feat(graphiql-toolkit): accept HeadersInit feat(graphiql-toolkit)!: accept HeadersInit Jan 23, 2025
@jasonkuhrt
Copy link
Author

@dimaMachina I've added tests and changeset. Waiting for your feedback.

Copy link
Contributor

@TallTed TallTed left a comment

Choose a reason for hiding this comment

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

Tiny, human-facing

.changeset/early-pears-remember.md Outdated Show resolved Hide resolved
packages/graphiql-toolkit/src/create-fetcher/lib.ts Outdated Show resolved Hide resolved
packages/graphiql-toolkit/src/create-fetcher/lib.ts Outdated Show resolved Hide resolved
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.

3 participants