Skip to content

Conversation

@tazsingh
Copy link
Contributor

@tazsingh tazsingh commented Jan 6, 2026

🚨 IMPORTANT: Please do not create a Pull Request without creating an issue first.

Any change needs to be discussed before proceeding. Failure to do so may result in the rejection of
the pull request.

Description

When using stringInterpolator.parse() with an object or array value, the interpolation was producing [object Object] instead of preserving the object or serializing it properly.

This change modifies the applyRule() method in interpolator.js to:

  1. Return objects/arrays directly when the entire template string is just a placeholder (e.g., {user})
  2. JSON stringify objects/arrays when embedded in a larger string (e.g., User: {user})

Fixes #9113

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as
    expected)
  • This change requires a documentation update

Screenshots/Sandbox (if appropriate/relevant):

const context = { user: { name: 'John', age: 30 } };

// Before fix:
stringInterpolator.parse('{user}', context);     // "[object Object]"
stringInterpolator.parse('User: {user}', context); // "User: [object Object]"

// After fix:
stringInterpolator.parse('{user}', context);     // { name: 'John', age: 30 }
stringInterpolator.parse('User: {user}', context); // 'User: {"name":"John","age":30}'

How Has This Been Tested?

Added unit tests in packages/string-interpolation/tests/parse-interpolation-strings.test.ts:

  • Returns object directly when entire string is a placeholder
  • Returns array directly when entire string is a placeholder
  • JSON stringifies object when embedded in a larger string
  • JSON stringifies array when embedded in a larger string

Test Environment:

  • OS: macOS
  • @graphql-mesh/string-interpolation: latest
  • NodeJS: 22.14.0

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works, and I have added a
    changeset using yarn changeset that bumps the version
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

This fix is particularly useful when using interpolation in configurations where you want to pass through object values rather than having them converted to strings. The implementation checks if the modified data is an object and handles it appropriately based on whether it's a standalone placeholder or embedded in text.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2026

📝 Walkthrough

Summary by CodeRabbit

Bug Fixes

  • Improved string interpolation to properly handle objects—returning them directly when they represent the entire result, and JSON-serializing them when embedded within text.

Tests

  • Added test coverage for object and array placeholder interpolation scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

This PR fixes string interpolation of objects and arrays in the @graphql-mesh/string-interpolation package. Objects and arrays that serve as the sole placeholder are now returned directly instead of being stringified to [object Object]. When embedded within larger strings, they are JSON-stringified instead.

Changes

Cohort / File(s) Summary
Configuration
.changeset/nine-plums-ask.md
Changeset entry marking a patch for "Fix string interpolation of objects".
Core Implementation
packages/string-interpolation/src/interpolator.js
Modified applyRule to conditionally handle object results: returns objects/arrays directly for placeholder-only templates; JSON-stringifies them when embedded in larger strings.
Test Coverage
packages/string-interpolation/tests/parse-interpolation-strings.test.ts
Added test cases for placeholder-only object/array returns and embedded object/array JSON-stringification scenarios.

Possibly related issues

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hopping through strings with care so true,
Objects no longer hide in "[object Object]" view,
Placeholders return their rightful form,
Embedded ones stringify—all proper and warm!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: preventing objects from being converted to the string '[object Object]' during string interpolation.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, explaining the problem, solution, test coverage, and providing concrete before/after examples.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/string-interpolation/tests/parse-interpolation-strings.test.ts (1)

23-33: Good coverage for embedded object stringification.

These tests properly validate that objects and arrays are JSON-stringified when embedded in larger strings.

Consider adding tests for additional edge cases:

Suggested additional test cases
it('handles multiple object placeholders in one string', () => {
  const context = { user: { name: 'John' }, role: { type: 'admin' } };
  const result = stringInterpolator.parse('{user} has role {role}', context);
  expect(result).toBe('{"name":"John"} has role {"type":"admin"}');
});

it('handles nested objects', () => {
  const context = { user: { name: 'John', address: { city: 'NYC' } } };
  const result = stringInterpolator.parse('User: {user}', context);
  expect(result).toBe('User: {"name":"John","address":{"city":"NYC"}}');
});

it('handles Date objects', () => {
  const date = new Date('2024-01-01T00:00:00Z');
  const context = { timestamp: date };
  const result = stringInterpolator.parse('{timestamp}', context);
  expect(result).toEqual(date); // returned directly
});

it('handles Date objects in strings', () => {
  const date = new Date('2024-01-01T00:00:00Z');
  const context = { timestamp: date };
  const result = stringInterpolator.parse('Time: {timestamp}', context);
  expect(result).toBe(`Time: ${JSON.stringify(date)}`);
});
packages/string-interpolation/src/interpolator.js (1)

129-139: Consider adding error handling for JSON.stringify as defensive programming.

The JSON.stringify call on line 137 could theoretically throw errors on circular references or BigInt values. While the test suite and actual usage patterns (headers, URLs, query parameters) don't demonstrate this occurring in practice, wrapping it in a try-catch with a fallback to String() would be more robust:

  // For objects embedded in a larger string, JSON stringify them
  if (typeof modifiedData === 'object' && modifiedData !== null) {
-   return str.replace(rule.replace, JSON.stringify(modifiedData));
+   try {
+     return str.replace(rule.replace, JSON.stringify(modifiedData));
+   } catch (e) {
+     return str.replace(rule.replace, String(modifiedData));
+   }
  }
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cff8dff and 56b5f59.

📒 Files selected for processing (3)
  • .changeset/nine-plums-ask.md
  • packages/string-interpolation/src/interpolator.js
  • packages/string-interpolation/tests/parse-interpolation-strings.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: unit / node 20
  • GitHub Check: integration / node 18
  • GitHub Check: integration / node 22
  • GitHub Check: e2e / node v22
  • GitHub Check: e2e / node v18
  • GitHub Check: e2e / node v20
  • GitHub Check: unit / node 22
  • GitHub Check: apollo-federation-compatibility
  • GitHub Check: check
  • GitHub Check: unit / node 18
  • GitHub Check: integration / node 20
🔇 Additional comments (2)
packages/string-interpolation/tests/parse-interpolation-strings.test.ts (1)

11-21: Excellent test coverage for the direct return behavior.

These tests clearly validate that objects and arrays are returned directly when the entire template is a single placeholder, which is the core feature of this PR.

.changeset/nine-plums-ask.md (1)

1-5: LGTM! Changeset is properly formatted.

The changeset correctly documents this as a patch-level fix for the @graphql-mesh/string-interpolation package with a clear description.

@ardatan ardatan merged commit 30e8e32 into ardatan:master Jan 7, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String Interpolator serializes toString instead of the object

2 participants