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

Fix the query hashing algorithm #6205

Open
wants to merge 104 commits into
base: dev
Choose a base branch
from
Open

Fix the query hashing algorithm #6205

wants to merge 104 commits into from

Conversation

Geal
Copy link
Contributor

@Geal Geal commented Oct 29, 2024

This splits part of the work from #5255 to make it easier to merge. This PR only covers the fixes for the query hashing algorithm, which is currently used in entity caching, without integrating the changes to the query planner cache key.


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Tests added and passing3
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

Note any exceptions here

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

Ok(visitor)
}

pub(crate) fn hash_schema(&mut self) -> Result<(), BoxError> {
Copy link
Member

Choose a reason for hiding this comment

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

As an idea for follow up PR: What if use struct destructuring (without ...) for accessing fields on schema?
In that way, we can ensure that all fields are considered for hashing, so if new fields are added to apollo-rs, they will result in compiler errors.
That way, we protect against stuff that would be added to the GraphQL spec in the future, e.g., support for directives in a new location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it could be useful for the other structures we use elsewhere in the visitor. Here, the schema also holds a list of types, but the point of this algorithm would be to ignore irrelevant types

Geal and others added 3 commits October 31, 2024 15:16
We do not need to hash the same field's definition multiple times, but the query's field usage needs to always be done

"^ARGUMENT_DEF_LIST".hash(self);
for argument in &field_def.arguments {
self.hash_input_value_definition(argument)?;
Copy link
Member

@IvanGoncharov IvanGoncharov Nov 1, 2024

Choose a reason for hiding this comment

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

I'm not 100% sure, but it looks like it also should hash @fromContext:
https://www.apollographql.com/docs/graphos/reference/federation/directives#fromcontext
I looked into apollo-federation, and it seems that Rust QP doesn't support it, but it is implemented in JS QP.
I don't think this directive will affect entity cache, because you also hash the selection set of subgraph query (I'm just assuming here)

However, this directive should affect QP (at least for the JS planner), so it's breaking QP caching.

Copy link
Member

@IvanGoncharov IvanGoncharov left a comment

Choose a reason for hiding this comment

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

@Geal I triple checked this PR and it looks great 👍
The only thing I found is @fromContext, otherwise it is ready to be merged.

If @fromContext is not an issue, then just ping me on Slack and I will approve this one ASAP.

@Geal
Copy link
Contributor Author

Geal commented Nov 5, 2024

@IvanGoncharov I added the initial support for the context directives. I will take another look tomorrow, but I think the approach is sound:

  • as we go through the query, whenever we encounter a @context directive, we record the type in a map context name -> [Type}
  • when we encounter a field with the join__field directive and its contextArguments argument, then for each argument, we take the context name and selection
  • we look up the types linked to that context name. Considering the way we go through the query and relevant types, it might return types that would not appear in this query, so it might be a bit stricter than necessary
  • for each of those type, we then use the selection and hash that in the same way we handle @key and @requires

There are some assumptions in there that we might want to check, like what happens when a contextArgument refers to a context that we did not find in the query (maybe that would have been prevented at composition?)

@IvanGoncharov IvanGoncharov self-assigned this Nov 26, 2024
@IvanGoncharov IvanGoncharov marked this pull request as draft November 26, 2024 19:41
@IvanGoncharov
Copy link
Member

Converting to draft as result of internal discussion regarding the risks introduced by parent PR, see details in #5255

@IvanGoncharov IvanGoncharov marked this pull request as ready for review November 27, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants