graphql-tag icon indicating copy to clipboard operation
graphql-tag copied to clipboard

Fragments bodies where added multiple times (duplicates)

Open lanwin opened this issue 5 years ago • 9 comments

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.

lanwin avatar Dec 04 '19 08:12 lanwin

@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/

apollo-cla avatar Dec 04 '19 08:12 apollo-cla

@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

hcharley avatar Dec 14 '19 23:12 hcharley

@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.

lanwin avatar Dec 15 '19 11:12 lanwin

Is this getting merged any time soon?

ElliottRoche avatar Jan 18 '20 19:01 ElliottRoche

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 avatar Jan 19 '20 18:01 lanwin

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

lanwin avatar Mar 23 '20 09:03 lanwin

@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

dimikot avatar Jun 26 '20 04:06 dimikot

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

zaguiini avatar Sep 02 '20 11:09 zaguiini

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;

sparebytes avatar May 17 '21 17:05 sparebytes