-
-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use a custom default resolver? #590
Comments
That is not possible at the moment. You can however add a custom directive and place that on you fields. |
Thanks for your quick reply. Fair enough. Would you consider adding an Event to enable extension of the default resolver? |
I am not sure it is going to be event-based, but it could be useful to have a mechanism of extending this functionality. Out of interest, what is your specific use case and what do you plan to do in the replaced default resolver? If we enable such a feature, we have to consider where to make the cut. There are a few "levels" of default resolvers, the priority where Lighthouse looks is something like:
At what point specifically would you like to hook in? |
Right, to prevent an x-y-problem, here's my use-case: I have an CMS-like graphical editor for the Schema and corrresponding resolvers that creates files like this:
I have a parser that turns these flat-CMS files into a schema string (just like the SchemaSticher@getSchemaString) and corresponding Lighthouse class definitions, e.g. <?php
namespace Queries;
class Hello() {
function resolve(...) {
return 'world';
}
} I basically generate the Lighthouse structure from my CMS files and then Lighthouse loads them from that structure. Now, it would be great if I could hook into the resolver construction directly and generate the resolver directly from my CMS files. I'm able to overwrite the I believe that concerns point 3 and perhabs 2 (although I currently don't support subscriptions). Hope that clarifies my requirements. |
Interesting stuff! I am going on vacation, so i will only have a chance to look into this in a few weeks. It seems like you found a reasonable way to resolve your issue, glad about that. Still, we might make cases like yours easier to extend. Open to suggestions on how exactly we could do this in a way that is:
|
Thanks for letting me know, there is no rush! I believe a good spot to extend the default resolving mechanism is FieldFactory@handle. Currently, the handler is looking for directives and uses their resolver or falls back on the default resolver of If we introduced an event here that receives if ($fieldResolver = $this->directiveFactory->createFieldResolver($fieldDefinitionNode))
{
$this->fieldValue = $fieldResolver->resolveField($fieldValue);
}
event(new CreatingFieldDefinition($fieldValue));
$resolver = $this->fieldValue->getResolver(); would allow to define a custom resolver function (&$fieldValue) {
if (!$fieldValue->hasResolver()) {
$fieldValue->setResolver(...);
}
} but also to extend the existing one returned by the directives or the default resolver. function(&$fieldValue) {
$fieldValue->setResolver(function ($root, array $args, $context, ResolveInfo $resolveInfo) use ($fieldValue) {
// extend resolving behaviour
...
return $fieldValue->getDefaultResolver()($root, $args, $context, $resolveInfo);
});
} Note that this requires to make To me that seems like a flexible solution since it is basically a 'global directive' and it works much like a custom directive implementation. In fact, the Event listener class would look like a FieldResolver where One thing I don't like about this solution is that the use of an Event seems semantically odd, particularly with an argument passed by reference. Instead it might be cleaner and more logical to implement it directly as a class definition that the user can register in the config file. |
@spawnia Let me know if you have some feedback on my proposal |
@frthjf as you noted yourself, the semantics of this approach do not feel quite right. I think we could just extract the logic for getting a default resolver into an interface like this: <?php
namespace Nuwave\Lighthouse\Support\Contracts;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
interface ProvidesDefaultResolver
{
public function defaultResolver(FieldValue $fieldValue): \Closure;
} |
@spawnia I agree, that would be much simpler! |
That's great, thanks @spawnia! |
Hi, first of all, thanks for this great package! It's amazing work.
I'm integrating Lighthouse with a CMS system and I'm looking for a way to overwrite the defaultResolver for FieldValues. Is it possible to specify a custom default resolver that returns the resolve closure?
I'm using the latest version 3.0 alpha.
The text was updated successfully, but these errors were encountered: