Skip to content

Merging GraphQL Queries Together #406

Closed
@theorygeek

Description

@theorygeek

For those of use who use Relay, we sometimes encounter N+1 node queries:

query {
  node(id: "...") { ... }
}

Relay might execute hundreds of these queries at once. For example, you might be displaying a table of data on the page, and maybe the user is able to add a new column. Relay might fetch the data for the new column for each row individually.

I wrote helper for GraphQL that can take these queries and combine them together. You can check it out here: https://gist.github.com/theorygeek/85b2f7df6931a0eb3f06141ef1e57f30

Example usage:

query_1_text = "..."
query_2_text = "..."
query_1_vars = {}
query_2_vars = {}

merged = GraphQL::Language.merge_queries(
  [query_1_text, query_2_text],
  [query_1_vars, query_2_vars]
)

# Returns an array of responses, in the same order as the queries you provided
result = merged.execute(YourSchema, context: your_context)

So I thought I'd ask for some feedback:

  • Would it be useful for this to be part of the gem's codebase?
  • Any problems (or other feedback) with how it's implemented?

One particular challenge is merging queries that specify different operations (eg, a mutation with a query). And I'm not really sure the impact on subscriptions, @defer, @stream, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions