diff --git a/src/index.js b/src/index.js index f52711e4..60970ea2 100644 --- a/src/index.js +++ b/src/index.js @@ -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]; } diff --git a/test/graphql.js b/test/graphql.js index 726e3918..86225ae0 100644 --- a/test/graphql.js +++ b/test/graphql.js @@ -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;