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

POC: Implement Multi fetch #3

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

POC: Implement Multi fetch #3

wants to merge 14 commits into from

Conversation

alexus37
Copy link
Owner

@alexus37 alexus37 commented Nov 26, 2024

This pull request introduces several significant enhancements to the post-processing capabilities of the graphql-go-tools package, specifically in the management of fetch tree nodes. The key modifications include the introduction of a new Multi fetch tree node type, updates to the loader to accommodate this new node type, and the addition of corresponding test cases to verify the functionality of these enhancements.

The rationale behind this change is to alleviate the load on a subgraph when a router resolves a query. The primary objective is to consolidate multiple requests, which typically execute in parallel, into a single "Multi" request.

This approach represents a trade-off between capacity and performance.

The overall implementation is executed in a two-step process:

  1. A new post-processor is introduced that analyzes the fetch tree and generates new "Multi" nodes for fetches that can be combined.
  2. A new loader is implemented to manage these "Multi" nodes and construct the necessary query.

The Multi Node Post Processor

This post-processor analyzes parallel nodes and generates multi nodes for all Entity or Batch Entity fetches directed to the same data source (subgraph).

The Multi Loader

This loader operates in a two-step manner: first, it utilizes all the fetches to create a Multi request using aliases; second, it disentangles the response into the appropriate paths for each fetch.

For example, two fetches like this would be combined into a single Multi fetch:

query($representations: [_Any!]!) {
_entities(representations: $representations) {
	... on Product {
		__typename
		otherField
	}
}

query($representations: [_Any!]!) {
_entities(representations: $representations) {
	... on User {
		__typename
		someField
	}
}

Result:

query MultiFetch($f_1representations: [_Any!]!, $f_2representations: [_Any!]!) {
f_1:
		_entities(representations: $f_1representations) {
			... on Product {
				__typename
				otherField
			}
		}
f_2:
		_entities(representations: $f_2representations) {
			... on User {
				__typename
				someField
			}
		}
}

Limitations and Future Work

I would like to emphasize that this is a proof of concept (PoC) and that several adjustments are necessary for proper implementation:

  1. The post processor should ideally generate the correct multi-fetch query string.
  2. Instead of using string concatenation, we should utilize variable rendering.
  3. Currently, only entity and batch entity fetches are supported; we should consider adding support for single fetches as well.
  4. More advanced merging. Eg. if two requests ask for the the same entity type with the same attributes this can be merged in to a single aliased request.
  5. Look into request skipping, this is currently not supported
  6. Most likely much more :)

@alexus37 alexus37 changed the title Multi POC: Implement Multi fetch Nov 28, 2024
@alexus37 alexus37 marked this pull request as ready for review November 28, 2024 16:07
processor.ProcessFetchTree(input)
expected := resolve.Sequence(
resolve.Single(&resolve.SingleFetch{FetchDependencies: resolve.FetchDependencies{FetchID: 0}}),
resolve.Parallel(

Choose a reason for hiding this comment

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

You can add an additional post processing step that replaces a Parallel Node in the root with the child if the child length is 1.

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