graphql-tag
graphql-tag copied to clipboard
Fragments bodies where added multiple times (duplicates)
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: 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/
@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 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.
Is this getting merged any time soon?
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.
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, 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
graphql-tag
is being released but this is not getting merged 😕
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;