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

Buggy: apollo-server-micro can't process request(limit > 1mb) #792

Closed
wansj opened this issue Jan 26, 2018 · 3 comments
Closed

Buggy: apollo-server-micro can't process request(limit > 1mb) #792

wansj opened this issue Jan 26, 2018 · 3 comments

Comments

@wansj
Copy link

wansj commented Jan 26, 2018

The source code of microGraphql in apollo-server-micro is as follow:

export function microGraphql(options: GraphQLOptions | MicroGraphQLOptionsFunction): RequestHandler {
  if (!options) {
    throw new Error('Apollo Server requires options.');
  }

  if (arguments.length > 1) {
    throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`);
  }

  return async function (req: IncomingMessage, res: ServerResponse) {
    let query;
    if (req.method === 'POST') {
      try {
        query = await json(req);
      } catch (err) {
        query = undefined;
      }
    } else {
      query = url.parse(req.url, true).query;
    }

It uses the json body parser to parse the request. The json parser can only process the request length shorter than 1mb by default. When I uploaded a file(larger than 1mb), the program stopped executation at the line of query = await json(req)
How to overcome this obstacle?

@wansj
Copy link
Author

wansj commented Jan 26, 2018

Someone had issued a PR to fix it #677

@wansj wansj closed this as completed Jan 26, 2018
@SanderVreeken
Copy link

Could you please tell me whether there is a way to make this work using the apollo-server-micro framework?

@wansj
Copy link
Author

wansj commented Mar 6, 2021

I solved this problem as this:
try {
await json(req, {limit: '1gb'})
} catch (e) {
throw e
}
return microGraphql({
schema,
context: {
db
}
})(req, res)
As you can see, I call json(req, {limit: '1gb'}) before calling the microGraphql method. The trick is json will cache the content it has prased in an inner WeakMap, thus when the microGraphql method call the json method again in its body, the content in the caching WeakMap will be returned.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants