contra/graphql-helix

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

dmokel opened this issue · 1 comments

dmokel commented
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v16.13.0
  • module version: 8.1.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapijs/hapi
  • any other relevant information: no

Problem

import { schema } from "@/api/schema";
import qs from "qs";
import { getGraphQLParameters, processRequest, Request, sendResult } from "graphql-helix";

  const server = Hapi.server({
    port: 8777,
    query: {
      parser: (query) => qs.parse(query),
    },
    routes: {
      cors: {
        origin: ["*"]
      }
    }
  });

  server.route({
    method:"POST",
    path: "/graphql",
    handler: async (req, h) => {
      const request: Request = {
        headers: req.headers,
        method: req.method,
        query: req.query,
        body: req.payload,
      };

      const { operationName, query, variables } = getGraphQLParameters(request);

      const result = await processRequest({
        request,
        schema,
        operationName,
        query,
        variables,
      });

      await sendResult(result, req.raw.res);
    }
  })

  await server.start();
  console.log(`Serve on ${server.info.uri}`);

I meet the err: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client,
and I think this error comes from 【await sendResult(result, req.raw.res)】, Is there any solution to solve it?

dmokel commented
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v16.13.0
  • module version: 8.1.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapijs/hapi
  • any other relevant information: no

Problem

import { schema } from "@/api/schema";
import qs from "qs";
import { getGraphQLParameters, processRequest, Request, sendResult } from "graphql-helix";

  const server = Hapi.server({
    port: 8777,
    query: {
      parser: (query) => qs.parse(query),
    },
    routes: {
      cors: {
        origin: ["*"]
      }
    }
  });

  server.route({
    method:"POST",
    path: "/graphql",
    handler: async (req, h) => {
      const request: Request = {
        headers: req.headers,
        method: req.method,
        query: req.query,
        body: req.payload,
      };

      const { operationName, query, variables } = getGraphQLParameters(request);

      const result = await processRequest({
        request,
        schema,
        operationName,
        query,
        variables,
      });

      await sendResult(result, req.raw.res);
    }
  })

  await server.start();
  console.log(`Serve on ${server.info.uri}`);

I meet the err: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, and I think this error comes from 【await sendResult(result, req.raw.res)】, Is there any solution to solve it?

replace

await sendResult(result, req.raw.res);

with

sendResult(result, req.raw.res);
return h.abandon;

can solve the problem.