Only merge definitions with directives when they are the same

This commit is contained in:
Chi Vinh Le
2017-12-01 16:44:01 +01:00
parent 72123cabe7
commit 4c7c852af7
@@ -6,14 +6,27 @@ import {
getOperationDefinition,
} from 'apollo-utilities';
import {murmur3} from 'murmurhash-js';
function getDirectivesID(directives) {
return murmur3(JSON.stringify(directives));
}
// If two definitions have the same id, they can be merged.
function getDefinitionID(definition) {
// Only merge when directives are exactly the same.
const trailing = definition.directives.length
? `_${getDirectivesID(definition.directives)}`
: '';
switch (definition.kind) {
case 'FragmentSpread':
return `FragmentSpread_${definition.name.value}`;
case 'Field':
return `Field_${definition.alias ? definition.alias.value : definition.name.value}`;
return `Field_${definition.alias ? definition.alias.value : definition.name.value}${trailing}`;
case 'InlineFragment':
return `InlineFragment_${definition.typeCondition.name.value}`;
return `InlineFragment_${definition.typeCondition.name.value}${trailing}`;
default:
throw new Error(`unknown definition kind ${definition.kind}`);
}