-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(graphiql-toolkit)!: accept HeadersInit #3854
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
Open
jasonkuhrt
wants to merge
11
commits into
graphql:main
Choose a base branch
from
jasonkuhrt:feat/graphiql-toolkit/accept-headers-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dc3dca7
feat(graphiql-toolkit): accept HeadersInit
jasonkuhrt 863ae8c
refactor
jasonkuhrt edb63e8
refactor
jasonkuhrt 56219f0
refactor
jasonkuhrt ba80845
add tests
jasonkuhrt 82626ba
fix types in graphiql-react
jasonkuhrt 1e5415e
changelog
jasonkuhrt f309b5b
Update .changeset/early-pears-remember.md
jasonkuhrt 6d6af80
Update packages/graphiql-toolkit/src/create-fetcher/lib.ts
jasonkuhrt b1ce926
Update packages/graphiql-toolkit/src/create-fetcher/lib.ts
jasonkuhrt 26db879
Update .changeset/early-pears-remember.md
dimaMachina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,23 @@ const errorHasCode = (err: unknown): err is { code: string } => { | |
return typeof err === 'object' && err !== null && 'code' in err; | ||
}; | ||
|
||
/** | ||
* Merge two Headers instances into one. | ||
* | ||
* Returns a new Headers instance (does not mutate). | ||
* | ||
* Headers are merged by having a copy of the first headers argument apply its `set` | ||
* method to assign each header from the second headers argument. This means that headers | ||
* from the second Headers instance overwrite same-named headers in the first. | ||
*/ | ||
const mergeHeadersWithSetStrategy = (headersA: Headers, headersB: Headers) => { | ||
const newHeaders = new Headers(headersA); | ||
for (const [key, value] of headersB.entries()) { | ||
newHeaders.set(key, value); | ||
} | ||
return newHeaders; | ||
}; | ||
|
||
/** | ||
* Returns true if the name matches a subscription in the AST | ||
* | ||
|
@@ -57,14 +74,18 @@ export const isSubscriptionWithName = ( | |
export const createSimpleFetcher = | ||
(options: CreateFetcherOptions, httpFetch: typeof fetch): Fetcher => | ||
async (graphQLParams: FetcherParams, fetcherOpts?: FetcherOpts) => { | ||
const headers = [ | ||
new Headers({ | ||
'content-type': 'application/json', | ||
}), | ||
new Headers(options.headers ?? {}), | ||
new Headers(fetcherOpts?.headers ?? {}), | ||
].reduce(mergeHeadersWithSetStrategy, new Headers()); | ||
|
||
const data = await httpFetch(options.url, { | ||
method: 'POST', | ||
body: JSON.stringify(graphQLParams), | ||
headers: { | ||
'content-type': 'application/json', | ||
...options.headers, | ||
...fetcherOpts?.headers, | ||
}, | ||
headers, | ||
}); | ||
return data.json(); | ||
}; | ||
|
@@ -141,17 +162,19 @@ export const createMultipartFetcher = ( | |
httpFetch: typeof fetch, | ||
): Fetcher => | ||
async function* (graphQLParams: FetcherParams, fetcherOpts?: FetcherOpts) { | ||
const headers = [ | ||
new Headers({ | ||
'content-type': 'application/json', | ||
accept: 'application/json, multipart/mixed', | ||
}), | ||
new Headers(options.headers ?? {}), | ||
new Headers(fetcherOpts?.headers ?? {}), | ||
].reduce(mergeHeadersWithSetStrategy, new Headers()); | ||
|
||
const response = await httpFetch(options.url, { | ||
method: 'POST', | ||
body: JSON.stringify(graphQLParams), | ||
headers: { | ||
'content-type': 'application/json', | ||
accept: 'application/json, multipart/mixed', | ||
...options.headers, | ||
// allow user-defined headers to override | ||
// the static provided headers | ||
...fetcherOpts?.headers, | ||
}, | ||
headers, | ||
}).then(r => | ||
meros<Extract<ExecutionResultPayload, { hasNext: boolean }>>(r, { | ||
multiple: true, | ||
|
@@ -187,9 +210,21 @@ export async function getWsFetcher( | |
return createWebsocketsFetcherFromClient(options.wsClient); | ||
} | ||
if (options.subscriptionUrl) { | ||
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(), | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// todo: If there are headers with multiple values, they will be lost. Is this a problem? | ||
jasonkuhrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const fetcherOptsHeadersRecord = Object.fromEntries( | ||
fetcherOptsHeadersEntries, | ||
); | ||
return createWebsocketsFetcherFromUrl(options.subscriptionUrl, { | ||
...options.wsConnectionParams, | ||
...fetcherOpts?.headers, | ||
...fetcherOptsHeadersRecord, | ||
}); | ||
} | ||
const legacyWebsocketsClient = options.legacyClient || options.legacyWsClient; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.