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

Remove duplicate fragments from resolved request document #460

Closed
wants to merge 3 commits into from
Closed

Remove duplicate fragments from resolved request document #460

wants to merge 3 commits into from

Conversation

moalazzam
Copy link

In scenarios where a fragment is nested and used multiple times, graphql-request includes the fragments' bodies multiple times.

Use case example: Unleash the power of Fragments with GraphQL Codegen

Scenario

const FragmentB = gql`
  fragment FragmentB on Other {
    number
  }
`;

const FragmentA = gql`
  fragment FragmentA on Something {
    value
    other {
      ...FragmentB
    }
  }
  ${FragmentB}
`;

const Query = gql`
  query Query {
    a {
      ...FragmentA
    }
    b {
      ...FragmentB
    }
  }
  ${FragmentA}
  ${FragmentB}
`;

Current output

  query Query {
    a {
      ...FragmentA
    }
    b {
      ...FragmentB
    }
  }

  fragment FragmentA on Something {
    value
    other {
      ...FragmentB
    }
  }

  fragment FragmentB on Other {
    number
  }

  fragment FragmentB on Other {
    number
  }

Pull request output

  query Query {
    a {
      ...FragmentA
    }
    b {
      ...FragmentB
    }
  }

  fragment FragmentA on Something {
    value
    other {
      ...FragmentB
    }
  }

  fragment FragmentB on Other {
    number
  }

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Feb 21, 2023

Thanks @moalazzam !

Was going to say can we add a test please. However, if we can use the graphql library for this instead we don't need tests or new custom logic. I would prefer that. Want to take a crack at that?

@moalazzam
Copy link
Author

I have updated the code to use the visit function from the graphql library, waiting for review my changes

@moalazzam moalazzam changed the title Fragments bodies added multiple times Remove duplicate fragments from resolved request document Feb 22, 2023
@jasonkuhrt
Copy link
Member

Going to close this. Feel free to open an issue to motivate a problem with graphql-request. But I don't think this is ready for implementation.

@jasonkuhrt jasonkuhrt closed this Feb 22, 2023
@moalazzam moalazzam deleted the dedup-fragments branch February 22, 2023 15:58
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