-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL SSE Distinct Connections support
- Loading branch information
Showing
15 changed files
with
282 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'graphql-yoga': minor | ||
--- | ||
|
||
GraphQL SSE Distinct Connections mode support with `sse.graphqlSSEDistinctConnections` flag |
This file contains 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 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 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 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
87 changes: 87 additions & 0 deletions
87
packages/graphql-yoga/__integration-tests__/graphql-sse-client.spec.ts
This file contains 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,87 @@ | ||
import { ExecutionResult } from 'graphql' | ||
import { createClient } from 'graphql-sse' | ||
import { createSchema, createYoga } from '../src' | ||
|
||
describe('GraphQL SSE Client compatibility', () => { | ||
describe('Distinct Connections', () => { | ||
const yoga = createYoga({ | ||
legacySse: false, | ||
schema: createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String | ||
} | ||
type Subscription { | ||
greetings: String | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'world', | ||
}, | ||
Subscription: { | ||
greetings: { | ||
async *subscribe() { | ||
yield { greetings: 'Hi' } | ||
await new Promise((resolve) => setTimeout(resolve, 300)) | ||
yield { greetings: 'Bonjour' } | ||
await new Promise((resolve) => setTimeout(resolve, 300)) | ||
yield { greetings: 'Hola' } | ||
await new Promise((resolve) => setTimeout(resolve, 300)) | ||
yield { greetings: 'Ciao' } | ||
await new Promise((resolve) => setTimeout(resolve, 300)) | ||
yield { greetings: 'Hallo' } | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
const client = createClient({ | ||
url: 'http://localhost:4000/graphql', | ||
fetchFn: yoga.fetch, | ||
abortControllerImpl: yoga.fetchAPI.AbortController, | ||
retryAttempts: 0, | ||
}) | ||
let unsubscribe: () => void | ||
afterAll(() => { | ||
unsubscribe?.() | ||
client.dispose() | ||
}) | ||
it('handle queries', async () => { | ||
const result = await new Promise((resolve, reject) => { | ||
let result: ExecutionResult<Record<string, unknown>, unknown> | ||
unsubscribe = client.subscribe( | ||
{ | ||
query: '{ hello }', | ||
}, | ||
{ | ||
next: (data) => (result = data), | ||
error: reject, | ||
complete: () => resolve(result), | ||
}, | ||
) | ||
}) | ||
|
||
expect(result).toEqual({ data: { hello: 'world' } }) | ||
}) | ||
it('handle subscriptions', async () => { | ||
const onNext = jest.fn() | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
unsubscribe = client.subscribe( | ||
{ | ||
query: 'subscription { greetings }', | ||
}, | ||
{ | ||
next: onNext, | ||
error: reject, | ||
complete: resolve, | ||
}, | ||
) | ||
}) | ||
|
||
expect(onNext).toBeCalledTimes(5) // we say "Hi" in 5 languages | ||
}) | ||
}) | ||
}) |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,86 @@ | ||
import { ExecutionResult } from 'graphql' | ||
import { isAsyncIterable } from '@envelop/core' | ||
|
||
import { getResponseInitByRespectingErrors } from '../../error.js' | ||
import { FetchAPI, MaybeArray } from '../../types.js' | ||
import { ResultProcessor, ResultProcessorInput } from '../types.js' | ||
import { jsonStringifyResultWithoutInternals } from './stringify.js' | ||
|
||
export interface SSEProcessorOptions { | ||
legacySSE: boolean | ||
} | ||
|
||
export function getSSEProcessor(opts: SSEProcessorOptions): ResultProcessor { | ||
return function processSSEResult( | ||
result: ResultProcessorInput, | ||
fetchAPI: FetchAPI, | ||
): Response { | ||
let pingIntervalMs = 12_000 | ||
|
||
// for testing the pings, reduce the timeout | ||
if (globalThis.process?.env?.NODE_ENV === 'test') { | ||
pingIntervalMs = 300 | ||
} | ||
|
||
const headersInit = { | ||
'Content-Type': 'text/event-stream', | ||
Connection: 'keep-alive', | ||
'Cache-Control': 'no-cache', | ||
'Content-Encoding': 'none', | ||
} | ||
|
||
const responseInit = getResponseInitByRespectingErrors(result, headersInit) | ||
|
||
let iterator: AsyncIterator<MaybeArray<ExecutionResult>> | ||
|
||
let pingInterval: number | ||
const textEncoder = new fetchAPI.TextEncoder() | ||
const readableStream = new fetchAPI.ReadableStream({ | ||
start(controller) { | ||
// ping client every 12 seconds to keep the connection alive | ||
pingInterval = setInterval(() => { | ||
if (!controller.desiredSize) { | ||
clearInterval(pingInterval) | ||
return | ||
} | ||
controller.enqueue(textEncoder.encode(':\n\n')) | ||
}, pingIntervalMs) as unknown as number | ||
|
||
if (isAsyncIterable(result)) { | ||
iterator = result[Symbol.asyncIterator]() | ||
} else { | ||
let finished = false | ||
iterator = { | ||
next: () => { | ||
if (finished) { | ||
return Promise.resolve({ done: true, value: null }) | ||
} | ||
finished = true | ||
return Promise.resolve({ done: false, value: result }) | ||
}, | ||
} | ||
} | ||
}, | ||
async pull(controller) { | ||
const { done, value } = await iterator.next() | ||
if (value != null) { | ||
if (!opts.legacySSE) { | ||
controller.enqueue(textEncoder.encode(`event: next\n`)) | ||
} | ||
const chunk = jsonStringifyResultWithoutInternals(value) | ||
controller.enqueue(textEncoder.encode(`data: ${chunk}\n\n`)) | ||
} | ||
if (done) { | ||
controller.enqueue(textEncoder.encode(`event: complete\n\n`)) | ||
clearInterval(pingInterval) | ||
controller.close() | ||
} | ||
}, | ||
async cancel(e) { | ||
clearInterval(pingInterval) | ||
await iterator.return?.(e) | ||
}, | ||
}) | ||
return new fetchAPI.Response(readableStream, responseInit) | ||
} | ||
} |
Oops, something went wrong.