graphql-paper
Version:
A flexible in-memory store based on a GraphQL Schema
61 lines (60 loc) • 3.22 kB
JavaScript
import { findDocument } from '../store/find-document.mjs';
import { extractObjectTypes } from '../graphql/extract-object-types.mjs';
import { isDocument } from '../document/is-document.mjs';
import { createDocument } from '../document/create-document.mjs';
import { extractListType } from '../graphql/extract-list-type.mjs';
function createOperation(context, typename, documentPartial) {
var _ref;
var {
store,
schema
} = context;
var document = createDocument(typename, documentPartial);
var gqlType = schema.getType(typename);
var gqlTypeFields = (_ref = (gqlType === null || gqlType === void 0 ? void 0 : gqlType.getFields) && (gqlType === null || gqlType === void 0 ? void 0 : gqlType.getFields())) !== null && _ref !== void 0 ? _ref : {};
// setup array of types if it doesn't already exist
store[typename] = store[typename] || [];
store[typename].push(document);
var _loop = function _loop(fieldName) {
var field = gqlTypeFields[fieldName];
if (!field) {
return 0; // continue
}
var documentFieldValue = document[fieldName];
var possibleObjectTypes = extractObjectTypes(schema, field.type);
var hasObjectReferences = possibleObjectTypes.length > 0;
var isSingularConnection = hasObjectReferences && !extractListType(field.type);
if (!hasObjectReferences) {
document[fieldName] = documentFieldValue;
return 0; // continue
}
if (documentFieldValue != null) {
var connectionCandidates = Array.isArray(documentFieldValue) ? documentFieldValue : [documentFieldValue];
var connectionDocuments = connectionCandidates.map(candidate => {
var isExistingDocument = isDocument(candidate) && findDocument(store, candidate) !== undefined;
if (isExistingDocument || candidate === null) {
return candidate;
}
if (candidate && possibleObjectTypes.length > 1) {
throw new Error("The \"".concat(typename, "\" with field \"").concat(fieldName, "\" is represented by multiple types: ").concat(possibleObjectTypes.join(', '), ". Therefore, cannot create document automatically for: \n\n ").concat(JSON.stringify(documentFieldValue, null, 2), ".\n\nUse the `create` operation within mutate to explicitly create this document with one of possible types (").concat(possibleObjectTypes.join(', '), ") and assign the connection explictly."));
}
var [fieldObjectType] = possibleObjectTypes;
var result = createOperation(context, fieldObjectType.name, candidate);
return result;
});
// Technically a singular connection field with multiple documents isn't
// allowed but this will be caught by the validations. The createOperation
// is only concerned with automatically creating documents that don't exist
document[fieldName] = isSingularConnection && connectionDocuments.length === 1 ? connectionDocuments[0] : connectionDocuments;
return 0; // continue
}
},
_ret;
for (var fieldName in document) {
_ret = _loop(fieldName);
if (_ret === 0) continue;
}
return document;
}
export { createOperation };
//# sourceMappingURL=create.mjs.map