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

feat: only wrap affected resolver functions by using the 'onSchemaChange' hook with 'mapSchema' instead of the 'onResolverCalled' hook. #4760

Merged
merged 6 commits into from
Mar 16, 2022

Conversation

n1ru4l
Copy link
Contributor

@n1ru4l n1ru4l commented Mar 15, 2022

As discussed with @dthyresson this is my PR for improving the performance of the useRedwoodDirective logic.

  • Only resolver functions that have redwood directives applied will be wrapped
  • All expensive lookups are moved to the plugin initialization phase instead of being done for each individual request

…nge' hook with 'mapSchema' instead of the 'onResolverCalled' hook.
@dthyresson
Copy link
Contributor

Thanks so much for this PR @n1ru4l

Both @dac09 and I will review.

@dthyresson dthyresson added the release:fix This PR is a fix label Mar 15, 2022
info.variableValues
) || {}

getDirectiveValues(directive, { directives: [directiveNode] }) || {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that it seems info.variableValues was not relevant at all, since we are looking at schema directives, not operation directives.

Comment on lines +143 to +146
if (isPromise(result)) {
return result.then(() =>
originalResolve(root, args, context, info)
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a small performance optimization for avoiding making stuff async where not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yaacovCR also created this cool micro library, it could be used instead.

Comment on lines 210 to 220
if (schema.extensions?.[didMapSchemaSymbol] === true) {
return
}
const transformedSchema = wrapAffectedResolvers(schema, options)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
transformedSchema.extensions = {
...schema.extensions,
[didMapSchemaSymbol]: true,
}
replaceSchema(transformedSchema)
Copy link
Contributor Author

@n1ru4l n1ru4l Mar 15, 2022

Choose a reason for hiding this comment

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

Since for every replaceSchema call the onSchemaChange hook of all other plugins is called we need a way to track whether a transform has already been applied for preventing an infinite loop. The schema extensions are perfect for this!

async onResolverCalled({ args, root, context, info }) {
const directiveNode = getDirectiveByName(info, options.name)
): GraphQLSchema {
return mapSchema(schema, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +205 to +206
* Currently graphql-js extensions typings are limited to string keys.
* We are using symbols as each useRedwoodDirective plugin instance should use its own unique symbol.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created graphql/graphql-js#3511 to address this.

@thedavidprice
Copy link
Contributor

Nice!

Also, how is this possible?!? I feel like you've been in the Redwood PRs for so very long now 🤔

Screen Shot 2022-03-15 at 3 06 40 PM

@n1ru4l
Copy link
Contributor Author

n1ru4l commented Mar 15, 2022

@thedavidprice haha, so far I only talked a lot 😆! Now I am finally getting my own hands dirty 🎉

use ts-expect-error so we can remove this ignore once the issue is resolved in graphql :)
@dac09
Copy link
Collaborator

dac09 commented Mar 16, 2022

@n1ru4l what a wonderful contribution from a first time contributor 😉

I very much appreciate your help here, everything seems to work perfectly. I've read your blog posts on this about 5 times now, I still struggle with it 🤣 - really couldn't have done this without the Guild's help.

Going to wait for smoke tests to pass then merge!

@dac09 dac09 enabled auto-merge (squash) March 16, 2022 17:43
@thedavidprice thedavidprice merged commit 532ea55 into redwoodjs:main Mar 16, 2022
@jtoar jtoar added this to the next-release milestone Mar 16, 2022
@thedavidprice thedavidprice removed this from the next-release milestone Mar 23, 2022
@thedavidprice thedavidprice added this to the v0.50.0 milestone Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

5 participants