diff --git a/src/core/blueprint/error.rs b/src/core/blueprint/error.rs index 4eb88192df..4d4dd6337b 100644 --- a/src/core/blueprint/error.rs +++ b/src/core/blueprint/error.rs @@ -55,6 +55,9 @@ pub enum BlueprintError { #[error("Batching capability was used without enabling it in upstream")] IncorrectBatchingUsage, + #[error("batchKey requires either body or query parameters")] + BatchKeyRequiresEitherBodyOrQuery, + #[error("script is required")] ScriptIsRequired, diff --git a/src/core/blueprint/operators/http.rs b/src/core/blueprint/operators/http.rs index a4391f53c2..ab25a25ced 100644 --- a/src/core/blueprint/operators/http.rs +++ b/src/core/blueprint/operators/http.rs @@ -34,6 +34,12 @@ pub fn compile_http( .unit() .trace("query"), ) + .and( + Valid::<(), BlueprintError>::fail(BlueprintError::BatchKeyRequiresEitherBodyOrQuery) + .when(|| { + !http.batch_key.is_empty() && (http.body.is_none() && http.query.is_empty()) + }), + ) .and(Valid::succeed(http.url.as_str())) .zip(mustache_headers) .and_then(|(base_url, headers)| { diff --git a/tests/core/snapshots/batching-validation.md_error.snap b/tests/core/snapshots/batching-validation.md_error.snap index 23465b7898..7c03000416 100644 --- a/tests/core/snapshots/batching-validation.md_error.snap +++ b/tests/core/snapshots/batching-validation.md_error.snap @@ -3,6 +3,15 @@ source: tests/core/spec.rs expression: errors --- [ + { + "message": "batchKey requires either body or query parameters", + "trace": [ + "Query", + "posts", + "@http" + ], + "description": null + }, { "message": "Request body batching requires exactly one dynamic value in the body.", "trace": [ diff --git a/tests/core/snapshots/test-batch-operator-post.md_error.snap b/tests/core/snapshots/test-batch-operator-post.md_error.snap index 17ed6612a7..d7898cfbcf 100644 --- a/tests/core/snapshots/test-batch-operator-post.md_error.snap +++ b/tests/core/snapshots/test-batch-operator-post.md_error.snap @@ -4,12 +4,11 @@ expression: errors --- [ { - "message": "Request body batching requires exactly one dynamic value in the body.", + "message": "batchKey requires either body or query parameters", "trace": [ "Query", "user", - "@http", - "body" + "@http" ], "description": null } diff --git a/tests/execution/batching-validation.md b/tests/execution/batching-validation.md index 4be54ab075..208107efc9 100644 --- a/tests/execution/batching-validation.md +++ b/tests/execution/batching-validation.md @@ -14,6 +14,12 @@ type User { name: String } +type Post { + id: Int + title: String + body: String +} + type Query { user(id: Int!): User @http( @@ -22,6 +28,7 @@ type Query { body: {uId: "{{.args.id}}", userId: "{{.args.id}}"} batchKey: ["id"] ) + posts: [Post] @http(url: "http://jsonplaceholder.typicode.com/posts", batchKey: ["id"]) userWithId(id: Int!): User @http( url: "http://jsonplaceholder.typicode.com/users"