@bus
directive
#2648
remipelhate
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
I generally support the idea and think it is a useful analogue to How exactly would the job class be instantiated? Is it going to be passed the field arguments as an What is the signature of a pipe? Is this documented in Laravel somewhere? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
Lately, as I'm working a lot with Laravel's command bus, I started noticing that most mutation resolvers in codebases I'm working on merely dispatch a job onto the command bus (as validation and authorization can all be done through existing Lighthouse directives). Unless I missed something, you currently have to create a custom resolver in order to dispatch a job on the command bus. For example:
This leads me to wonder if it could be interesting to have a Lighthouse directive to directly map mutations to a job from within the schema definition.
Proposal
I would propose to introduce a new
@bus
directive with a similar setup to the@event
directive:With this specification, the example in the introduction could be implemented as follows:
Forcing sync dispatch
By default, jobs would be dispatched through
Illuminate\Contracts\Bus\Dispatcher::dispatch()
, which inspects the job to determine whether the job should be queued or not. However, theasync
argument on the@bus
directive would allow developers to force dispatching a job synchronously by usingIlluminate\Contracts\Bus\Dispatcher::dispatchSync()
instead.Manipulating jobs before or after dispatch
Laravel's command bus allows developers to define pipes to pass a job through upon dispatch. These pipes basically behave like middleware, meaning jobs can be manipulated before or after dispatch. This could be useful to allow for example formatting the directive's return value in order to match the schema definition:
Breaking changes
As this would introduce a new directive, no breaking changes should be made to implement this idea.
Implementation
I haven't developed this yet, but are willing to do so if there's any interest. This is merely a basic proposal, any suggestions to improve or extend the idea are welcome.
Beta Was this translation helpful? Give feedback.
All reactions