graphql-request
graphql-request copied to clipboard
Request cookies not included
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
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.
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
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.
Can you put your CORS configuration in a comment?
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);
I am facing the same issue as well. Cookies not being sent in the request
Any workaround for this issue? This is a dependency on RTK Query and I need to pass cookie in the request header
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