A Plugin To Manipulate Cookies? #1006
-
I use the RedwoodJS framework which recently switched to Envelop+Helix I see there is not an Envelop plugin to help services manipulate cookies Are cookies available in some other way in my GraphQL services? Should I write a plugin to help services manipulate cookies? I feel like I'm missing something - there is no way that I'm the first person to want to manipulate cookies in my gql-services... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hi! Basically, To address that in a generic way to send headers, I think you can create a simple plugin like that: const useResponseHeadersPlugin = () => {
return {
onContextBuilding({ extendContext }) {
extendContext({
responseHeaders: new Set()
});
},
onExecute({ args }) {
return {
onExecuteDone({ result, setResult }) {
// Set headers to be located on `extensions` and later you need to handle it and send it over the HTTP transport
setResult({ extensions: { ...(result.extensions || {}), headers: args.contextValue.responseHeaders });
// If you are passing `{ req, res }` to your handler, you can also set it directly on the `res.headers`
}
}
}
}
} Then, in your resolvers, you can do things like: const resolvers = {
Mutation: {
login: (root, args, context) => {
// ...
context.responseHeaders.set("X-My-Header", "")
}
}
}
cc @dthyresson :) @n1ru4l @ardatan I think maybe in Helix we can pass headers from the response somehow? I know that we have this: https://github.com/contra/graphql-helix/blob/master/packages/core/lib/process-request.ts#L254 , but it seems like it's always empty. I guess it can take |
Beta Was this translation helpful? Give feedback.
-
I solved the problem without needing cookies to flow thru Envelop Thanks for the pointers, I'll pick this up again should it become a 'something' |
Beta Was this translation helpful? Give feedback.
Hi!
Are you referring to set cookies through the response of the GraphQL result? If you are referring to something like
Set-Cookie
headers (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) ?Basically,
envelop
aims to avoid dealing with things that are specific to your transport, so fromenvelop
point-of-view, it's out of scope.To address that in a generic way to send headers, I think you can create a simple plugin like that: