-
-
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
Standardized way to organize custom field resolver #2188
Comments
I am a big fan of having one class per resolver, it makes dependency injection very elegant. |
I am big fan too, that's why I already proposed a way to do it so. But as reading documentation I thought you (as nuwave company) would be more happy with one class per Type as default. Because of this documented example : type User {
created_at: String!
@field(resolver: "App\\GraphQL\\Types\\UserType@created_at")
} |
I guess we might just change the default resolver and omit Here is how I did on my project:
|
Love the idea about default resolver. But, I also had in my drafts a proposition to automatically fallback on snake case when attribute doesn't exists in GraphQL schema case. Because in real life situation, my GraphQL is 100% camelCase, and Laravel still "forces" me to have snake_case in database :( Then instead of doing this : type User {
id: ID!
firstName: String @rename(attribute: "first_name")
lastName: String @rename(attribute: "last_name")
avatarUrl: String @rename(attribute: "avatar_url")
} I would love to simply write : type User {
id: ID!
firstName: String
lastName: String
avatarUrl: String
} Maybe that all together might become a bit too much "magic" ? |
The first thing to do The more complicated continuation
These middlewares might be turned on/off in the config. So each user can easily configure the default resolver if they don't want to write their own. The current default resolver is defined in https://github.com/webonyx/graphql-php/blob/master/src/Executor/Executor.php#L181 |
What problem does this feature proposal attempt to solve?
Propose, and document a standardized way to organize custom field resolver in a Laravel project.
Which possible solutions should be considered?
Idea is about making the
resolver
attribute optional in the@field
directive.When resolver attribute not provided on
@field
directive, resolver classname/method would be computed from following :- The configured namespace in
config(‘lighthouse.namespaces.types’)
- The parent type name
- The field name
Example
becomes equivalent as :
by convention.
Implementation detail
Possibly some user would prefer to have something even more fine-grained like, why not a dedicated class per resolver, let's say
App\\GraphQL\\Resolvers\\User\\GravatarResolver@__invoke
.To still permit this, the « default resolver name resolver » (yeah.. a proper name has to be found) would be written in a way it could be customized/overridden in project-land (like an app()->bind in a service provider). Then up to the developer to write his own logic of default resolver name resolution.
Backward compatibility / Breaking changes
Currently, resolver attribute is required on
@field
directive. So proposed file organization adoption is optional, and 100% backward compatible with existing lighthouse integration.I would be happy to contrib to this feature.
The text was updated successfully, but these errors were encountered: