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

Improve directive and fragment removal logic #6322

Closed
wants to merge 16 commits into from

Conversation

mtsmfm
Copy link
Contributor

@mtsmfm mtsmfm commented May 20, 2020

Fixes #6311 and incorporates changes from #7488.

@benjamn benjamn self-assigned this Jun 23, 2020
@jpvajda jpvajda added 🏓 awaiting-contributor-response requires input from a contributor 🏓 awaiting-team-response requires input from the apollo team and removed 🏓 awaiting-contributor-response requires input from a contributor labels May 3, 2022
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

Hey @mtsmfm 👋! Thanks for your patience! I've noticed your 2 tests seem to conflict in logic with each other. I'd like to see if we can keep the behavior as consistent as possible. I see this is related to an issue that worked in v2 but not in v3. Can you confirm this is still an issue in the latest versions of Apollo (v3.7.1 at the time of this writing)?

Feel free to close or amend this PR as need be. Thanks!

src/utilities/graphql/__tests__/transform.ts Show resolved Hide resolved
@changeset-bot
Copy link

changeset-bot bot commented Feb 2, 2023

🦋 Changeset detected

Latest commit: e394739

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

This PR includes changesets to release 1 package
Name Type
@apollo/client 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

@alessbell alessbell changed the base branch from master to main February 2, 2023 17:47
@@ -218,10 +218,6 @@ describe('Basic resolver capabilities', () => {
const serverQuery = gql`
fragment Foo on Foo {
bar
...Foo2
Copy link
Contributor

@alessbell alessbell Feb 2, 2023

Choose a reason for hiding this comment

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

This test was added recently and has a __typename-only fragment which would have been previously removed from the document. It can simply be omitted here.

@alessbell alessbell removed the request for review from StephenBarlow February 2, 2023 20:01
Consider the following schema on the server:

```graphql
interface Character {
  appearsIn: [Episode]!
}

type Human implements Character {
  appearsIn: [Episode]!
  height: Int
}
```

Then the following addition on the client:

```graphql
type Droid implements Character {
  appearsIn: [Episode]!
  primaryFunction: String
}
```

With the following query:

```graphql
query HeroForEpisode($ep: Episode!) {
  hero(episode: $ep) {
    ... on Human {
      height
    }
    ... on Droid @client {
      primaryFunction
    }
  }
}
```

The server will complain that the fragment spread has no type overlap
with parent, as the server does not have knowledge of the `Droid` type.

This commit strips out inline fragments which use a client directive,
allowing them to be resolved via local state.
@alessbell
Copy link
Contributor

alessbell commented Feb 3, 2023

Doing some tidying before merging this: I cherry-picked the commit from #7488 since the PRs are somewhat related (and both needed minor updates based on when they were opened), and just removed the removeTypenameOnlyFragment option which wasn't necessary from what I can see.

The only outstanding item here is I'd like to move the second call to visit back out of removeDirectivesFromDocument since this performs an entire second traversal of the document AST, and removeDirectivesFromDocument is called (via other fns) in relatively hot paths of the library. That and I forgot to also cherry-pick the changeset from #7488 :)

@alessbell
Copy link
Contributor

After discussing with @benjamn, I've done some light refactoring here (there are more improvements we can make in the future, but for now I was able to remove one call to visit).

This PR is ready for review 😄

@alessbell alessbell changed the title Keep fragment when it doesn't have @client directive Improve directive and fragment removal logic Feb 6, 2023
@phryneas phryneas self-requested a review February 7, 2023 11:38
@alessbell alessbell added 🐞 bug 🥚 backwards-compatible for PRs that do not introduce any breaking changes and removed 🏓 awaiting-team-response requires input from the apollo team labels Feb 7, 2023
@alessbell alessbell self-assigned this Feb 7, 2023
@alessbell
Copy link
Contributor

alessbell commented Feb 10, 2023

@benjamn after our last chat about this I added some tests where the fragment appears before the query, and the one failing test is for should remove @client and __typename only fragment, but this test is also failing on main. (The fragment spread is never removed because it has not yet been registered via fragmentSpreadsInUse/not an issue introduced by removing the visit from removeClientSetsFromDocument.)

I've kept the test with a FIXME comment pointing to #10539 and .skip it for now. Otherwise I think this PR is ready for a final review 👍

@alessbell
Copy link
Contributor

Closing in favor of #10560.

@alessbell alessbell closed this Feb 15, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🥚 backwards-compatible for PRs that do not introduce any breaking changes 🐞 bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fragment which has __typename only is removed by removeClientSetsFromDocument
6 participants