Skip to main content

Overview

The DeduplicateJoinsPlugin automatically removes duplicate join clauses from your queries. This is particularly useful when building complex queries dynamically or when using query composition patterns that might inadvertently add the same join multiple times.

Installation

Usage Example

Without the Plugin

With the Plugin

Methods

transformQuery

Transforms the query by removing duplicate join clauses. Parameters:
  • args.queryId - Unique identifier for the query
  • args.node - The root operation node to transform
Returns: The transformed operation node with duplicate joins removed Source: ~/workspace/source/src/plugin/deduplicate-joins/deduplicate-joins-plugin.ts:19

transformResult

Passes through the result unchanged. This plugin only modifies queries, not results. Parameters:
  • args.queryId - Unique identifier for the query
  • args.result - The query result
Returns: A promise resolving to the original, unmodified result Source: ~/workspace/source/src/plugin/deduplicate-joins/deduplicate-joins-plugin.ts:23

Real-World Use Cases

Dynamic Query Building

When building queries dynamically, you might add joins conditionally without tracking whether they’ve already been added:

Query Composition with Helper Functions

Reusable Query Fragments

Complex Multi-Table Queries

How It Works

The plugin uses a DeduplicateJoinsTransformer that:
  1. Traverses the operation node tree
  2. Identifies all join nodes in the query
  3. Compares join nodes for equality
  4. Removes subsequent joins that are identical to earlier ones
  5. Preserves the order of the first occurrence of each unique join

When Joins Are Considered Duplicates

Two joins are considered duplicates if they have:
  • Same join type (INNER, LEFT, RIGHT, etc.)
  • Same table being joined
  • Same join condition
  • Same table alias (if any)

Performance Impact

The plugin adds minimal overhead:
  • Only processes queries that have joins
  • Comparison is done using operation node equality
  • No runtime performance impact on query execution
  • Prevents unnecessary database work by eliminating redundant joins

Per-Query Usage

You can apply the plugin to specific queries instead of globally:

See Also