nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 438 forks source link

Access context in `FieldMiddleware` #2506

Closed jaulz closed 7 months ago

jaulz commented 7 months ago

What problem does this feature proposal attempt to solve?

It would be great if we could access the $context in FieldMiddleware so we can for example access the resolved user. In my specific case I would like to access the authenticated user (via context) in the ValidateDirective so I can only allow specific input values that are available to the user. Right now, I can simply use auth()->user() but it does not feel 100% right (or am I wrong?).

Which possible solutions should be considered?

I assume it must be implemented here but I haven't got any nice idea about a backward compatible change: https://github.com/nuwave/lighthouse/blob/fa7af9d17958b5c4d4e7bde7c4dfd7d0dcff06db/src/Schema/Values/FieldValue.php#L158-L187

spawnia commented 7 months ago

You already can, see https://lighthouse-php.com/master/custom-directives/field-directives.html#fieldmiddleware.

jaulz commented 7 months ago

@spawnia sorry, you are right and I phrased it wrong/didn't understand it correctly before. In fact I meant the argument set transformers which are registered by the FieldMiddleware, e.g. the ValidateDirective like I mentioned in my example.

spawnia commented 7 months ago

Yeah, I could see passing the context to them. It would be a breaking change to the interface, so it would have to wait until the next major version. Open for pull requests.