|
| 1 | +import { createSchema, createYoga } from '../src/index.js' |
| 2 | +import { createClient } from 'graphql-sse' |
| 3 | + |
| 4 | +describe('GraphQL over SSE', () => { |
| 5 | + const schema = createSchema({ |
| 6 | + typeDefs: /* GraphQL */ ` |
| 7 | + type Query { |
| 8 | + hello: String! |
| 9 | + } |
| 10 | + type Subscription { |
| 11 | + greetings: String! |
| 12 | + waitForPings: String! |
| 13 | + } |
| 14 | + `, |
| 15 | + resolvers: { |
| 16 | + Query: { |
| 17 | + async hello() { |
| 18 | + return 'world' |
| 19 | + }, |
| 20 | + }, |
| 21 | + Subscription: { |
| 22 | + greetings: { |
| 23 | + async *subscribe() { |
| 24 | + for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) { |
| 25 | + yield { greetings: hi } |
| 26 | + } |
| 27 | + }, |
| 28 | + }, |
| 29 | + waitForPings: { |
| 30 | + // eslint-disable-next-line require-yield |
| 31 | + async *subscribe() { |
| 32 | + // a ping is issued every 300ms, wait for a few and just return |
| 33 | + await new Promise((resolve) => setTimeout(resolve, 300 * 3 + 100)) |
| 34 | + return |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | + |
| 41 | + const yoga = createYoga({ |
| 42 | + schema, |
| 43 | + legacySse: false, |
| 44 | + maskedErrors: false, |
| 45 | + }) |
| 46 | + |
| 47 | + describe('Distinct connections mode', () => { |
| 48 | + test('should issue pings while connected', async () => { |
| 49 | + const res = await yoga.fetch( |
| 50 | + 'http://yoga/graphql?query=subscription{waitForPings}', |
| 51 | + { |
| 52 | + headers: { |
| 53 | + accept: 'text/event-stream', |
| 54 | + }, |
| 55 | + }, |
| 56 | + ) |
| 57 | + expect(res.ok).toBeTruthy() |
| 58 | + await expect(res.text()).resolves.toMatchInlineSnapshot(` |
| 59 | + ": |
| 60 | +
|
| 61 | + : |
| 62 | +
|
| 63 | + : |
| 64 | +
|
| 65 | + event: complete |
| 66 | +
|
| 67 | + " |
| 68 | + `) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should support single result operations', async () => { |
| 72 | + const client = createClient({ |
| 73 | + url: 'http://yoga/graphql', |
| 74 | + fetchFn: yoga.fetch, |
| 75 | + abortControllerImpl: yoga.fetchAPI.AbortController, |
| 76 | + singleConnection: false, // distinct connection mode |
| 77 | + retryAttempts: 0, |
| 78 | + }) |
| 79 | + |
| 80 | + await expect( |
| 81 | + new Promise((resolve, reject) => { |
| 82 | + let result: unknown |
| 83 | + client.subscribe( |
| 84 | + { |
| 85 | + query: /* GraphQL */ ` |
| 86 | + { |
| 87 | + hello |
| 88 | + } |
| 89 | + `, |
| 90 | + }, |
| 91 | + { |
| 92 | + next: (msg) => (result = msg), |
| 93 | + error: reject, |
| 94 | + complete: () => resolve(result), |
| 95 | + }, |
| 96 | + ) |
| 97 | + }), |
| 98 | + ).resolves.toMatchInlineSnapshot(` |
| 99 | + { |
| 100 | + "data": { |
| 101 | + "hello": "world", |
| 102 | + }, |
| 103 | + } |
| 104 | + `) |
| 105 | + |
| 106 | + client.dispose() |
| 107 | + }) |
| 108 | + |
| 109 | + it('should support streaming operations', async () => { |
| 110 | + const client = createClient({ |
| 111 | + url: 'http://yoga/graphql', |
| 112 | + fetchFn: yoga.fetch, |
| 113 | + abortControllerImpl: yoga.fetchAPI.AbortController, |
| 114 | + singleConnection: false, // distinct connection mode |
| 115 | + retryAttempts: 0, |
| 116 | + }) |
| 117 | + |
| 118 | + await expect( |
| 119 | + new Promise((resolve, reject) => { |
| 120 | + const msgs: unknown[] = [] |
| 121 | + client.subscribe( |
| 122 | + { |
| 123 | + query: /* GraphQL */ ` |
| 124 | + subscription { |
| 125 | + greetings |
| 126 | + } |
| 127 | + `, |
| 128 | + }, |
| 129 | + { |
| 130 | + next: (msg) => msgs.push(msg), |
| 131 | + error: reject, |
| 132 | + complete: () => resolve(msgs), |
| 133 | + }, |
| 134 | + ) |
| 135 | + }), |
| 136 | + ).resolves.toMatchInlineSnapshot(` |
| 137 | + [ |
| 138 | + { |
| 139 | + "data": { |
| 140 | + "greetings": "Hi", |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "greetings": "Bonjour", |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + "data": { |
| 150 | + "greetings": "Hola", |
| 151 | + }, |
| 152 | + }, |
| 153 | + { |
| 154 | + "data": { |
| 155 | + "greetings": "Ciao", |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + "data": { |
| 160 | + "greetings": "Zdravo", |
| 161 | + }, |
| 162 | + }, |
| 163 | + ] |
| 164 | + `) |
| 165 | + |
| 166 | + client.dispose() |
| 167 | + }) |
| 168 | + |
| 169 | + it('should report errors through the stream', async () => { |
| 170 | + const res = await yoga.fetch('http://yoga/graphql?query={nope}', { |
| 171 | + headers: { |
| 172 | + accept: 'text/event-stream', |
| 173 | + }, |
| 174 | + }) |
| 175 | + expect(res.ok).toBeTruthy() |
| 176 | + await expect(res.text()).resolves.toMatchInlineSnapshot(` |
| 177 | + "event: next |
| 178 | + data: {"errors":[{"message":"Cannot query field \\"nope\\" on type \\"Query\\".","locations":[{"line":1,"column":2}]}]} |
| 179 | +
|
| 180 | + event: complete |
| 181 | +
|
| 182 | + " |
| 183 | + `) |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + it.todo('Single connections mode') |
| 188 | +}) |
0 commit comments