Skip to content

Commit

Permalink
feat(serve/legacy/http): allow extra params in body (#7183)
Browse files Browse the repository at this point in the history
* feat(serve/legacy/http): allow extra params in body

* chore(dependencies): updated changesets for modified dependencies

* Tests

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Jul 1, 2024
1 parent bd2814d commit 8a04cf7
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_fusion-runtime-7183-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/fusion-runtime": patch
---
dependencies updates:
- Updated dependency [`graphql-yoga@^5.6.0` ↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.6.0) (from `^5.3.0`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_http-7183-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/http": patch
---
dependencies updates:
- Updated dependency [`graphql-yoga@^5.6.0` ↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.6.0) (from `^5.3.0`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-response-cache": patch
---
dependencies updates:
- Updated dependency [`graphql-yoga@^5.6.0` ↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.6.0) (from `^5.1.1`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_serve-runtime-7183-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/serve-runtime": patch
---
dependencies updates:
- Updated dependency [`graphql-yoga@^5.6.0` ↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.6.0) (from `^5.3.0`, in `dependencies`)
30 changes: 30 additions & 0 deletions .changeset/flat-actors-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@graphql-mesh/types': patch
'@graphql-mesh/http': patch
---

By default, Mesh does not allow extra parameters in the request body other than `query`, `operationName`, `extensions`, and `variables`, then throws 400 HTTP Error.
This change adds a new option called `extraParamNames` to allow extra parameters in the request body.

```yaml
serve:
extraParamNames:
- extraParam1
- extraParam2
```
```ts
const res = await fetch('/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: 'query { __typename }',
extraParam1: 'value1',
extraParam2: 'value2',
}),
});

console.assert(res.status === 200);
```
2 changes: 1 addition & 1 deletion e2e/auto-type-merging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"@graphql-mesh/serve-cli": "workspace:*",
"@omnigraph/openapi": "workspace:*",
"graphql": "^16.8.1",
"graphql-yoga": "^5.3.0"
"graphql-yoga": "^5.6.0"
}
}
2 changes: 1 addition & 1 deletion e2e/type-merging-batching/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"@graphql-mesh/fusion-runtime": "workspace:*",
"@graphql-mesh/serve-cli": "workspace:*",
"graphql": "^16.8.1",
"graphql-yoga": "^5.3.0"
"graphql-yoga": "^5.6.0"
}
}
2 changes: 1 addition & 1 deletion examples/graphql-file-upload-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@graphql-mesh/graphql": "0.98.10",
"concurrently": "8.2.2",
"graphql": "16.9.0",
"graphql-yoga": "^5.3.0",
"graphql-yoga": "^5.6.0",
"sharp": "0.33.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/v1-next/hive-example/my-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"@graphql-hive/cli": "0.38.4",
"graphql": "16.9.0",
"graphql-yoga": "5.4.0",
"graphql-yoga": "5.6.0",
"tsx": "4.15.8"
}
}
2 changes: 1 addition & 1 deletion packages/fusion/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@graphql-tools/wrap": "^10.0.5",
"change-case": "^4.1.2",
"disposablestack": "^1.1.6",
"graphql-yoga": "^5.3.0",
"graphql-yoga": "^5.6.0",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
11 changes: 11 additions & 0 deletions packages/legacy/cli/src/commands/serve/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ type ServeConfig @md {
Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
"""
healthCheckEndpoint: String

"""
By default, GraphQL Mesh does not allow parameters in the request body except `query`, `variables`, `extensions`, and `operationName`.
This option allows you to specify additional parameters that are allowed in the request body.
@default []
@example ['doc_id', 'id']
"""
extraParamNames: [String]
}

union Port = Int | String
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@whatwg-node/server": "^0.9.34",
"graphql-yoga": "^5.3.0"
"graphql-yoga": "^5.6.0"
},
"devDependencies": {
"@types/lodash.get": "4.4.9",
Expand Down
3 changes: 3 additions & 0 deletions packages/legacy/http/src/graphqlHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const graphqlHandler = ({
corsConfig,
batchingLimit,
healthCheckEndpoint = '/healthcheck',
extraParamNames,
}: {
getBuiltMesh: () => Promise<MeshInstance>;
playgroundTitle: string;
Expand All @@ -18,6 +19,7 @@ export const graphqlHandler = ({
corsConfig: CORSOptions;
batchingLimit?: number;
healthCheckEndpoint?: string;
extraParamNames?: string[];
}) => {
const getYogaForMesh = memoize1(function getYogaForMesh(mesh: MeshInstance) {
return createYoga({
Expand All @@ -32,6 +34,7 @@ export const graphqlHandler = ({
},
}),
],
extraParamNames,
logging: mesh.logger,
maskedErrors: false,
graphiql: playgroundEnabled && {
Expand Down
2 changes: 2 additions & 0 deletions packages/legacy/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function createMeshHTTPHandler<TServerContext>({
healthCheckEndpoint = '/healthcheck',
// TODO
// trustProxy = 'loopback',
extraParamNames,
} = rawServeConfig;

getBuiltMesh()
Expand All @@ -50,6 +51,7 @@ export function createMeshHTTPHandler<TServerContext>({
graphqlEndpoint: graphqlPath,
corsConfig,
batchingLimit,
extraParamNames,
}),
{
plugins: [
Expand Down
71 changes: 71 additions & 0 deletions packages/legacy/http/test/http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ExecutionResult } from 'graphql';
import { createMeshHTTPHandler } from '@graphql-mesh/http';
import { MeshInstance } from '@graphql-mesh/runtime';
import { getTestMesh } from '../../testing/getTestMesh.js';
Expand Down Expand Up @@ -56,5 +57,75 @@ describe('http', () => {
const response = await httpHandler.fetch('http://localhost:4000/custom-health-check');
expect(response.status).toBe(200);
});
it('throws when unprovided extra parameters are given in the request', async () => {
await using mesh = await getTestMesh();
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => mesh,
});
const response = await httpHandler.fetch('http://localhost:4000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `query { __typename }`,
extraParam: 'extraParamValue',
}),
});
expect(response.status).toBe(400);
const result: ExecutionResult = await response.json();
expect(result.errors[0].message).toBe(
'Unexpected parameter "extraParam" in the request body.',
);
});
it('respects the extra parameters given in the config', async () => {
await using mesh = await getTestMesh();
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => mesh,
rawServeConfig: {
extraParamNames: ['extraParam'],
},
});
const response = await httpHandler.fetch('http://localhost:4000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `query { __typename }`,
extraParam: 'extraParamValue',
}),
});
expect(response.status).toBe(200);
const result: ExecutionResult = await response.json();
expect(result.data.__typename).toBe('Query');
});
it('throws when unprovided extra parameters are given in the request while there are provided some', async () => {
await using mesh = await getTestMesh();
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => mesh,
rawServeConfig: {
extraParamNames: ['extraParam'],
},
});
const response = await httpHandler.fetch('http://localhost:4000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `query { __typename }`,
extraParam1: 'extraParamValue',
}),
});
expect(response.status).toBe(400);
const result: ExecutionResult = await response.json();
expect(result.errors[0].message).toBe(
'Unexpected parameter "extraParam1" in the request body.',
);
});
});
});
8 changes: 8 additions & 0 deletions packages/legacy/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@
"healthCheckEndpoint": {
"type": "string",
"description": "Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)"
},
"extraParamNames": {
"type": "array",
"items": {
"type": "string"
},
"additionalItems": false,
"description": "By default, GraphQL Mesh does not allow parameters in the request body except `query`, `variables`, `extensions`, and `operationName`.\n\nThis option allows you to specify additional parameters that are allowed in the request body.\n\n@default []\n\n@example ['doc_id', 'id']"
}
}
},
Expand Down
10 changes: 10 additions & 0 deletions packages/legacy/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ export interface ServeConfig {
* Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
*/
healthCheckEndpoint?: string;
/**
* By default, GraphQL Mesh does not allow parameters in the request body except `query`, `variables`, `extensions`, and `operationName`.
*
* This option allows you to specify additional parameters that are allowed in the request body.
*
* @default []
*
* @example ['doc_id', 'id']
*/
extraParamNames?: string[];
}
/**
* Configuration for CORS
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@graphql-tools/utils": "10.2.2",
"@whatwg-node/fetch": "0.9.18",
"fets": "0.8.1",
"graphql-yoga": "5.4.0",
"graphql-yoga": "5.6.0",
"json-bigint-patch": "0.0.8"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/newrelic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"devDependencies": {
"@newrelic/test-utilities": "6.5.5",
"@types/newrelic": "9.14.4",
"graphql-yoga": "5.4.0",
"graphql-yoga": "5.6.0",
"newrelic": "10.6.2"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/response-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@envelop/response-cache": "^6.1.1",
"@graphql-mesh/string-interpolation": "0.5.4",
"@graphql-yoga/plugin-response-cache": "^3.1.1",
"graphql-yoga": "^5.1.1"
"graphql-yoga": "^5.6.0"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/serve-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@graphql-tools/utils": "^10.2.1",
"@whatwg-node/server": "^0.9.34",
"disposablestack": "^1.1.6",
"graphql-yoga": "^5.3.0"
"graphql-yoga": "^5.6.0"
},
"publishConfig": {
"access": "public",
Expand Down
9 changes: 8 additions & 1 deletion website/src/generated-markdown/ServeConfig.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ This feature can be disabled by passing `false` One of:
* `trustProxy` (type: `String`) - Configure Express Proxy Handling
[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)
* `healthCheckEndpoint` (type: `String`) - Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
* `healthCheckEndpoint` (type: `String`) - Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
* `extraParamNames` (type: `Array of String`) - By default, GraphQL Mesh does not allow parameters in the request body except `query`, `variables`, `extensions`, and `operationName`.

This option allows you to specify additional parameters that are allowed in the request body.

@default []

@example ['doc_id', 'id']
Loading

0 comments on commit 8a04cf7

Please sign in to comment.