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

Fragments bodies where added multiple times (duplicates) #278

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

Conversation

lanwin
Copy link

@lanwin lanwin commented Dec 4, 2019

In scenarios where a fragment is nested and used multiple times graphql-tag includes the fragments bodies (source) multiple times. While its obviously wrong, our graphql server also rejecting that requests.

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 (v2.10.1 and before)

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

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

  fragment FragmentB on Other {
    number
  }



  fragment FragmentB on Other {
    number
  }

Expected output (this Pullrequest)

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

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

  fragment FragmentB on Other {
    number
  }

I also added a test for this scenario.

@apollo-cla
Copy link

@lanwin: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@hcharley
Copy link

@lanwin wondering if you could follow up with @apollo-cla and get this PR on track. This would help me solve the issue I'm having as well with codegen and graphql-tag

@lanwin
Copy link
Author

lanwin commented Dec 15, 2019

@lanwin wondering if you could follow up with @apollo-cla and get this PR on track. This would help me solve the issue I'm having as well with codegen and graphql-tag

I already did this a while ago. As you can see, the pullrequest is mergeable.

@ElliottRoche
Copy link

Is this getting merged any time soon?

@lanwin
Copy link
Author

lanwin commented Jan 19, 2020

If you look closely at the commit history, there wasnt any commit since nearly one year ago. So it seems it will not happen in the near future.

@lanwin
Copy link
Author

lanwin commented Mar 23, 2020

If you are suffering from this problem. Ive created a minimal version of graphql-tag which outputs only the source of the query and did not have this problem. https://gist.github.com/lanwin/bdc33d0bf291851bda48d17f98b98ff2

@dimikot
Copy link

dimikot commented Jun 26, 2020

@lanwin, I wrote an extension which wraps gql and is a slight improvement of your approach. To use, just replace import gql from "graphql-tag" to import gql from "gql-dedup", no other changes are required (and for TS, it's ought to produce strongly typed DocumentNode results, similar to graphql-tag's).

https://www.npmjs.com/package/gql-dedup

@zaguiini
Copy link

zaguiini commented Sep 2, 2020

graphql-tag is being released but this is not getting merged 😕

@sparebytes
Copy link

I've attempted to apply this patch for [email protected] but not having any luck. I'm using graphql-code-generator and I believe there might a similar issue affecting the typescript-operations and typed-document-node plugins.

Regardless, I'l leave my patch-package file in case anyone else finds it useful:

patches/graphql-tag+2.12.4.patch
diff --git a/node_modules/graphql-tag/lib/graphql-tag.umd.js b/node_modules/graphql-tag/lib/graphql-tag.umd.js
index 629f159..ce3d19b 100644
--- a/node_modules/graphql-tag/lib/graphql-tag.umd.js
+++ b/node_modules/graphql-tag/lib/graphql-tag.umd.js
@@ -87,7 +87,11 @@
        var result = literals[0];
        args.forEach(function (arg, i) {
            if (arg && arg.kind === 'Document') {
-                result += arg.loc.source.body;
+                var body = arg.loc.source.body;
+                // Dont add body's that were already added by other documents
+                if (result.indexOf(body) === -1) {
+                    result += body;
+                }
            }
            else {
                result += arg;
diff --git a/node_modules/graphql-tag/lib/index.js b/node_modules/graphql-tag/lib/index.js
index 81db598..8f78f09 100644
--- a/node_modules/graphql-tag/lib/index.js
+++ b/node_modules/graphql-tag/lib/index.js
@@ -83,7 +83,11 @@ export function gql(literals) {
    var result = literals[0];
    args.forEach(function (arg, i) {
        if (arg && arg.kind === 'Document') {
-            result += arg.loc.source.body;
+            var body = arg.loc.source.body;
+            // Dont add body's that were already added by other documents
+            if (result.indexOf(body) === -1) {
+                result += body;
+            }
        }
        else {
            result += arg;

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.

7 participants