Skip to content

Fix: Empty strings converted to undefined in string interpolation #9354

@CStre

Description

@CStre

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @graphql-mesh/string-interpolation for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@graphql-mesh/string-interpolation/cjs/interpolator.js b/node_modules/@graphql-mesh/string-interpolation/cjs/interpolator.js
index 885e9b1..333d0e3 100644
--- a/node_modules/@graphql-mesh/string-interpolation/cjs/interpolator.js
+++ b/node_modules/@graphql-mesh/string-interpolation/cjs/interpolator.js
@@ -96,7 +96,12 @@ class Interpolator {
                 result === '' &&
                 strOrEmptyStr.startsWith('{') &&
                 strOrEmptyStr.endsWith('}')) {
-                return undefined;
+                // Only return undefined when the data value was actually missing (undefined).
+                // An explicit empty string '' is a valid value and must be preserved.
+                const rawDataValue = this.applyData(rules[0].key, data);
+                if (rawDataValue === undefined) {
+                    return undefined;
+                }
             }
             return result;
         }
diff --git a/node_modules/@graphql-mesh/string-interpolation/esm/interpolator.js b/node_modules/@graphql-mesh/string-interpolation/esm/interpolator.js
index 6a302cf..00559c9 100644
--- a/node_modules/@graphql-mesh/string-interpolation/esm/interpolator.js
+++ b/node_modules/@graphql-mesh/string-interpolation/esm/interpolator.js
@@ -92,7 +92,12 @@ export class Interpolator {
                 result === '' &&
                 strOrEmptyStr.startsWith('{') &&
                 strOrEmptyStr.endsWith('}')) {
-                return undefined;
+                // Only return undefined when the data value was actually missing (undefined).
+                // An explicit empty string '' is a valid value and must be preserved.
+                const rawDataValue = this.applyData(rules[0].key, data);
+                if (rawDataValue === undefined) {
+                    return undefined;
+                }
             }
             return result;
         }

The current implementation treats explicit empty strings as undefined values, causing silent data loss in string interpolation.

Problem:
When a template variable resolves to an empty string and the entire interpolated value consists only of that placeholder (e.g., "{fieldName}"), the Interpolator class unconditionally returns undefined instead of the empty string itself.

Example:

Template: "{fieldName}"
Data: { fieldName: "" }
Expected Result: ""
Actual Result: undefined ❌

This breaks GraphQL queries and mutations where empty strings are legitimate and required values, resulting in unexpected undefined fields in the final output.

Solution:
The provided fix distinguishes between truly missing/undefined values and explicit empty strings by checking the raw data value before deciding to return undefined.

The fix:

  1. Retrieves the raw data value using this.applyData(rules[0].key, data)
  2. Only returns undefined if the raw value is actually undefined (genuinely missing)
  3. Preserves explicit empty strings as valid interpolated values

This ensures that empty strings are treated as legitimate values while maintaining backward compatibility for undefined/missing data.

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions