Skip to content

Commit

Permalink
Disable any batch size when allowBatchedHttpRequests is disabled
Browse files Browse the repository at this point in the history
* Implement requested changes and update docs
  • Loading branch information
Stelios Kotanidis committed Oct 12, 2021
1 parent bf3bf21 commit 53c868b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
23 changes: 13 additions & 10 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,19 @@ An object containing configuration options for connecting Apollo Server to [Apol
</td>
</tr>

<tr>
<td>

##### `allowBatchedHttpRequests`

`Boolean`
</td>
<td>

Controls whether to allow [Batching Queries](../requests/#batching) in a single HTTP Request. Defaults to `true`. If the GraphQL Server has this flag set to `false` and a request comes in with more than one query, an error will be thrown.
</td>
</tr>


<tr>
<td colspan="2">
Expand Down Expand Up @@ -411,16 +424,6 @@ If this is set to any string value, use that value instead of the environment va
</td>
</tr>

##### `allowBatchedHttpRequests`

`Boolean`
</td>
<td>

Controls whether to allow batching multiple Queries in a single HTTP Request. Defaults to `true`. If the GraphQL Server has this flag set to `false` and a request comes in with more than one query, an error will be thrown.
</td>
</tr>

</tbody>
</table>

Expand Down
2 changes: 2 additions & 0 deletions docs/source/requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ You can send a batch of queries in a single POST request by providing a JSON-enc

If you send a batched request, Apollo Server responds with a corresponding array of GraphQL responses.

Allowing batched requests can be toggled through the [allowBatchedHttpRequests](../apollo-server/#allowbatchedhttprequests) flag in the Apollo Server Options.

## GET requests

Apollo Server also accepts GET requests for queries (but not mutations). With a GET request, query details (`query`, `operationName`, `variables`) are provided as URL query parameters. The `variables` option is a URL-escaped JSON object.
Expand Down
17 changes: 8 additions & 9 deletions packages/apollo-server-core/src/__tests__/runHttpQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ describe('runHttpQuery', () => {
options: {
debug: false,
schema,
schemaHash: generateSchemaHash(schema),
schemaHash: 'deprecated' as SchemaHash,
allowBatchedHttpRequests: false,
},
request: new MockReq(),
};

it('succeeds when there are multiple queries in the request', async () => {
it('succeeds when there are no batched queries in the request', async () => {
await expect(
runHttpQuery([], mockDisabledBatchQueryRequest),
).resolves.not.toThrow();
});

it('throws when there are multiple queries in the request', () => {
const multipleQueryRequest = Object.assign(
it('throws when there are batched queries in the request', () => {
const batchedQueryRequest = Object.assign(
{},
mockDisabledBatchQueryRequest,
{
Expand All @@ -95,15 +95,14 @@ describe('runHttpQuery', () => {
],
},
);
return runHttpQuery([], multipleQueryRequest).catch(
return runHttpQuery([], batchedQueryRequest).catch(
(err: HttpQueryError) => {
expect(err.statusCode).toEqual(500);
expect(err.statusCode).toEqual(400);
expect(err.message).toEqual(
JSON.stringify({
errors: [
{
message:
'GraphQL Query Batching is not allowed by Apollo Server, but the request contained multiple queries.',
message: 'Operation batching disabled.',
extensions: { code: 'INTERNAL_SERVER_ERROR' },
},
],
Expand All @@ -123,7 +122,7 @@ describe('runHttpQuery', () => {
options: {
debug: false,
schema,
schemaHash: generateSchemaHash(schema),
schemaHash: 'deprecated' as SchemaHash,
allowBatchedHttpRequests: true,
},
request: new MockReq(),
Expand Down
10 changes: 3 additions & 7 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,10 @@ export async function processHTTPRequest<TContext>(
parseGraphQLRequest(httpRequest.request, requestParams),
);

if (requests.length > 1 && options.allowBatchedHttpRequests === false) {
if (options.allowBatchedHttpRequests === false) {
return throwHttpGraphQLError(
500,
[
new Error(
'GraphQL Query Batching is not allowed by Apollo Server, but the request contained multiple queries.',
),
],
400,
[new Error('Operation batching disabled.')],
options,
);
}
Expand Down
22 changes: 10 additions & 12 deletions packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ export default ({
},
});

const req = request(app)
const res = await request(app)
.post('/graphql')
.send([
{
Expand All @@ -903,17 +903,15 @@ export default ({
operationName: 'testX',
},
]);
return req.then((res) => {
expect(res.status).toEqual(500);
expect(res.body).toEqual({
errors: [
{
message:
'GraphQL Query Batching is not allowed by Apollo Server, but the request contained multiple queries.',
extensions: { code: 'INTERNAL_SERVER_ERROR' },
},
],
});

expect(res.status).toEqual(400);
expect(res.body).toEqual({
errors: [
{
message: 'Operation batching disabled.',
extensions: { code: 'INTERNAL_SERVER_ERROR' },
},
],
});
});

Expand Down

0 comments on commit 53c868b

Please sign in to comment.