Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase default body-parser limit for standalone server #7172

Merged
merged 6 commits into from Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-ligers-impress.md
@@ -0,0 +1,5 @@
---
'@apollo/server': patch
---

Restore body-parser limit of standalone server to 50mb (as it was in the `apollo-server` package)
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
@@ -1,6 +1,6 @@
import { ApolloServer } from '../..';
import { startStandaloneServer } from '../../standalone';
import { describe, it } from '@jest/globals';
import { describe, it, expect } from '@jest/globals';

describe('Typings: TContext inference', () => {
it('correctly infers BaseContext when no `context` function is provided', async () => {
Expand Down Expand Up @@ -103,3 +103,41 @@ describe('Typings: TContext inference', () => {
await server.stop();
});
});

describe('Configuration', () => {
it('allows > 100KiB bodies to be sent (body-parser default)', async () => {
const server = new ApolloServer({
typeDefs: `type Query { hello: String }`,
resolvers: {
Query: {
hello: () => 'hello world!',
},
},
});

const { url } = await startStandaloneServer(server, {
listen: { port: 0 },
});

const excessivelyLargeBody = JSON.stringify({
query: `{hello}`,
variables: Object.fromEntries([...Array(7960)].map((_, i) => [i, 'foo'])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GraphQL doesn't require all variable values to match declared variables, but it's maybe a little awkward that these variables don't even have valid names (they start with a digit)... I mean, not "wrong" but you could also make it actually valid pretty easily (can just be one very long string or one very long array of ints too).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

// 100kib limit = 102400 bytes
expect(Buffer.byteLength(excessivelyLargeBody)).toBe(102403);

const result = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: excessivelyLargeBody,
});
const { data } = await result.json();

expect(data.hello).toEqual('hello world!');

await server.stop();
});
});
6 changes: 5 additions & 1 deletion packages/server/src/standalone/index.ts
Expand Up @@ -56,7 +56,11 @@ export async function startStandaloneServer<TContext extends BaseContext>(
await server.start();

const context = options?.context ?? (async () => ({} as TContext));
app.use(cors(), bodyParser.json(), expressMiddleware(server, { context }));
app.use(
cors(),
bodyParser.json({ limit: '50mb' }),
expressMiddleware(server, { context }),
);

const listenOptions = options?.listen ?? { port: 4000 };
// Wait for server to start listening
Expand Down