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

Cookies does not work with Pothos GraphQL & Bun #3114

Open
madsbuch opened this issue Nov 24, 2023 · 6 comments · May be fixed by #3129
Open

Cookies does not work with Pothos GraphQL & Bun #3114

madsbuch opened this issue Nov 24, 2023 · 6 comments · May be fixed by #3129
Labels
kind/docs Improvements or additions to documentation

Comments

@madsbuch
Copy link

madsbuch commented Nov 24, 2023

Describe the bug

Using Yogi and Pothos with the recommended cookie plugin from @whatwg-node/server-plugin-cookies gives two bugs:

  1. Cookies er not being set in the browser
  2. Cookies are not being flushed on the server (and hence shared between requests)

To reproduce:

  1. Start a server using the snippet below
  2. run mutation:
    mutation {
      set(value: "test 123456")
    }
  3. Restart server
  4. Observe that the following query fails:
    query {
      get
    }

Note: If the server is not being restarted, the cookies will stay in all subsequent requests. (we use cookies to store JWTs so that part os obviously critical).

From my perspective it looks like a cache that is not being properly flushed to the client.

The test setup:

import { YogaInitialContext, createYoga } from "graphql-yoga";
import SchemaBuilder from "@pothos/core";
import AuthzPlugin from "@pothos/plugin-authz";
import { useCookies } from "@whatwg-node/server-plugin-cookies";

const builder = new SchemaBuilder<{
    Context: YogaInitialContext;
}>({
    plugins: [
        AuthzPlugin,
    ],
});

builder.mutationType();
builder.queryType();

builder.queryField("get", t =>
    t.string({
        resolve: async (p, a, c, i) => {
            const cookieVal = (await c.request.cookieStore?.get("test"))?.value 
            if (cookieVal === undefined) {
                throw new Error("No cookie set")
            }
            return cookieVal
        }
    })
)
builder.mutationField("set", t => t.string({
    args: {
        value: t.arg.string({ required: true })
    },
    resolve: async (p, a, c, i) => {
        await c.request.cookieStore?.set("test", a.value)
        return "OK"
    }
}))

const yoga = createYoga({
    schema: builder.toSchema(),
    plugins: [useCookies()]
})

const server = Bun.serve({
    fetch: yoga,
    port: process.env.PORT,
    hostname: "0.0.0.0",
  });

Your Example Website or App

https://codesandbox.io/p/devbox/falling-silence-3hphlr

Steps to Reproduce the Bug or Issue

  1. Start a server using the snippet below
  2. run mutation:
    mutation {
      set(value: "test 123456")
    }
  3. Restart server
  4. Observe that the following query fails:
    query {
      get
    }

Expected behavior

It is expected that:

  1. The cookies is set on the client
  2. The cookies is not store in the server

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Bun 1.0.12
  • "graphql-yoga": "^5.0.0"

Additional context

As written, it seems like cookie store is not flushed to the client, but is being persisted in the server.

@madsbuch
Copy link
Author

Note: It could also be Bun that is the issue here.

@ardatan ardatan changed the title Cookies does not work with Pothos GraphQL Cookies does not work with Pothos GraphQL & Bun Nov 24, 2023
@mattigrthr
Copy link

Experiencing the same issue. 😕

Followed this documentation: https://the-guild.dev/graphql/yoga-server/docs/features/cookies

@madsbuch
Copy link
Author

I can provide some more context:

This ways to start the yoga server using graphQL is faulty (and should fail on types).

const yoga = createYoga(...)
const server = Bun.serve({
    fetch: yoga,
  });

Instead one should start the server like:

const yoga = createYoga(...)
const server = Bun.serve({
    fetch: (req) => yoga(req),
  });

I can see that the yoga function as returned from createYoga has a number of overloads. When assigning it directly to the fetch function, it would appear that the server instance is being supplied to yoga as the context, which breaks request isolation.

I will happily make a PR to update some documentation to provide this example for Bun and also look into update typing, if someone can point me in the right direction.

@EmrysMyrddin
Copy link
Collaborator

So if I follow you, this is not in particular linked to Photos ?
It's just because our documentation is using a non working boilerplate for Bun right ?

For now, this workaround seems acceptable, PR are very welcome!

For typing, HTTP adapter related types are defined here: https://github.com/ardatan/whatwg-node/blob/master/packages/server/src/types.ts

@EmrysMyrddin EmrysMyrddin linked a pull request Dec 5, 2023 that will close this issue
@EmrysMyrddin EmrysMyrddin added kind/docs Improvements or additions to documentation and removed stage/1-reproduction A reproduction exists kind/bug labels Dec 5, 2023
@madsbuch
Copy link
Author

madsbuch commented Dec 5, 2023

@EmrysMyrddin yep, this is solely a Yoga issue when running on Bun (Yoga provides bun snippets for installing packages, so Bun seems to be something that is expected to work).

Thanks, I will look at it when I can find some time!

@EmrysMyrddin
Copy link
Collaborator

Hi! Can you try the latest version to see if the problem is still there? It should be fixed now that we have fixed some Bun specific bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/docs Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants