graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Request cookies not included

Open fookit opened this issue 4 years ago • 6 comments

I have this request:

// src/requests/user.ts

require('fetch-cookie/node-fetch')(require('node-fetch'));

const client = new GraphQLClient(endpoint, {
  credentials: "include",
  mode: "cors",
});

export async function queryUser(id: string) {
  const {user} = await client.request(
    gql`
      query user($id: ID!) {
        user(id: $id) {
          id
          name
        }
      }
    `,
    {
      id,
    }
  );
  return user ? user : null;
}

This is the fastify-cors options:

// src/app.ts
...

  app.register(cors, {
    origin: true,
    credentials: true,
  } as FastifyCorsOptions);

...

Set cookies via log in and when I make a user request, Node.js API got the header without cookies:

req.headers: {
  'content-type': 'application/json',
  accept: '*/*',
  'content-length': '523',
  'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
  'accept-encoding': 'gzip,deflate',
  connection: 'close',
  host: 'localhost:8000'
}

Please help me.


graphql-request v3.4.0, node.js v14.17.2 LTS

fookit avatar Jul 27 '21 17:07 fookit

Did you ever figure it out? I think I currently have the same issue-- the cookie shows up in the network request but not under storage in the application tab.

apaul45 avatar Jan 13 '22 23:01 apaul45

I believe it's the CORS settings on the server - if origin is * (I assume true would do the same as setting it to *) then cookies won't be included in the request. See this StackOverflow answer. Set origin to the frontend's domain, e.g. http://localhost:4000 and it should work. Make sure you also include the domain used in production on the frontend

dodiameer avatar Jan 23 '22 17:01 dodiameer

I believe I completed all necessary steps for configuring cors in the backend, but I still don't know how to set credentials to true for this specific library. I tried using the example provided but the cookie still only appears in the network request itself and not in the application memory.

apaul45 avatar Jan 26 '22 01:01 apaul45

Can you put your CORS configuration in a comment?

dodiameer avatar Jan 26 '22 19:01 dodiameer

In server file:

const app = express();

const corsOptions = { origin: "http://localhost:3000", credentials: true };

expressMiddleware(app, corsOptions);

const server = new ApolloServer({ typeDefs: typeDefs, resolvers: resolvers, context: ({ req, res }) => ({req, res}), uploads: false, cors: false, });

In middleware:

const corsPolicy = async (req, res, next) => { console.log(req.headers.origin); res.set("Access-Control-Allow-Credentials", true); res.set("Access-Control-Allow-Origin", req.headers.origin); res.set("Access-Control-Allow-Headers", req.headers); next(); };

app.use(cors(corsOptions)); app.use(corsPolicy);

apaul45 avatar Feb 25 '22 01:02 apaul45

I am facing the same issue as well. Cookies not being sent in the request

phaniteja1 avatar Feb 28 '22 20:02 phaniteja1

Any workaround for this issue? This is a dependency on RTK Query and I need to pass cookie in the request header

MylesWardell avatar Mar 22 '23 06:03 MylesWardell

It's working for me when I'm specifying a custom fetch method.

const customFetch = require('fetch-cookie/node-fetch')(require('node-fetch'));

const client = new GraphQLClient(endpoint, {
  fetch: customFetch,
  credentials: "include",
  mode: "cors",
});

I'm using graphql-request v5.1.0, node.js v16.13.2

MartinGassner avatar Mar 24 '23 14:03 MartinGassner