Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mergeDeep): handle undefined sources correctly #7012

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ardatan
Copy link
Owner

@ardatan ardatan commented Mar 7, 2025

Fixes #7011

The following inputs and outputs are corrected;

  • mergeDeep([{a:2} undefined]) - Any nullish values should be ignored so it should return {a:2}
  • mergeDeep([]) - no sources should return undefined
  • mergeDeep([undefined]) - no sources should return undefined

Copy link

changeset-bot bot commented Mar 7, 2025

🦋 Changeset detected

Latest commit: 14365b3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 26 packages
Name Type
@graphql-tools/utils Patch
@graphql-tools/executor Patch
@graphql-tools/graphql-tag-pluck Patch
@graphql-tools/import Patch
@graphql-tools/links Patch
@graphql-tools/load Patch
@graphql-tools/merge Patch
@graphql-tools/mock Patch
@graphql-tools/node-require Patch
@graphql-tools/relay-operation-optimizer Patch
@graphql-tools/resolvers-composition Patch
@graphql-tools/schema Patch
@graphql-tools/apollo-engine-loader Patch
@graphql-tools/code-file-loader Patch
@graphql-tools/git-loader Patch
@graphql-tools/github-loader Patch
@graphql-tools/graphql-file-loader Patch
@graphql-tools/json-file-loader Patch
@graphql-tools/module-loader Patch
@graphql-tools/url-loader Patch
@graphql-tools/executor-apollo-link Patch
@graphql-tools/executor-envelop Patch
@graphql-tools/executor-legacy-ws Patch
@graphql-tools/executor-urql-exchange Patch
@graphql-tools/executor-yoga Patch
graphql-tools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ardatan ardatan marked this pull request as ready for review March 7, 2025 12:03
Copy link
Contributor

coderabbitai bot commented Mar 7, 2025

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved deep merging behavior to correctly ignore nullish inputs and handle empty arrays, ensuring more reliable results.
  • Tests

    • Added new test cases to validate the updated merging functionality and confirm proper behavior in edge-case scenarios.

Walkthrough

This pull request introduces a patch for the mergeDeep function in the @graphql-tools/utils library, improving its handling of nullish values and empty arrays. The function now returns undefined for empty input arrays and skips over nullish values during the merging process. Additionally, it returns the first source when only one is provided. New test cases have been added to ensure these behaviors are correctly implemented.

Changes

File Path(s) Change Summary
.changeset/chilly-icons-guess.md
packages/utils/src/mergeDeep.ts
Updated mergeDeep to handle nullish values and empty inputs. Added checks for empty sources (returning undefined) and single element sources (returning the element directly), and ensured nullish values are skipped during merging.
packages/utils/tests/mergeDeep.test.ts Introduced three new test cases to verify that mergeDeep filters out nullish values, merges non-empty objects, and returns undefined for an empty sources array.

Sequence Diagram(s)

sequenceDiagram
    participant C as Caller
    participant M as mergeDeep Function
    participant S as Source Item

    C->>M: Call mergeDeep(sources)
    alt sources array is empty
        M-->>C: Return undefined
    else sources length is one
        M-->>C: Return the single source
    else
        loop For each source in sources
            alt Source is nullish
                M->>M: Skip this source
            else
                M->>M: Merge source into result
            end
        end
        M-->>C: Return merged result
    end
Loading

Poem

I'm a rabbit hopping through the code,
Skipping over bugs along my winding road.
I fixed mergeDeep with a nimble, clever twist,
Nullish values now fade into a mist.
With tests that crunch like carrots at night,
I celebrate our patch with pure delight! 🥕✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/utils/src/mergeDeep.ts

Oops! Something went wrong! :(

ESLint: 9.21.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a82ff87 and 14365b3.

📒 Files selected for processing (2)
  • .changeset/chilly-icons-guess.md (1 hunks)
  • packages/utils/src/mergeDeep.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/utils/src/mergeDeep.ts
  • .changeset/chilly-icons-guess.md
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v15
  • GitHub Check: deployment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@theguild-bot
Copy link
Collaborator

theguild-bot commented Mar 7, 2025

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-tools/executor 1.4.5-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/executor-apollo-link 1.0.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/executor-envelop 3.0.26-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/executor-legacy-ws 1.1.16-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/executor-urql-exchange 1.0.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/executor-yoga 3.0.26-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/graphql-tag-pluck 8.3.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
graphql-tools 9.0.17-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/import 7.0.17-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/links 9.0.26-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/load 8.0.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/apollo-engine-loader 8.0.19-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/code-file-loader 8.1.19-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/git-loader 8.0.23-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/github-loader 8.0.19-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/graphql-file-loader 8.0.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/json-file-loader 8.0.17-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/module-loader 8.0.17-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/url-loader 8.0.30-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/merge 9.0.23-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/mock 9.0.21-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/node-require 7.0.19-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/relay-operation-optimizer 7.0.18-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/resolvers-composition 7.0.17-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/schema 10.0.22-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎
@graphql-tools/utils 10.8.5-alpha-20250307121300-14365b34ed1b50dd48041446ce0a72986ae45a1c npm ↗︎ unpkg ↗︎

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 (1)
packages/utils/tests/mergeDeep.test.ts (1)

76-86: Consider adding a test for array with only undefined values

The current tests cover empty arrays and arrays with some undefined values, but there's no explicit test for mergeDeep([undefined]) which is mentioned in the changeset. Adding such a test would ensure complete coverage of the scenarios described in the changeset.

  it('returns undefined when an empty sources array passed', () => {
    expect(mergeDeep([])).toEqual(undefined);
  });
+
+ it('returns undefined when array contains only undefined values', () => {
+   expect(mergeDeep([undefined])).toEqual(undefined);
+   expect(mergeDeep([undefined, null, undefined])).toEqual(undefined);
+ });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff11a77 and a82ff87.

📒 Files selected for processing (3)
  • .changeset/chilly-icons-guess.md (1 hunks)
  • packages/utils/src/mergeDeep.ts (2 hunks)
  • packages/utils/tests/mergeDeep.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Bun (ubuntu-latest) and GraphQL v16
  • GitHub Check: Full Check on GraphQL v16
  • GitHub Check: Unit Test on Bun (ubuntu-latest) and GraphQL v15
  • GitHub Check: deployment
🔇 Additional comments (6)
.changeset/chilly-icons-guess.md (1)

1-11: Well-documented changeset for the bug fix

The changeset clearly describes the bug fix in the mergeDeep function and provides specific examples of inputs and their expected outputs. This documentation helps users understand the new behavior and is aligned with the implementation changes.

packages/utils/src/mergeDeep.ts (2)

15-20: LGTM: Early return for edge cases improves function clarity

These additions handle the edge cases as described in the changeset:

  1. Returning undefined when an empty array is passed
  2. Returning the single source when only one item is provided

This is a good optimization that avoids unnecessary processing when the result is deterministic.


63-65: LGTM: Properly skipping null/undefined values

This change correctly implements the behavior described in the changeset, where nullish values in the sources array are ignored during the merge process. This prevents errors that could occur when trying to process null or undefined as objects.

packages/utils/tests/mergeDeep.test.ts (3)

76-78: Good test for filtering nullish values

This test case properly verifies that the function ignores undefined values in the sources array, aligning with the fix implemented in the main function.


80-82: Good test for single empty object

This test confirms that the function correctly handles a source array containing only an empty object, returning that empty object as expected.


84-86: Good test for empty sources array

This test verifies the new behavior where an empty sources array returns undefined, which is consistent with the function implementation.

Copy link
Contributor

github-actions bot commented Mar 7, 2025

💻 Website Preview

The latest changes are available as preview in: https://ed121853.graphql-tools.pages.dev

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.

mergeDeep behaviour with undefined values changed in 10.6.2
2 participants