-
-
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
Add configurable namespace to allow custom non-root type field resolvers without FQN #1711
Comments
Regarding namespaces: We should use them where they line up with the structure of the GraphQL schema. Types fall within the root GraphQL namespace and thus should not be put into sub-namespaces. On the other hand, fields are naturally namespaced within their respective type, and we should also represent that within the PHP namespaces. Those are different scenarios. I propose we add another root namespace in 'namespaces' => [
'models' => ['App', 'App\\Models'],
'queries' => 'App\\GraphQL\\Queries',
...
+ 'types' => 'App\\GraphQL\\Types',
'interfaces' => 'App\\GraphQL\\Interfaces',
...
], This brings parity with the namespaces for other root level type classes, such as 'namespaces' => [
'queries' => 'App\\GraphQL\\Types\\Query',
], With this new
|
Just a question - do accessors (like Apart from that - great idea. It would make lighthouse more flexible, but also more clearly structured due convention-based classes, like
|
Yes, the default resolver calls them through simple property access on the I like your suggestions regarding the docs, that is some good advice to give. |
@lorado Just a note about cleaning your schema, utilizing
I think this looks nice. However we have to be careful that we are not just adding more namespaces with very little gain. |
The next major version of Lighthouse could actually drop explicit namespaces for the root type fields, so that line would not even be in |
Right, almost all my root queries/mutations are actually using those directives. But in other Types fields, especially Eloquent models, it feels to me better to use accessors, so I don't need to write an extra directive to resolve a field. Moreover I can use accessors in my regular laravel code, e.g. in emails generating or cron jobs. |
Not exactly on topic of the discussion, but you should probably escape those |
I originally wrote this issue differently, but rewrote it after realizing Lighthouse doesn't work the way I thought it did w.r.t. non-root type field resolvers
What problem does this feature proposal attempt to solve?
My team is working on a fairly large GraphQL API for a restaurant platform. This is our first project using GraphQL, which lead to a lot of "not following best practices" when we just started. We are now tidying up our GraphQL schema, and I found that a lot of our non-root type field resolvers are using a long
@field
directive with a fully qualified class name. I would like to see a method where non-root types can define custom field resolvers without a FQN in the@field
directive.Which possible solutions should be considered?
I came up with a rough idea that would tackle this problem.
We should specify a
fields
(ortypes
) namespace inlighthouse.php
, just like the namespaces that already exist for queries, mutations, etc. This in itself would already greatly decrease clutter for custom field resolvers, as one could already point to resolver classes in this namespace without using their FQN when using the@field
directive. For the sake of this example, let's say we definedfields
asApp\GraphQL\Fields
.To make things a bit neater, I propsose we allow sub-namespaces in the (single)
fields
namespace. Each of these will represent a GraphQL type. Since GraphQL types are uniquely named, every sub-namespace would uniquely cover each type's fields, as long as we limit the namespace search to at most one level deeper than the originally specifiedfields
namespace. In this case, one would use a structure likeApp\GraphQL\Fields\Restaurant\PhoneNumber
to specify a resolver for thephoneNumber
field of theRestaurant
object. This resolver will only be used if the field specifies@field(resolver: "PhoneNumber")
.Finally, with this extra structure in place, we could automatically use custom field resolvers if one exists for a specific type field. Let's take the previous
Restaurant
andphoneNumber
as an example. If thefields
namespace has a sub-namespace calledRestaurant
, we search inside this for aPhoneNumber
class, and automatically use this as the field resolver, in a similar fashion to how queries and mutations automatically pick up custom resolvers.I found #1580 while looking if someone already suggested what I'd like to see. While my idea is different than the idea proposed by the author of this feature request, I would understand if the stance on namespaces would make this an unviable solution. I personally think namespaces are valuable rather than harmful, because different types can have fields with the same name (and namespaces allow exactly this behaviour). Even without steps 2 and 3, just implementing step 1 would already make our schema a lot neater (with shorter lines because we don't need to use FQNs).
The text was updated successfully, but these errors were encountered: