graphql-paper
Version:
A flexible in-memory store based on a GraphQL Schema
28 lines (27 loc) • 1.14 kB
JavaScript
import { unwrap } from '../../graphql/unwrap.mjs';
var uniqueIdFieldValidator = {
skipConnectionValue: true,
skipNullValue: true,
validate(_ref) {
var _store$type$name;
var {
type,
fieldName,
fieldValue,
store,
field
} = _ref;
var returnType = unwrap(field.type);
var 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') {
var matchingIdDocuments = typeDocuments.filter(doc => doc[fieldName] === fieldValue);
var hasDuplicates = matchingIdDocuments.length > 1;
if (hasDuplicates) {
var documentsToCheckNotice = matchingIdDocuments.map(document => JSON.stringify(document, null, 2)).join('\n\n');
throw new Error("Found duplciate documents with same ID (\"".concat(fieldValue, "\") for type \"").concat(type.name, "\" on field \"").concat(fieldName, "\":\n\n").concat(documentsToCheckNotice, "\""));
}
}
}
};
export { uniqueIdFieldValidator };
//# sourceMappingURL=unique-id.mjs.map