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] [#149] do not merge/extract fragment fields into node fields #151

Merged
merged 2 commits into from
Jun 10, 2024

Conversation

kindermax
Copy link
Collaborator

@kindermax kindermax commented Jun 5, 2024

If there is no such field in node, leave field in fragment

There was a part of algo in nodes merging which will extract fields from fragment if same fields exist in node - this is to prevent same field to be resolved multiple times. The simplest example is { user { id ... on User { id } } } would become { user { id } } so id field will be resolved only once.

But previous algo (before this fix) would merge eagerly, so even if no field exits in node, algo will try to eliminate fragment at all, for example { user { ... on User { id } } } would become { user { id } } - fragment is eliminated.

This behavior breaks union queries and we must not touch union fragment, since this breaks unions. This fix tells algo to merge less eagerly, thas is - if fragment field not exist in node, we leave this field in fragment since there is nothing to optimize, no fields duplication will occur.

Note that although we do not merge fragment field to node fields, we still merge fields for the very same fragment field (see _merge_link).

Here are the rules:

{ 
  user { 
    ... on User { id }  # <- leave this fragment (this is fixed by PR)
  } 
}
{ 
  user { 
    id
    ... on User { id } # <- merge id into parent node
  } 
}
# to
{ 
  user { 
    id
  } 
}
{ 
  user { 
    id
    ... on User { id email } # <- merge id field into parent node
  } 
}
# to
{ 
  user { 
    id
    ... on User { email } 
  } 
}
{ 
  user { 
    ... on User {  # <- leave this fragment (this is fixed by PR)
      id
      company {
        id
        .. on Company {  # <- but merge this fragment
          id
        }
      } 
    } 
  } 
}
# to
{ 
  user { 
    ... on User { 
      id
      company {
        id
      } 
    } 
  } 
}

@kindermax kindermax requested a review from n4mespace June 5, 2024 18:55
… there is no such field in node

There was a part of algo in nodes merging which will extract fields from fragment if same fields exist in node - this is to prevent same field to be resolved multiple times. The simplest example is `{ user { id ... on User { id } } }` would become `{ user { id } }` so `id` field will be resolved only once.

But previous algo (before this fix) would merge eagerly, so even if no field exits in not, algo will try to eliminate fragment at all, for example `{ user { ... on User { id } } }` would become `{ user { id } }` - fragment is eliminated.

This behavior breaks union queries and we must not touch union fragment, since this breaks unions. This fix tells algo to merge less eagerly, thas is - if fragment field not exist in node, we leave this field in fragment since there is nothing to optimize, no fields duplication will occur.

Note that although we do not merge fragment field to node fields, we still merge
fields for fragment field (see _merge_link).
@kindermax kindermax force-pushed the fix-fragments-merging branch from 7fd0f95 to 6fe5e89 Compare June 5, 2024 19:04
Copy link
Collaborator

@n4mespace n4mespace left a comment

Choose a reason for hiding this comment

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

lgtm

@kindermax kindermax merged commit 8b99382 into master Jun 10, 2024
7 checks passed
@kindermax kindermax deleted the fix-fragments-merging branch June 10, 2024 12:11
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.

2 participants