-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v1] close subscriptions on disposal and schema change with different…
… codes (#7220)
- Loading branch information
Showing
10 changed files
with
369 additions
and
161 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,35 @@ | ||
--- | ||
'@graphql-mesh/serve-runtime': patch | ||
--- | ||
|
||
Close subscriptions on disposal and schema change with different codes. | ||
|
||
When the server gets disposed (on shutdown), all active subscriptions will complete emitting the following execution error: | ||
|
||
```json | ||
{ | ||
"errors": [ | ||
{ | ||
"extensions": { | ||
"code": "SHUTTING_DOWN", | ||
}, | ||
"message": "subscription has been closed because the server is shutting down", | ||
}, | ||
], | ||
} | ||
``` | ||
|
||
However, when the server detects a schema change, all active subscriptions will complete emitting the following execution error: | ||
|
||
```json | ||
{ | ||
"errors": [ | ||
{ | ||
"extensions": { | ||
"code": "SUBSCRIPTION_SCHEMA_RELOAD", | ||
}, | ||
"message": "subscription has been closed due to a schema reload", | ||
}, | ||
], | ||
} | ||
``` |
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
45 changes: 45 additions & 0 deletions
45
packages/serve-runtime/src/useCompleteSubscriptionsOnSchemaChange.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,45 @@ | ||
import { createGraphQLError, isAsyncIterable, Repeater } from 'graphql-yoga'; | ||
import type { MeshServePlugin } from './types'; | ||
|
||
export function useCompleteSubscriptionsOnSchemaChange(): MeshServePlugin { | ||
const activeSubs: (() => void)[] = []; | ||
return { | ||
onSchemaChange() { | ||
while (activeSubs.length) { | ||
activeSubs.pop()?.(); | ||
} | ||
}, | ||
onSubscribe() { | ||
return { | ||
onSubscribeResult({ result, setResult }) { | ||
if (isAsyncIterable(result)) { | ||
setResult( | ||
Repeater.race([ | ||
result, | ||
new Repeater((_push, stop) => { | ||
function complete() { | ||
stop( | ||
createGraphQLError('subscription has been closed due to a schema reload', { | ||
extensions: { | ||
code: 'SUBSCRIPTION_SCHEMA_RELOAD', | ||
}, | ||
}), | ||
); | ||
} | ||
activeSubs.push(complete); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
stop.then(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
result.return?.(); | ||
activeSubs.splice(activeSubs.indexOf(complete), 1); | ||
}); | ||
}), | ||
]), | ||
); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/serve-runtime/src/useCompleteSubscriptionsOnUnifiedGraphDispose.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,38 @@ | ||
import { createGraphQLError, isAsyncIterable, Repeater } from 'graphql-yoga'; | ||
import type { MeshServePlugin } from './types'; | ||
|
||
export function useCompleteSubscriptionsOnUnifiedGraphDispose( | ||
onDispose: (cb: () => void) => void, | ||
): MeshServePlugin { | ||
return { | ||
onSubscribe() { | ||
return { | ||
onSubscribeResult({ result, setResult }) { | ||
if (isAsyncIterable(result)) { | ||
setResult( | ||
Repeater.race([ | ||
result, | ||
new Repeater((_push, stop) => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
stop.then(() => result.return?.()); | ||
onDispose(() => { | ||
stop( | ||
createGraphQLError( | ||
'subscription has been closed because the server is shutting down', | ||
{ | ||
extensions: { | ||
code: 'SHUTTING_DOWN', | ||
}, | ||
}, | ||
), | ||
); | ||
}); | ||
}), | ||
]), | ||
); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.