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

GraphQL SSE Distinct Connections support #2445

Merged
merged 9 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-teachers-sin.md
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
ardatan marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions examples/fastify/__integration-tests__/fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ describe('fastify example integration', () => {

data: {"data":{"countdown":0}}

event: complete

"
`)
})
Expand Down Expand Up @@ -202,6 +204,8 @@ describe('fastify example integration', () => {

data: {"data":{"countdown":0}}

event: complete

"
`)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('graphql-auth example integration', () => {
for await (const chunk of response.body!) {
const chunkString = Buffer.from(chunk).toString('utf-8')
if (chunkString.includes('data:')) {
expect(chunkString.trim()).toBe('data: {"data":{"public":"hi"}}')
expect(chunkString.trim()).toContain('data: {"data":{"public":"hi"}}')
break
}
}
Expand All @@ -91,7 +91,7 @@ describe('graphql-auth example integration', () => {
for await (const chunk of response.body!) {
const chunkStr = Buffer.from(chunk).toString('utf-8')
if (chunkStr.startsWith('data:')) {
expect(chunkStr.trim()).toBe(
expect(chunkStr.trim()).toContain(
'data: {"data":{"requiresAuth":"hi foo@foo.com"}}',
)
break
Expand All @@ -112,7 +112,7 @@ describe('graphql-auth example integration', () => {
for await (const chunk of response.body!) {
const chunkStr = Buffer.from(chunk).toString('utf-8')
if (chunkStr.startsWith('data:')) {
expect(chunkStr.trim()).toBe(
expect(chunkStr.trim()).toContain(
'data: {"data":null,"errors":[{"message":"Accessing \'Subscription.requiresAuth\' requires authentication.","locations":[{"line":1,"column":14}]}]}',
)
break
Expand Down
2 changes: 2 additions & 0 deletions examples/hapi/__integration-tests__/hapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ describe('hapi example integration', () => {

data: {"data":{"greetings":"Zdravo"}}

event: complete

"
`)
})
Expand Down
2 changes: 2 additions & 0 deletions examples/node-ts/__integration-tests__/node-ts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('node-ts example integration', () => {
expect(await response.text()).toMatchInlineSnapshot(`
"data: {"errors":[{"message":"Subscriptions have been disabled"}]}

event: complete

"
`)
})
Expand Down
2 changes: 2 additions & 0 deletions examples/pothos/__integration-tests__/pothos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('pothos example integration', () => {

data: {"data":{"greetings":"Zdravo"}}

event: complete

"
`)
})
Expand Down
188 changes: 188 additions & 0 deletions packages/graphql-yoga/__tests__/graphql-sse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { createSchema, createYoga } from '../src/index.js'
import { createClient } from 'graphql-sse'

describe('GraphQL over SSE', () => {
const schema = createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String!
}
type Subscription {
greetings: String!
waitForPings: String!
}
`,
resolvers: {
Query: {
async hello() {
return 'world'
},
},
Subscription: {
greetings: {
async *subscribe() {
for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) {
yield { greetings: hi }
}
},
},
waitForPings: {
// eslint-disable-next-line require-yield
async *subscribe() {
// a ping is issued every 300ms, wait for a few and just return
await new Promise((resolve) => setTimeout(resolve, 300 * 3 + 100))
return
},
},
},
},
})

const yoga = createYoga({
schema,
legacySse: false,
maskedErrors: false,
})

describe('Distinct connections mode', () => {
test('should issue pings while connected', async () => {
const res = await yoga.fetch(
'http://yoga/graphql?query=subscription{waitForPings}',
{
headers: {
accept: 'text/event-stream',
},
},
)
expect(res.ok).toBeTruthy()
await expect(res.text()).resolves.toMatchInlineSnapshot(`
":

:

:

event: complete

"
`)
})

it('should support single result operations', async () => {
const client = createClient({
url: 'http://yoga/graphql',
fetchFn: yoga.fetch,
abortControllerImpl: yoga.fetchAPI.AbortController,
singleConnection: false, // distinct connection mode
retryAttempts: 0,
})

await expect(
new Promise((resolve, reject) => {
let result: unknown
client.subscribe(
{
query: /* GraphQL */ `
{
hello
}
`,
},
{
next: (msg) => (result = msg),
error: reject,
complete: () => resolve(result),
},
)
}),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"hello": "world",
},
}
`)

client.dispose()
})

it('should support streaming operations', async () => {
const client = createClient({
url: 'http://yoga/graphql',
fetchFn: yoga.fetch,
abortControllerImpl: yoga.fetchAPI.AbortController,
singleConnection: false, // distinct connection mode
retryAttempts: 0,
})

await expect(
new Promise((resolve, reject) => {
const msgs: unknown[] = []
client.subscribe(
{
query: /* GraphQL */ `
subscription {
greetings
}
`,
},
{
next: (msg) => msgs.push(msg),
error: reject,
complete: () => resolve(msgs),
},
)
}),
).resolves.toMatchInlineSnapshot(`
[
{
"data": {
"greetings": "Hi",
},
},
{
"data": {
"greetings": "Bonjour",
},
},
{
"data": {
"greetings": "Hola",
},
},
{
"data": {
"greetings": "Ciao",
},
},
{
"data": {
"greetings": "Zdravo",
},
},
]
`)

client.dispose()
})

it('should report errors through the stream', async () => {
const res = await yoga.fetch('http://yoga/graphql?query={nope}', {
headers: {
accept: 'text/event-stream',
},
})
expect(res.ok).toBeTruthy()
await expect(res.text()).resolves.toMatchInlineSnapshot(`
"event: next
data: {"errors":[{"message":"Cannot query field \\"nope\\" on type \\"Query\\".","locations":[{"line":1,"column":2}]}]}

event: complete

"
`)
})
})

it.todo('Single connections mode')
})
25 changes: 14 additions & 11 deletions packages/graphql-yoga/__tests__/subscriptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,24 @@ describe('Subscription', () => {
}

expect(results).toMatchInlineSnapshot(`
[
":
[
":

",
":

",
":
",
":

",
":
",
"data: {"data":{"hi":"hi"}}

",
"data: {"data":{"hi":"hi"}}
",
"event: complete

",
]
`)
",
]
`)
})

test('should issue pings event if event source never publishes anything', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"graphql": "^16.0.1",
"graphql-http": "^1.7.2",
"graphql-scalars": "1.20.4",
"graphql-sse": "2.0.0",
"html-minifier-terser": "7.1.0",
"json-bigint-patch": "0.0.8",
"puppeteer": "19.6.0"
Expand Down
78 changes: 0 additions & 78 deletions packages/graphql-yoga/src/plugins/resultProcessor/push.ts

This file was deleted.

Loading