You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
QueryTraverser only descends into FragmentSpreads once #1695
On a query such as this, QueryTraverser will visit the name field only once (and will visit the ...userInfo spread twice, as expected). This is distinctly different from the behavior one would get for a semantically equivalent query with no fragments:
{
users {
namefriends {
name
}
}
}
One immediate implication is on query complexity analysis. A (malicious?) query with recursive fragment spreads will get a score underestimated by orders of magnitude.
As an example, MaxQueryComplexityInstrumentation will calculate the complexity of the full introspection query to be 45, while in reality over a 100 fields are requested. And that query isn't intentionally crafted to trick the calculation with large, recursive, fragment spreads.
Investigation
The reason why this happens is that QueryTraverserin this line considers FragmentDefinition the only child of each FragmentSpread. Since both spreads have the same definitions, when the traverser encounters the 2nd spread, it will falsely believe (in this line) its children (the definition) have already been visited and will not descend.
Happy to help fixing this, but I'd need guidance on the approach...
I understood the traverser ignores duplicates and I found that reasonable enough, especially for the purposes of complexity analysis (as the "price" of resolving each field will only be paid once). But fragments I think are special, as they can be spread in unrelated contexts, effectively expanding into entirely new (and not visited) fields. Of course, spreading a fragment can result in duplicate fields, going back to your original question... which I'm still unsure how to answer.
On one hand, it makes sense for the traverser to visit each field as specified. But since the duplicate fields get merged during resolution anyway, visiting them only once sounds correct too. I'd honestly go for whatever is easier to implement and see if anyone has a legit use-case for the alternative.
Btw, if I said something nonsensical, please forgive, it's 3am where I am and my brain isn't at it's best 😶 I'll think some more on this in the morning.
NormalizedQueryTree looked very promising for precise complexity analysis, but can't seem to provide the info I'm looking for after all. The main hurdle is that I'd like to detect alternative subtrees and pick the most expensive one.
I'd like to detect that ... on Cat and ...on Dog subtrees are alternatives, calculate the complexity for both and pick only the most expensive one, but that doesn't seem possible by looking at NormalizedField structure.
I'm currently using a tangled mess of internal methods to calculate complexity, and on every upgrade I'm terrified of anything changing in there. Do you see anything I could use instead for this purpose?
I'd like to detect that ... on Cat and ...on Dog subtrees are alternatives, calculate the complexity for both and pick only the most expensive one, but that doesn't seem possible by looking at NormalizedField structure.
I think we need to understand more about that. NormalisedField allows you to know the object type (so you can know if its a Cat or a Dog) and you can see the parent and child fields from a given NF
Hello!
Is there a solution coming for this issue? We realized we're accepting queries that have way higher complexity than we should because of fragments being used in several places.
Cheers
Has anything changed since 2020 regarding this? Any way to build accurate complexity analysis?
ExecutionContext.getNormalizedQueryTree() is public, but the returned ExecutableNormalizedOperation is @Internal, closing that door...
The only supported way to reach into the info stored in ExecutableNormalizedField seems to be via the SelectedField, which is only accessible from DataFetchingEnvironment and not from high-level instrumentation steps (e.g. beginExecuteOperation), making it not useful for complexity analysis (not possible to analyze the entire query before any of it executes, as DataFetchingEnvironment only exists for a specific field).
The ExecutableNormalizedXXX classes will be made public in the next version. We started off internal as we built them out
They actually power quite a bit of the Atlassian graphql code. Nadel (our federation library) is based entirely on ENFs and we recently used it in some schema CLI work as well as some subscription work.
So in short they have proven themselves. So we will at some point soon make them public API.
So this is the way forward. I like your PR linked as an idea. See the PR for more comments.
Activity
QueryTraverser only descends into FragmentSpreads once
andimarek commentedon Jan 9, 2020
@kaqqao thanks for reporting.
That is indeed an interesting case: The general traverser doesn't visit any vertex in a graph twice in order to avoid loops and extra work.
But I can see that in the case it would be good to visit the FragmentDefinition twice (or as many times it is referenced).
andimarek commentedon Jan 9, 2020
@kaqqao to add on this: what is your expectations around duplicate fields?
for example:
{ foo { bar} foo {bar}}would you expect that
baris visited twice or once?kaqqao commentedon Jan 10, 2020
I understood the traverser ignores duplicates and I found that reasonable enough, especially for the purposes of complexity analysis (as the "price" of resolving each field will only be paid once). But fragments I think are special, as they can be spread in unrelated contexts, effectively expanding into entirely new (and not visited) fields. Of course, spreading a fragment can result in duplicate fields, going back to your original question... which I'm still unsure how to answer.
On one hand, it makes sense for the traverser to visit each field as specified. But since the duplicate fields get merged during resolution anyway, visiting them only once sounds correct too. I'd honestly go for whatever is easier to implement and see if anyone has a legit use-case for the alternative.
Btw, if I said something nonsensical, please forgive, it's 3am where I am and my brain isn't at it's best 😶 I'll think some more on this in the morning.
andimarek commentedon Jan 10, 2020
@kaqqao I created a new PR which adds an analyzer which produces a "query execution tree": this tree contains all the fields merged together.
Have a look at this test:
https://github.com/graphql-java/graphql-java/pull/1731/files#diff-72f87f8a5876e2927c1df5eb8cb4bd38R479
A complexity analysis should probably traverse this query execution tree for the most accurate analysis.
kaqqao commentedon Feb 8, 2021
NormalizedQueryTreelooked very promising for precise complexity analysis, but can't seem to provide the info I'm looking for after all. The main hurdle is that I'd like to detect alternative subtrees and pick the most expensive one.E.g. for a query like this:
{ pet(id: 123) { sound owner { name } ... on Cat { catField } ... on Dog { dogField owner { age } } } }I'd like to detect that
... on Catand...on Dogsubtrees are alternatives, calculate the complexity for both and pick only the most expensive one, but that doesn't seem possible by looking atNormalizedFieldstructure.I'm currently using a tangled mess of internal methods to calculate complexity, and on every upgrade I'm terrified of anything changing in there. Do you see anything I could use instead for this purpose?
bbakerman commentedon Feb 8, 2021
I think we need to understand more about that.
NormalisedFieldallows you to know the object type (so you can know if its a Cat or a Dog) and you can see the parent and child fields from a givenNFWhat is it that is missing for you?
jstlnd commentedon Apr 22, 2021
Hello!
Is there a solution coming for this issue? We realized we're accepting queries that have way higher complexity than we should because of fragments being used in several places.
Cheers
kaqqao commentedon Apr 8, 2023
Has anything changed since 2020 regarding this? Any way to build accurate complexity analysis?
ExecutionContext.getNormalizedQueryTree()is public, but the returnedExecutableNormalizedOperationis@Internal, closing that door...The only supported way to reach into the info stored in
ExecutableNormalizedFieldseems to be via theSelectedField, which is only accessible fromDataFetchingEnvironmentand not from high-level instrumentation steps (e.g.beginExecuteOperation), making it not useful for complexity analysis (not possible to analyze the entire query before any of it executes, asDataFetchingEnvironmentonly exists for a specific field).Seems like all doors are closed...
Enable selection set analysis before field resolution starts
bbakerman commentedon Apr 11, 2023
The
ExecutableNormalizedXXXclasses will be made public in the next version. We started off internal as we built them outThey actually power quite a bit of the Atlassian graphql code. Nadel (our federation library) is based entirely on ENFs and we recently used it in some schema CLI work as well as some subscription work.
So in short they have proven themselves. So we will at some point soon make them public API.
So this is the way forward. I like your PR linked as an idea. See the PR for more comments.
kaqqao commentedon Apr 11, 2023
Alright! Thanks for the heads-up. I'll close this issue.