graphql-paper
Version:
A flexible in-memory store based on a GraphQL Schema
29 lines (27 loc) • 1.11 kB
JavaScript
;
var unwrap = require('../../graphql/unwrap.js');
const uniqueIdFieldValidator = {
skipConnectionValue: true,
skipNullValue: true,
validate({
type,
fieldName,
fieldValue,
store,
field
}) {
var _store$type$name;
const returnType = unwrap.unwrap(field.type);
const typeDocuments = (_store$type$name = store[type.name]) !== null && _store$type$name !== void 0 ? _store$type$name : [];
if ((returnType === null || returnType === void 0 ? void 0 : returnType.name) === 'ID') {
const matchingIdDocuments = typeDocuments.filter(doc => doc[fieldName] === fieldValue);
const hasDuplicates = matchingIdDocuments.length > 1;
if (hasDuplicates) {
const documentsToCheckNotice = matchingIdDocuments.map(document => JSON.stringify(document, null, 2)).join('\n\n');
throw new Error(`Found duplciate documents with same ID ("${fieldValue}") for type "${type.name}" on field "${fieldName}":\n\n${documentsToCheckNotice}"`);
}
}
}
};
exports.uniqueIdFieldValidator = uniqueIdFieldValidator;
//# sourceMappingURL=unique-id.js.map