-
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 7 commits
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
'@graphiql/toolkit': minor | ||
'@graphiql/react': patch | ||
--- | ||
|
||
`graphiql-toolkit` now accepts `HeadersInit` input. | ||
|
||
`graphiql-react` has internal type changes to support this. | ||
|
||
BREAKING CHANGE: | ||
|
||
Because `graphiql-toolkit` functions now accept HeadersInit where previously a partially wider type of `Record<string, unknown>` was accepted, there is a technical backwards incompatibility. This new stricter type could for example cause your project to fail type checking after this upgrade. At runtime, nothing should change since if you weren't already using `string` typed value headers already then they were being coerced implicitly. In practice, this should only serve to marginally improve your code with trivial effort. | ||
jasonkuhrt marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
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
36 changes: 36 additions & 0 deletions
36
packages/graphiql-toolkit/src/create-fetcher/__tests__/_helpers.ts
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable */ | ||
|
||
import { Mock, it as itBase } from 'vitest'; | ||
|
||
export const test = itBase.extend<{ | ||
fetch: Mock<typeof fetch>; | ||
graphqlWs: { | ||
createClient: Mock< | ||
(parameters: { connectionParams: Record<string, string> }) => any | ||
>; | ||
}; | ||
}>({ | ||
// @ts-expect-error fixme | ||
fetch: async ({}, use) => { | ||
const originalFetch = globalThis.fetch; | ||
const mock = vi | ||
.fn() | ||
.mockResolvedValueOnce(new Response(JSON.stringify({ data: {} }))); | ||
globalThis.fetch = mock; | ||
await use(fetch); | ||
globalThis.fetch = originalFetch; | ||
}, | ||
graphqlWs: async ({}, use) => { | ||
const graphqlWsExports = { | ||
createClient: vi.fn(() => { | ||
return { | ||
subscribe: vi.fn(), | ||
}; | ||
}), | ||
}; | ||
vi.doMock('graphql-ws', () => { | ||
return graphqlWsExports; | ||
}); | ||
await use(graphqlWsExports); | ||
}, | ||
}); |
77 changes: 77 additions & 0 deletions
77
packages/graphiql-toolkit/src/create-fetcher/__tests__/createFetcher.spec.ts
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint-disable */ | ||
|
||
import { parse } from 'graphql'; | ||
import { createGraphiQLFetcher } from '../createFetcher'; | ||
import { test } from './_helpers'; | ||
|
||
interface TestCase { | ||
constructor: HeadersInit; | ||
send: HeadersInit; | ||
expected: Record<string, string>; | ||
} | ||
|
||
const H = Headers; | ||
const cases: TestCase[] = [ | ||
// --- levels merge | ||
{ constructor: { a:'1' } , send: { b:'2' } , expected: { a:'1', b:'2' } }, | ||
{ constructor: [['a','1']] , send: [['b','2']] , expected: { a:'1', b:'2' } }, | ||
{ constructor: new H({a:'1'}) , send: new H({b:'2'}) , expected: { a:'1', b:'2' } }, | ||
// --- send level takes precedence | ||
{ constructor: { a:'1' } , send: { a:'2' } , expected: { a:'2' } }, | ||
{ constructor: [['a','1']] , send: [['a','2']] , expected: { a:'2' } }, | ||
{ constructor: new H({a:'1'}) , send: new H({a:'2'}) , expected: { a:'2' } }, | ||
]; // prettier-ignore | ||
|
||
describe('accepts HeadersInit on constructor and send levels, send taking precedence', () => { | ||
test.for(cases)('%j', async (_case, { fetch }) => { | ||
const fetcher = createGraphiQLFetcher({ | ||
url: 'https://foobar', | ||
enableIncrementalDelivery: false, | ||
headers: _case.constructor, | ||
}); | ||
await fetcher({ query: '' }, { headers: _case.send }); | ||
// @ts-expect-error | ||
const requestHeaders = Object.fromEntries(new Headers(fetch.mock.calls[0]?.[1]?.headers ?? {}).entries()); // prettier-ignore | ||
expect(fetch).toBeCalledTimes(1); | ||
expect(requestHeaders).toMatchObject(_case.expected); | ||
}); | ||
|
||
test.for(cases)('incremental delivery: %j', async (_case, { fetch }) => { | ||
const fetcher = createGraphiQLFetcher({ | ||
url: 'https://foobar', | ||
enableIncrementalDelivery: true, | ||
headers: _case.constructor, | ||
}); | ||
const result = await fetcher({ query: '' }, { headers: _case.send }); | ||
// TODO: Improve types to indicate that result is AsyncIterable when enableIncrementalDelivery is true | ||
await drainAsyncIterable(result as AsyncIterable<any>); | ||
// @ts-expect-error | ||
const requestHeaders = Object.fromEntries(new Headers(fetch.mock.calls[0]?.[1]?.headers ?? {}).entries()); // prettier-ignore | ||
expect(fetch).toBeCalledTimes(1); | ||
expect(requestHeaders).toMatchObject(_case.expected); | ||
}); | ||
|
||
test.for(cases)('subscription: %j', async (_case, { graphqlWs }) => { | ||
const fetcher = createGraphiQLFetcher({ | ||
url: 'https://foobar', | ||
headers: _case.constructor, | ||
subscriptionUrl: 'wss://foobar', | ||
}); | ||
await fetcher({ query: '', operationName:'foo' }, { headers: _case.send, documentAST: parse('subscription foo { bar }') }); // prettier-ignore | ||
const connectionParams = graphqlWs.createClient.mock.calls[0]?.[0]?.connectionParams ?? {}; // prettier-ignore | ||
expect(graphqlWs.createClient).toBeCalledTimes(1); | ||
expect(connectionParams).toMatchObject(_case.expected); | ||
}); | ||
}); | ||
|
||
// ------------------------------------------------------------------------------------------------- | ||
// Helpers | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
const drainAsyncIterable = async (iterable: AsyncIterable<any>) => { | ||
const result: any[] = []; | ||
for await (const item of iterable) { | ||
result.push(item); | ||
} | ||
return result; | ||
}; |
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
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
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.