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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ function gql(/* arguments */) {

for (var i = 1; i < args.length; i++) {
if (args[i] && args[i].kind && args[i].kind === 'Document') {
result += args[i].loc.source.body;
var body = args[i].loc.source.body;
// Dont add body's that where already added by other documents
if (result.indexOf(body) === -1) result += body;
} else {
result += args[i];
}
Expand Down
53 changes: 53 additions & 0 deletions test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,59 @@ const assert = require('chai').assert;
`);
});

it('can include fragments sources only once if they are nested and used multiple times', () => {
const fragmentB = gql`
fragment FragmentB on Other {
number
}
`;

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

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

assert.deepEqual(ast, gql`
query Query {
a {
...FragmentA
}
b {
...FragmentB
}
}

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

fragment FragmentB on Other {
number
}
`);
});

describe('fragment warnings', () => {
let warnings = [];
const oldConsoleWarn = console.warn;
Expand Down