Skip to content

Commit 83f91d5

Browse files
committed
relay-transforms: optionally use native gql fragment spreads for unmasked fragments
When Relay encountered a fragment spread with the `@relay(mask: false)` directive, it would previously inline that fragment text into the query itself. This is completely valid and equivalent to a native GraphQL fragment spread. That inlining can dramatically increase the size of serialize for a query with multiple fragments. Such repetition compresses extremely well, but clients and servers still pay the cost of having uncompressed strings in-memory (plus compress/decompress time). Allow relay users to opt out of fragment inlining for `@relay(mask: false)` fragment spreads in queries, using a native GraphQL fragment spread. The reader AST and type generation are not affected so there's no impact to the relay runtime.
1 parent a6f0fb3 commit 83f91d5

File tree

6 files changed

+433
-2
lines changed

6 files changed

+433
-2
lines changed

compiler/crates/common/src/feature_flags.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ pub struct FeatureFlags {
167167
/// across a number of diffs.
168168
#[serde(default)]
169169
pub legacy_include_path_in_required_reader_nodes: FeatureFlag,
170+
171+
/// Produce native GraphQL fragment spreads when spreading a fragment with
172+
/// @relay(mask: false) into a query, instead of inlining the contents of
173+
/// that fragment for each use. This significantly reduces the uncompressed
174+
/// size of query text when several mask:false fragment spreads are present,
175+
/// but has no impact on masking behavior.
176+
#[serde(default)]
177+
pub use_native_fragment_spreads_for_unmasked_fragments: bool,
170178
}
171179

172180
#[derive(Debug, Deserialize, Clone, Serialize, Default, JsonSchema)]

compiler/crates/relay-compiler/relay-compiler-config-schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,11 @@
829829
}
830830
]
831831
},
832+
"use_native_fragment_spreads_for_unmasked_fragments": {
833+
"description": "Produce native GraphQL fragment spreads when spreading a fragment with @relay(mask: false) into a query, instead of inlining the contents of that fragment for each use. This significantly reduces the uncompressed size of query text when several mask:false fragment spreads are present, but has no impact on masking behavior.",
834+
"default": false,
835+
"type": "boolean"
836+
},
832837
"use_reader_module_imports": {
833838
"description": "Generate the `moduleImports` field in the Reader AST.",
834839
"default": {
@@ -1067,6 +1072,7 @@
10671072
"text_artifacts": {
10681073
"kind": "disabled"
10691074
},
1075+
"use_native_fragment_spreads_for_unmasked_fragments": false,
10701076
"use_reader_module_imports": {
10711077
"kind": "disabled"
10721078
}

0 commit comments

Comments
 (0)