question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fastify example - cannot set cookies

See original GitHub issue

Hi there,

I am trying out the Fastify example in combination with @fastify/session, basically the equivalent to express-session. This package sets a cookie on the browser whenever I set a session. However, the cookie does not get set through GraphiQL. Most likely because of the res.raw that is being used to send the actual result.

Whenever I create a normal endpoint and set a session, the cookie does get set. So it must be something to do with the /graphql endpoint.

I am not too sure if this is an issue with graphql-helix or with Fastify. I would assume that it has something to do with the sendResult(result, res.raw) which sends the result without any cookies or something.

Not quite sure, hope someone is able to help me out. Thanks!

Edit: I have also created an issue inside the Fastify repository to make sure that this is not an issue related to graphql-helix and could be expected behaviour when using res.raw.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
n1ru4lcommented, Jan 11, 2022

@gino https://github.com/contra/graphql-helix/pull/228 Can you try this?

https://github.com/contra/graphql-helix/pull/228#issuecomment-1008959477

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 😇

1reaction
n1ru4lcommented, Jan 10, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fastify example - cannot set cookies · Issue #227 - GitHub
This package sets a cookie on the browser whenever I set a session. However, the cookie does not get set through GraphiQL. Most...
Read more >
Unable to set cookie from node server -> app - Stack Overflow
I'm trying to set an httpOnly cookie from my node.js api (localhost:3001) to work with my react client app (localhost:3000), everything I've ...
Read more >
@fastify/cookie - npm
Plugin for fastify to add support for cookies. ... Start using @fastify/cookie in your project by running `npm i @fastify/cookie`.
Read more >
Cookies | NestJS - A progressive Node.js framework
import fastifyCookie from '@fastify/cookie'; // somewhere in your initialization file const app = await NestFactory.create<NestFastifyApplication>( ...
Read more >
Reply - Fastify
This is done to avoid parsing the set-cookie header when added to a reply ... Example (no reply.code() call) sets status code to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found