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

Cannot separate route configs for POST and GET requests #4967

Closed
richiejd opened this issue Feb 27, 2021 · 5 comments
Closed

Cannot separate route configs for POST and GET requests #4967

richiejd opened this issue Feb 27, 2021 · 5 comments

Comments

@richiejd
Copy link

richiejd commented Feb 27, 2021

This git issue was closed (incorrectly?). The reason it's an issue is because of the referenced.
#3354

This came up for us when trying to change the max payload size:

  await apolloServer.applyMiddleware({
    app: hapiServer,
    path: pathname,
    route: {
      cors: true,
      payload: { maxBytes: MAX_BYTES },
    },
  });

You cannot set payload on route because the config is used for both the GET and POST requests.
GET requests cannot have payload config.
The plugin should allow specifying different route config for the GET and POST requests.

@glasser
Copy link
Member

glasser commented Mar 1, 2021

That makes sense.

There's not a lot of hapi expertise on the Apollo Server core team nor would we be QAing any hapi changes in a real production environment, so this is the kind of change I'd be happy to review if it came in as a PR.

@champagnetony
Copy link

champagnetony commented Apr 29, 2021

I haven't written any tests but here's how I "fixed" this and did some testing with it. The live testing did allow larger byte sizes
Maybe this will help someone get an actual PR with tests completed.

const handler = async (request, h) => {
        try {
            const { graphqlResponse, responseInit } = await runHttpQuery(
                [request, h],
                {
                    method: request.method.toUpperCase(),
                    options: options.graphqlOptions,
                    query:
                        request.method === 'post'
                            ? // TODO type payload as string or Record
                            (request.payload as any)
                            : request.query,
                    request: convertNodeHttpToRequest(request.raw.req),
                },
            );

            const response = h.response(graphqlResponse);
            Object.keys(responseInit.headers).forEach(key =>
                response.header(key, responseInit.headers[key]),
            );
            return response;
        } catch (error) {
            if ('HttpQueryError' !== error.name) {
                throw Boom.boomify(error);
            }

            if (true === error.isGraphQLError) {
                const response = h.response(error.message);
                response.code(error.statusCode);
                response.type('application/json');
                return response;
            }

            const err = new Boom(error.message, { statusCode: error.statusCode });
            if (error.headers) {
                Object.keys(error.headers).forEach(header => {
                    err.output.headers[header] = error.headers[header];
                });
            }
            // Boom hides the error when status code is 500
            err.output.payload.message = error.message;
            throw err;
        }
    };
    server.route({
      method: ['POST'],
      path: options.path || '/graphql',
      vhost: options.vhost || undefined,
      options: options.route || {},
      handler
    });
    if (options.route && options.route.payload) {
        delete options.route.payload;
    }
    server.route({
      method: ['GET'],
      path: options.path || '/graphql',
      vhost: options.vhost || undefined,
      options: options.route || {},
      handler
    });
    

arimus added a commit to arimus/apollo-server that referenced this issue Nov 28, 2021
arimus added a commit to arimus/apollo-server that referenced this issue Nov 28, 2021
arimus added a commit to arimus/apollo-server that referenced this issue Nov 28, 2021
arimus added a commit to arimus/apollo-server that referenced this issue Nov 28, 2021
@arimus
Copy link
Contributor

arimus commented Nov 28, 2021

@champagnetony I've created a pull request. Thanks for the pointers.

@glasser I've created a PR to address this ticket and I've validated that it works with a test and actually using it in my project successfully. Let me know if you need anything else, like more robust tests, etc. I did the minimal needed to prove that payload options would make themselves into the POST route configuration and not the GET ones. Not sophisticated, but it allows some more robust configuration in the HAPI configuration simply.

p.s. I have a v2 back port of the same if anyone is really interested. Didn't want to go through the PR hassle unless anyone actually cared though.

arimus added a commit to arimus/apollo-server that referenced this issue Dec 2, 2021
arimus added a commit to arimus/apollo-server that referenced this issue Dec 2, 2021
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 12, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus added a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus pushed a commit to arimus/apollo-server that referenced this issue Feb 26, 2022
arimus pushed a commit to arimus/apollo-server that referenced this issue Jun 25, 2022
@trevor-scheer
Copy link
Member

Looks like the person who took interest in resolving this issue (@arimus) is also the person who's currently working on the AS 4 Hapi integration. I recommend keeping an eye out or collaborating with them on the v4 integration.

@trevor-scheer trevor-scheer closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2022
@arimus
Copy link
Contributor

arimus commented Nov 5, 2022

For anyone stumbling onto this issue, the integration can now be found here: https://github.com/apollo-server-integrations/apollo-server-integration-hapi/

It's still a work in progress. Tests are passing, but there is some needed configuration support necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 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

5 participants