Skip to content

Commit 6f2d94c

Browse files
authored
Hive Gateway dispose after response in Cloudflare Workers (#6846)
1 parent a26cf60 commit 6f2d94c

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

packages/web/docs/src/content/api-reference/gateway-config.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import CloudflareKVCacheStorage from '@graphql-mesh/cache-cfw-kv'
8282
import supergraph from './my-remote-supergraph-config'
8383

8484
export default {
85-
fetch(request, env, ctx) {
85+
async fetch(request, env, ctx) {
8686
const gateway = createGatewayRuntime({
8787
supergraph,
8888
pollingInterval: 5_000, // TTL in milliseconds of the remote supergraph
@@ -92,8 +92,9 @@ export default {
9292
namespace: env.NAMESPACE
9393
})
9494
})
95+
const response = await gateway(request, env, ctx)
9596
ctx.waitUntil(gateway[Symbol.asyncDispose]())
96-
return gateway(request, env, ctx)
97+
return response
9798
}
9899
}
99100
```

packages/web/docs/src/content/gateway/deployment/serverless/cloudflare-workers.mdx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,25 @@ See [Bundling Problems](/docs/gateway/deployment/serverless#bundling-problem) fo
1818
how to load the supergraph and `transports` option.
1919

2020
In the following example, we showcase how Hive Gateway can be used in a serverless environment with
21-
Cloudflare Workers. Notice how the implementation leverages the `using` sytax for
22-
[Explicit Resource Management](https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/#explicit-resource-management),
23-
this makes sure that the Hive Gateway is gracefully and properly disposed before completing the
24-
worker.
21+
Cloudflare Workers.
2522

2623
```ts filename="index.js"
2724
import { createGatewayRuntime } from '@graphql-hive/gateway-runtime'
2825
import http from '@graphql-mesh/transport-http'
2926
import supergraph from './supergraph'
3027

3128
export default {
32-
fetch(request, env, ctx) {
29+
async fetch(request, env, ctx) {
3330
const gateway = createGatewayRuntime({
3431
// All options available in `gateway.config.ts` configuration can also be passed here.
3532
supergraph,
3633
transports: {
3734
http // http transport is required for subgraphs using standard GraphQL over HTTP.
3835
}
3936
})
37+
const response = await gateway(request, env, ctx)
4038
ctx.waitUntil(gateway[Symbol.asyncDispose]())
41-
return gateway(request, env, ctx)
39+
return response
4240
}
4341
}
4442
```
@@ -50,6 +48,11 @@ export default /* GraphQL */ `
5048
```
5149

5250
<Callout>
51+
The example above does not support streaming responses because Hive Gateway is being disposed
52+
immediatelly after assembling a response with `ctx.waitUntil(gateway[Symbol.asyncDispose]())`.
53+
</Callout>
54+
55+
<Callout type="info">
5356
If you want to use [Cloudflare KV
5457
Cache](https://developers.cloudflare.com/workers/runtime-apis/kv) as a distributed cache, [see
5558
here for Hive Gateway integration](/docs/gateway/other-features/performance#cloudflare-workers-kv)

packages/web/docs/src/content/gateway/other-features/performance/index.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ import CloudflareKVCacheStorage from '@graphql-mesh/cache-cfw-kv'
148148
import supergraph from './my-remote-supergraph-config'
149149

150150
export default {
151-
fetch(request, env, ctx) {
151+
async fetch(request, env, ctx) {
152152
const gateway = createGatewayRuntime({
153153
supergraph,
154154
transports: { http },
@@ -160,8 +160,9 @@ export default {
160160
namespace: env.NAMESPACE
161161
})
162162
})
163+
const response = await gateway(request, env, ctx)
163164
ctx.waitUntil(gateway[Symbol.asyncDispose]())
164-
return gateway(request, env, ctx)
165+
return response
165166
}
166167
}
167168
```

0 commit comments

Comments
 (0)