Skip to content

Commit

Permalink
Request Batching in the gateway level (#5633)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Jul 3, 2023
1 parent e5e2955 commit d0d4917
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changeset/early-onions-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-mesh/types': patch
'@graphql-mesh/http': patch
'@graphql-mesh/cli': patch
---

Request Batching on the gateway
4 changes: 4 additions & 0 deletions packages/cli/src/commands/serve/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type ServeConfig @md {
[Learn more](https://expressjs.com/en/guide/behind-proxies.html)
"""
trustProxy: String
"""
Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
"""
batchingLimit: Int
}

union Port = Int | String
Expand Down
23 changes: 16 additions & 7 deletions packages/http/src/graphqlHandler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { CORSOptions, createYoga, useLogger } from 'graphql-yoga';
import { MeshInstance } from '@graphql-mesh/runtime';

export const graphqlHandler = (
getBuiltMesh: () => Promise<MeshInstance>,
playgroundTitle: string,
playgroundEnabled: boolean,
graphqlEndpoint: string,
corsConfig: CORSOptions,
) => {
export const graphqlHandler = ({
getBuiltMesh,
playgroundTitle,
playgroundEnabled,
graphqlEndpoint,
corsConfig,
batchingLimit,
}: {
getBuiltMesh: () => Promise<MeshInstance>;
playgroundTitle: string;
playgroundEnabled: boolean;
graphqlEndpoint: string;
corsConfig: CORSOptions;
batchingLimit?: number;
}) => {
let yoga$: Promise<ReturnType<typeof createYoga>>;
return (request: Request, ctx: any) => {
if (!yoga$) {
Expand All @@ -32,6 +40,7 @@ export const graphqlHandler = (
cors: corsConfig,
graphqlEndpoint,
landingPage: false,
batching: batchingLimit ? { limit: batchingLimit } : false,
}),
);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ export function createMeshHTTPHandler<TServerContext>({
staticFiles,
playground: playgroundEnabled,
endpoint: graphqlPath = '/graphql',
batchingLimit,
// TODO
// trustProxy = 'loopback',
} = rawServeConfig;

return createServerAdapter<TServerContext>(
graphqlHandler(() => mesh$, playgroundTitle, playgroundEnabled, graphqlPath, corsConfig),
graphqlHandler({
getBuiltMesh: () => mesh$,
playgroundTitle,
playgroundEnabled,
graphqlEndpoint: graphqlPath,
corsConfig,
batchingLimit,
}),
{
plugins: [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
"trustProxy": {
"type": "string",
"description": "Configure Express Proxy Handling\n[Learn more](https://expressjs.com/en/guide/behind-proxies.html)"
},
"batchingLimit": {
"type": "integer",
"description": "Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export interface ServeConfig {
* [Learn more](https://expressjs.com/en/guide/behind-proxies.html)
*/
trustProxy?: string;
/**
* Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
*/
batchingLimit?: number;
}
/**
* Configuration for CORS
Expand Down
3 changes: 2 additions & 1 deletion website/src/generated-markdown/ServeConfig.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ This feature can be disabled by passing `false` One of:
* `Boolean`
* `playgroundTitle` (type: `String`) - Title of GraphiQL Playground
* `trustProxy` (type: `String`) - Configure Express Proxy Handling
[Learn more](https://expressjs.com/en/guide/behind-proxies.html)
[Learn more](https://expressjs.com/en/guide/behind-proxies.html)
* `batchingLimit` (type: `Int`) - Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
11 changes: 11 additions & 0 deletions website/src/pages/docs/guides/batching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ Mesh Gateway->>Book.author resolver: Query.stores[0].bookSells[0].book[6].author
Mesh Gateway->>Book.author resolver: Query.stores[0].bookSells[0].book[7].author
Book.author resolver->>Authors API: GetAuthors(input: \{ ids: ["123", "754", "332"] \})
```

# Request Batching on the gateway level

Mesh also provides a way to batch requests on the gateway level. This is useful when you want to
send multiple requests to the gateway in a single HTTP request. This follows
[Batching RFC](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)

```yaml
serve:
batchingLimit: 10 # You have to define an explicit limit for batching
```

0 comments on commit d0d4917

Please sign in to comment.