Skip to content
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

Remove uses of reduce/spread which causes significant latency to large middlewares #567

Merged
merged 2 commits into from Jul 7, 2023

Conversation

ryanquinn3
Copy link
Contributor

Hi there, at work we have a very large graphql schema and use graphql middlewares heavily. I've recently been debugging why our graphql server takes so long to start up and identified one area in this library that contributes a good chunk of that time.

I found that this library performs quite poorly when a particular middleware contains a lot (~1k+) of keys. After a deeper inspection, I see that applicator.ts uses the reduce/spread anti-pattern which I have been bitten by a few times in the last handful of years. The simple changes in this PR should not change the behavior of this library at all but they did reduce the total latency of our applyMiddleware call from ~11s to about 6s. If others in the community are using this library with large schemas & middlewares then I want them to benefit as well.

@ryanquinn3
Copy link
Contributor Author

Hey @dimatill, any chance you've had a moment to look over this change?

@dimatill
Copy link
Collaborator

dimatill commented Jul 7, 2023

wow, @ryanquinn3 this is a great finding! Thank you!

src/applicator.ts Show resolved Hide resolved
Comment on lines 154 to 159
(resolvers, fieldName) => {
resolvers[fieldName] = applyMiddlewareToField(
fieldMap[field],
options,
middleware[field],
),
}),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot find name 'field'. Could you please rename fieldName back to field, or use fieldName inside the function?

@ryanquinn3
Copy link
Contributor Author

@dimatill This is what I get for submitting the PR via github's web editor 😞

Commit pushed. I've confirmed that the package builds & tests pass.

@dimatill dimatill merged commit e9eac82 into maticzav:master Jul 7, 2023
1 check passed
@dimatill
Copy link
Collaborator

dimatill commented Jul 7, 2023

Thank you @ryanquinn3 !!!

@ryanquinn3 ryanquinn3 deleted the rq/optimize-reduce-spread branch July 7, 2023 16:19
@github-actions
Copy link

github-actions bot commented Jul 7, 2023

🎉 This PR is included in version 6.1.35 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants