-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(resolvergen): add batch resolver stub generation (blocked by #4006) #4007
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
base: master
Are you sure you want to change the base?
Conversation
| // Users is the resolver for the users field. | ||
| func (r *queryResolver) Users(ctx context.Context) ([]*User, error) { | ||
| panic(fmt.Errorf("not implemented: Users - users")) | ||
| } | ||
|
|
||
| // NullableBatchBatch is the batch resolver for the nullableBatch field. | ||
| func (r *userResolver) NullableBatchBatch(ctx context.Context, objs []*User) ([]*Profile, []error) { | ||
| panic("not implemented: NullableBatchBatch - nullableBatch") | ||
| } | ||
|
|
||
| // NullableNonBatch is the resolver for the nullableNonBatch field. | ||
| func (r *userResolver) NullableNonBatch(ctx context.Context, obj *User) (*Profile, error) { | ||
| panic("not implemented") | ||
| } | ||
|
|
||
| // NonNullableBatchBatch is the batch resolver for the nonNullableBatch field. | ||
| func (r *userResolver) NonNullableBatchBatch(ctx context.Context, objs []*User) ([]*Profile, []error) { | ||
| panic("not implemented: NonNullableBatchBatch - nonNullableBatch") | ||
| } | ||
|
|
||
| // NonNullableNonBatch is the resolver for the nonNullableNonBatch field. | ||
| func (r *userResolver) NonNullableNonBatch(ctx context.Context, obj *User) (*Profile, error) { | ||
| panic("not implemented") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, even when batch: true is specified, we would generate both a regular resolver and a batch resolver, allowing gqlgen's executor to intelligently choose between them based on the situation.
However, I have determined that this is difficult to achieve with the current template structure. This is because the marshaler is currently designed to be invoked N times for N elements in an array.
That said, this is not a significant problem. In general, a batch resolver implementation subsumes the non-batch resolver implementation. When processing a single element, we can simply pass it as a single-element array to the batch resolver.
We will leave this optimization as an opportunity for future improvement.
3c3e5d3 to
8700b25
Compare
Goal
This series adds first-class batch resolver support to gqlgen, letting you solve the GraphQL N+1 problem without external libraries.
The N+1 problem happens when a single query fans out into many individual DB/API calls, killing performance. Today most people solve this with dataloader libraries; this makes it possible to handle it in gqlgen directly.
Since the behavior is opt-in and doesn't affect existing users, shipping it as experimental still provides clear value.
This PR alone might not give you the full picture, so take a look at the
_examples/batchresolverdirectory on theperformance-enhancement-4branch (#4008). Checking the test files,generated.go, andschema.resolvers.goshould make the difference from existing non-batch resolvers clear. The output itself doesn't change, and there are tests to prove it.Configuration and generated resolver shape
To enable batch resolvers, mark fields explicitly in the config:
When
batch: trueis set, gqlgen generates a batch resolver method that receives multiple parents and returns per‑parent results:If batch is not set, gqlgen generates the standard per‑object resolver:
Only fields with
batch: trueare affected. All other resolvers remain unchanged.Preconditions and approach
Role of each PR
performance‑enhancement‑1branch#4005
Allow batch to be declared in config
performance‑enhancement‑2branch based onperformance‑enhancement‑1#4006
Propagate the batch flag into codegen
performance‑enhancement‑3branch based onperformance‑enhancement‑2#4007
Generate batch resolver stubs
performance‑enhancement‑4branch based onperformance‑enhancement‑3#4008
Add runtime support and verification
Describe your PR and link to any relevant issues.
I have: