Giter Site home page Giter Site logo

Comments (5)

n1ru4l avatar n1ru4l commented on June 3, 2024 1

We are planning to move away from using response.raw for helix, by exposing a Node.js compatible stream API that can be consumed by fastify. https://github.com/fastify/fastify/blob/main/lib/reply.js#L142-L147

Then you can use any fastify plugin with helix!

from graphql-helix.

n1ru4l avatar n1ru4l commented on June 3, 2024 1

@gino #228 Can you try this?

#228 (comment)

import fastify from "fastify";
import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL } from "graphql-helix";
import { toResponsePayload } from "graphql-helix/to-response-payload";
import { toReadable } from "graphql-helix/node/to-readable";
import { schema } from "./schema";
const app = fastify();
app.route({
  method: ["GET", "POST"],
  url: "/graphql",
  async handler(req, reply) {
    const request = {
      body: req.body,
      headers: req.headers,
      method: req.method,
      query: req.query,
    };
    if (shouldRenderGraphiQL(request)) {
      reply.type("text/html");
      reply.send(renderGraphiQL({}));
    } else {
      const request = {
        body: req.body,
        headers: req.headers,
        method: req.method,
        query: req.query,
      };
      const { operationName, query, variables } = getGraphQLParameters(request);
      const result = await processRequest({
        operationName,
        query,
        variables,
        request,
        schema,
      });
      const responsePayload = toResponsePayload(result);
      reply.status(responsePayload.status);
      reply.headers(responsePayload.headers);
      reply.send(toReadable(responsePayload.source));
    }
  },
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
  console.log(`GraphQL server is running on port ${port}.`);
});

It is working fine for me 😇

from graphql-helix.

gino avatar gino commented on June 3, 2024

Apparently the fastify session plugin is not supported in this case as it bypasses fastify by using res.raw, as being said in this issue.

from graphql-helix.

gino avatar gino commented on June 3, 2024

@n1ru4l Oh that's great, would love to stick with Helix!

from graphql-helix.

gino avatar gino commented on June 3, 2024

@n1ru4l Thank you for the example! I have just tried it and according to this comment, I changed the example a little bit to make it use Readable.from(). It looked like a cookie was getting set. However, I immediately received an error in my console:
Cannot set headers after they are sent to the client, which makes my Fastify server unreachable.

I tried to chain the reply function, which didn't help with that error unfortunately:

res
  .headers(responsePayload.headers)
  .status(responsePayload.status)
  .send(Readable.from(responsePayload.source));

It looked like it worked until I refreshed the page again, which gave me the error again. Not quite sure why.

Edit: I figured out that the headers error has something to do with my custom session store which I used in combination with the fastify-session plugin. I just tried your example again without my session store and everything works fine!

from graphql-helix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.