Skip to content

Commit b64c530

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 afb9b5d commit b64c530

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
@@ -803,6 +803,11 @@
803803
}
804804
]
805805
},
806+
"use_native_fragment_spreads_for_unmasked_fragments": {
807+
"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.",
808+
"default": false,
809+
"type": "boolean"
810+
},
806811
"use_reader_module_imports": {
807812
"description": "Generate the `moduleImports` field in the Reader AST.",
808813
"default": {
@@ -1040,6 +1045,7 @@
10401045
"text_artifacts": {
10411046
"kind": "disabled"
10421047
},
1048+
"use_native_fragment_spreads_for_unmasked_fragments": false,
10431049
"use_reader_module_imports": {
10441050
"kind": "disabled"
10451051
}

0 commit comments

Comments
 (0)