Set response header in resolver

134 Views Asked by At

I am trying to implement authentication in a custom resolver function but to achieve that, I must be able to set a cookie on the response.

I found the following docs on what data is available, unfortunately, I see no way to access the response object nor the response headers. How would this be achievable?

2

There are 2 best solutions below

1
spawnia On BEST ANSWER

I recommend you to keep the HTTP layer separate from the GraphQL layer. That means:

  • don't deal with cookies in resolvers
  • don't expect certain resolvers in HTTP middleware

In my experience, authentication work best when handled exclusively on the HTTP layer - use standard Laravel mechanisms such as guards, described in https://laravel.com/docs/authentication and tie it into your Lighthouse server through middleware such as Nuwave\Lighthouse\Http\Middleware\AttemptAuthentication.

0
mostafa On

If you want to set something in the response of the graphQL you can use EndRequestEvent.