Skip to content

Rework NormalizedField to reference multiple objects - #2345

Merged
andimarek merged 12 commits into
masterfrom
nf-work
May 20, 2021
Merged

andimarek merged 12 commits into
masterfrom
nf-work

Conversation

@andimarek

@andimarek andimarek commented May 18, 2021 •

Copy link
Copy Markdown
Member

This changes NormalizedFields to contain a list of Object type names and not just one.

The main reason for this change change is to avoid an explosion of normalized Fields when you have Interfaces implemented by many Objects. This could lead to a huge amount of normalized fields, which had significant performant impact in certain cases.

It also aligns a NormalizedField more closely with what most people expect:

For example a schema

type Query {
  pets: [Pet]
}
interface Pet {
  name: String
}
type Dog implements Pet {
  name: String
}
type Cat implements Pet {
  name: String
}

and this query:

{pets{name}}

the normalized query would contain three fields: for each Object implementing Pet one field: Dog.name and Cat.name. Now it is only one field referencing both: [Dog, Cat].name.

Breaking Changes for DataFetchingFieldSelectionSet

As a consequence the API of DataFetchingFieldSelectionSet and SelectionSet changed.

SelctionSet was changed to reflect multiple objects now.

The API of DataFetchingFieldSelectionSet didn't change but the fully qualified names of a SelectionSet now contains a List of objects if there is more than one: e.g. [Dog, Cat].name instead of Dog.name and Cat.name.

This means some glob pattern may not work anymore as before. For example to match any Cat name field you need now a *Cat*.name instead of Cat.name.

@andimarek andimarek added the Not to be merged spikes or other stuff that should never or not yet to be merged label May 18, 2021
@andimarek andimarek changed the title WIP Rework NormalizedField and DataFetcherSelectionSet May 19, 2021
@andimarek andimarek added this to the 17.0 milestone May 19, 2021
@andimarek andimarek changed the title Rework NormalizedField and DataFetcherSelectionSet Rework NormalizedField to reference multiple objects May 19, 2021
@aleksandarsusnjar

Copy link
Copy Markdown

Early morning here... Just read your description. Definitely sounds like an improvement from my perspective, as I can (still) easily adjust my implementation and the present one is sort of dangerous for anyone who may ned up having a recursive schema. Will test very soon (but maybe not today exactly - will try).

Question though: Why do you need to "pre-normalize" at all? To me it seems that we, the graphql-java users have to handle the actual fetching and graphql-java has to get the values from our fetchers. Can't it decide at that time what it needs based on actual/concrete type?

@bbakerman

Copy link
Copy Markdown
Member

Question though: Why do you need to "pre-normalize" at all? To me it seems that we, the graphql-java users have to handle the actual fetching and graphql-java has to get the values from our fetchers. Can't it decide at that time what it needs based on actual/concrete type?

Today the algorithm of graphql-java execution is based on the simple reference implementation

A breath first descent of the AST tree.

However over the years we have learnt new ways of executing queries that may be more powerful for some use cases. For example if we have a better "tree representation of the query (with types and arg values all pre rendered and resolved) then we could write an engine that did batching better.

At Atlassian we are writing a graphql gateway that doesnt execute queries per se but pulls them apart and feeds them onto underlying systems

We have learnt that a more optimised representation of the query allows for the engine to be easy to write and performs better. This is where the original NormalisedQuery work came from.

We then learnt that the original implementation can explode out into millions of tree entries on certain schemas. So this is revisiting those early decisions

Imagine we could store that in a DB - we have a pre built query ready to execute - imagine we passed not query AST to an underlying graphql service, bit partial NormalisedQuery structures say.

This is en evolution to a state where perhaps a graphql query is not parsed AST but in fact a pre built NormalisedQuery with all the types and args resolved.

public GraphQLFieldDefinition getFieldDefinition() {
return normalizedField.getFieldDefinition();
public List<String> getObjectTypeNames() {
return new ArrayList<>(normalizedField.getObjectTypeNames());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ImmutableList.of() - no allocation if its immutable I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

    public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) {
        if (elements instanceof ImmutableCollection) {
            ImmutableList<E> list = ((ImmutableCollection)elements).asList();
            return list.isPartialView() ? asImmutableList(list.toArray()) : list;
        } else {
            return construct(elements.toArray());
        }
    }

Yup no allocation on immutable collections

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

well some but smarter

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

unfortunately object types names is a mutable Set (on purpose)

return TraversalControl.CONTINUE;
}
});
return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should something like this go on NormalisedQuery itself??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

maybe yes ... but I will not do it now.

@bbakerman bbakerman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should do one more pass on Immutable structures here. This will improve the memory profile of this even more

Also dont rely on the iterator.next trick. (Ahh is it a Set ....maybe you have to)

@andimarek
andimarek merged commit fdd8d1a into master May 20, 2021
@aleksandarsusnjar

Copy link
Copy Markdown

At Atlassian we are writing a graphql gateway that doesnt execute queries per se but pulls them apart and feeds them onto underlying systems

We have learnt that a more optimised representation of the query allows for the engine to be easy to write and performs better. This is where the original NormalisedQuery work came from.

We then learnt that the original implementation can explode out into millions of tree entries on certain schemas. So this is revisiting those early decisions

Imagine we could store that in a DB - we have a pre built query ready to execute - imagine we passed not query AST to an underlying graphql service, bit partial NormalisedQuery structures say.

What I am doing with graphql-java, and I imagine many are to some extent, is exposing stuff we store in the DB. I have a model we wish to expose. My code is doing the refactoring of the GraphQL queries into "somewhat more" efficient underlying queries based on our assumptions and statistics. Note that the GraphQL data model isn't the same as the storage model so the GraphQL "engine" can't really know what is the most efficient way of executing those (I am not talking about aggregators from multiple sources). By trying to be "helpful" it is taking away the original query model that is may be hiding the actual user's intent as well. What I am doing now is almost literally "reverse engineering" that normalization - trying to get what did the author of the query really say. This may sound unimportant but that provides the author with some way to influence the optimizer and nudge it a different way, intentionally - as all the optimization intelligence based on statistics can and does break down here-and-there.

Note that for every exposed type I have my own "mapping metadata". I also keep a graph of all types with links to their immediate/direct sub- and super-types and also have fast means to find all (not only immediate/direct) sub- and super-types as well. I can check type relationships, property declarations and inheritance, etc. very quickly. I can tell you exactly in which types a property exists in nanonseconds. Sure, this takes some memory but only "once" (per schema).

@andimarek andimarek removed the Not to be merged spikes or other stuff that should never or not yet to be merged label May 20, 2021
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.

3 participants