Replies: 3 comments 1 reply
-
I read the documentation and realized that the gqlgen already provides a very convenient interface for setting the complexity via As a variant we can add a field to directive @goField(
forceResolver: Boolean
forceComplexity: Boolean
name: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION So we can use this to mark a field, that the field have custom complexity function: type Query {
findSomething(filter: Filter!) {
someField
} @goModel(forceResolver: true, forceComplexity: true)
} The generator, in turn, will generate the corresponding functions: // Field resolver
func (r *queryResolver) FindSomething(ctx context.Context, obj *model.Query, filter *model.Filter) (model.SomethingList, error) {
panic("Not implemented")
}
// Field complexity function
func QueryFindSomethingComplexity(childComplexity int, filter *model.Filter) int {
panic("Not implemented")
} And automatically bind this function to complexity resolver in generated runtime: func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
ec := executionContext{nil, e}
_ = ec
switch typeName + "." + field {
case "Query.findSomething":
args, err := ec.field_Query_findSomething_args(context.TODO(), rawArgs)
if err != nil {
return 0, false
}
return gen.QueryFindSomethingComplexity(childComplexity, args["filter"].(gen.Filter)), true
<...> |
Beta Was this translation helpful? Give feedback.
-
If we could define a complexity fallback function, we can define our own resolver that passes a complexity parameter down to the complexity fallback function and automates the process of computing the complexity. That would lest us just add @cost directives to every field that has a non-trivial complexity and implement the complexity logic just one time in a generic function. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone! I've developed a plugin that calculates query complexity for GraphQL. This plugin supports the complexity directive and follows the Relay pagination specifications. You can find the plugin at the following link: https://github.com/Warashi/compgen |
Beta Was this translation helpful? Give feedback.
-
It would be very convenient to define the field complexity through a special directive like here.
Is it possible to implement such feature?
Beta Was this translation helpful? Give feedback.
All reactions