Issues with Cookies Not Appearing in Browser for GraphQL Requests Across Microservices #7456
-
Issue with Cookies in Local DevelopmentHi everyone, I have two microservices: Auth (using GraphQL Yoga) and Gateway (using GraphQL Mesh). The services are deployed locally on ports 3000 and 4000 respectively. When I navigate to Here are the relevant configurations:
sources:
- name: Auth
handler:
graphql:
endpoint: http://localhost:3000/graphql
plugins:
- httpDetailsExtensions:
if: "env.NODE_ENV === 'development'"
pollingInterval: 3600000
serve:
playground: true
hostname: 0.0.0.0
cors:
credentials: true
origin: http://localhost:4000
export function setCookie(ctx: GraphQLContext, name: TokenType, token: string, expires: string | number) {
ctx.request.cookieStore?.set({
name,
sameSite: COOKIES_SAME_SITE as CookieSameSite,
secure: en(COOKIES_SECURE),
domain: COOKIES_DOMAIN!,
expires: new Date(Date.now() + ms(expires.toString())),
value: token,
httpOnly: en(COOKIES_HTTPONLY)
});
}
export function assignAuthorizationCookies(ctx: GraphQLContext, { id, name, email, phone }: { id: string, name: string, email?: string | null, phone?: string | null }) {
const accessToken = generateToken({ id, name, email, phone }, JWT_TOKEN!, SESSION_EXPIRES!);
setCookie(ctx, TokenType.ACCESS_TOKEN, accessToken, SESSION_EXPIRES!);
const refreshToken = generateToken({ id }, REFRESH_TOKEN!, REFRESH_SESSION_EXPIRES!);
setCookie(ctx, TokenType.REFRESH_TOKEN, refreshToken, REFRESH_SESSION_EXPIRES!);
} Mutation and Response DetailsMutation: mutation {
loginUser(emailOrPhone: "88005553535", password: "password") {
name
}
} Response: {
"data": {
"loginUser": {
"name": "Name"
}
},
"extensions": {
"httpDetails": [
{
"sourceName": "Auth",
"request": {
"timestamp": 1722756057656,
"url": "http://localhost:3000/graphql",
"method": "POST",
"headers": {
"accept": "application/graphql-response+json, application/json, multipart/mixed",
"content-type": "application/json"
}
},
"response": {
"timestamp": 1722756060429,
"status": 200,
"statusText": "OK",
"headers": {
"set-cookie": [
"accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY2YTMzODFlNDc3YjQwZWI2YzY5MGYwZCIsIm5hbWUiOiJUaW11ciIsImVtYWlsIjpudWxsLCJwaG9uZSI6Iis3OTg3MDU5OTEzNyIsImlhdCI6MTcyM; Domain=localhost; Path=/; Expires=Sun, 04 Aug 2024 21:21:21 GMT; SameSite=Strict; HttpOnly",
"refreshToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY2YTMzODFlNDc3YjQwZWI2YzY5MGYwZCIsImlhdCI; Domain=localhost; Path=/; Expires=Tue, 03 Sep 2024 21:21:21 GMT; SameSite=Strict; HttpOnly"
],
"content-type": "application/graphql-response+json; charset=utf-8",
"date": "Sun, 04 Aug 2024 07:21:00 GMT",
"content-length": "39"
}
},
"responseTime": 2773
}
]
}
} CORS ConfigurationI also tried configuring CORS, but it didn't help:
export const yoga = createYoga({
...
plugins: [
...
useCookies(),
...
],
cors: {
origin: 'http://localhost:3000',
credentials: true,
allowedHeaders: ['X-Custom-Header'],
methods: ['POST']
}
});
cors:
credentials: true
origin: http://localhost:4000 Any ideas on why cookies are not being set for requests to the Gateway service? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Because Mesh doesn't have this kind of a feature yet :) |
Beta Was this translation helpful? Give feedback.
-
Not sure I completely understand your issue, but we are using cookies with no issues with Graphql mesh. This is what I did: Inside
|
Beta Was this translation helpful? Give feedback.
Because Mesh doesn't have this kind of a feature yet :)