evenchange4 / graphql.macro

Compile GraphQL AST at build-time with babel-plugin-macros.
MIT License
239 stars 21 forks source link

Handle duplicate fragments #90

Closed mxstbr closed 5 years ago

mxstbr commented 5 years ago

I have a query that uses a fragment directly, and another fragment that uses the same fragment in a nested manner:

query getViewerCommunities($id: ID!) {
  user(id: $id) {
    ...userInfo
    communities {
      participants {
        ...communityParticipantInfo
      }
    }
  }
}
${userInfoFragment}
${communityParticipantInfoFragment}

With a communityParticipantInfo fragment that looks like this:

fragment communityParticipantInfo on CommunityParticipant {
  idm
  user {
    ...userInfo
  }
}
${userInfoFragment}

As you can see, the userInfoFragment is repeated twice. Using graphql-tag at runtime correctly deduplicates that fragment and everything works perfectly.

Unfortunately, graphql.macro does not deduplicate the fragment and my server thusly throws an error saying "Duplicate fragment" and rejects the query entirely.

FezVrasta commented 5 years ago

@evenchange4 are you going to work on this? I can give it a shot otherwise

evenchange4 commented 5 years ago

Verified in my project. It should be fixed with version v1.4.2 now. Thanks @FezVrasta !

danielkcz commented 4 years ago

@evenchange4 I am using 1.4.2 and this is still the issue. Fragments are duplicated and the server rejects it. I've checked the test you added for this and I don't think it's sufficient. Apparently, something else is happening when fragments are nested over several documents. I've verified that with repeated fragments over a single documents the deduplication indeed works.

query {
  order {
    items {
      ...FItemDetail
      subitems {
         ...FItemDetail
      }
    }
  }
}
fragment FItemDetail on Item {
  price {
     ...FPrice
  }
}
fragment FPrice on PriceConverted {
  value
  currency
}