Skip to content

Commit

Permalink
Checking document for other fragment definitions
Browse files Browse the repository at this point in the history
Its possible that fragments have already been defined in the document itself - we want to check against those to make sure we’re only attaching fragments that either
1) don’t already exist in the document
2) are distinct in the fragments to be attached to the document
  • Loading branch information
jnwng committed Nov 15, 2016
1 parent 3e8825a commit 0187354
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/queries/getFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import assign = require('lodash.assign');
import countBy = require('lodash.countby');
import identity = require('lodash.identity');
import includes = require('lodash.includes');
import uniq = require('lodash.uniq');

export function getMutationDefinition(doc: Document): OperationDefinition {
Expand Down Expand Up @@ -154,7 +155,14 @@ export function addFragmentsToDocument(queryDoc: Document,
return queryDoc;
}
checkDocument(queryDoc);

const existingFragmentNames = getFragmentDefinitions(queryDoc)
.map((fragment) => fragment.name.value) as String[];
const distinctFragments = uniq(fragments.filter((fragment) => {
return !includes(existingFragmentNames, fragment.name.value);
})) as FragmentDefinition[];

return assign({}, queryDoc, {
definitions: queryDoc.definitions.concat(uniq(fragments)),
definitions: queryDoc.definitions.concat(distinctFragments),
}) as Document;
}
5 changes: 5 additions & 0 deletions test/getFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ fragment businessAreaInfo on BusinessArea {
...businessAreaInfo
}
}
fragment subjectInfo on Subject {
id
name
}
`;

const fullDoc = addFragmentsToDocument(query, businessAreaInfo);
Expand Down

0 comments on commit 0187354

Please sign in to comment.