graphql-paper
Version:
A flexible in-memory store based on a GraphQL Schema
121 lines (118 loc) • 6.56 kB
JavaScript
;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var immer = require('immer');
var dispatch = require('./events/dispatch.js');
var index = require('./operations/index.js');
var transaction = require('./transaction/transaction.js');
var createDocumentStore = require('./store/create-document-store.js');
var findDocument = require('./store/find-document.js');
var proxyWrap = require('./store/proxy-wrap.js');
var documentPropertyExistsAsFieldOnType = require('./validations/validators/document-property-exists-as-field-on-type.js');
var captureTransactionResultKeys = require('./transaction/capture-transaction-result-keys.js');
var convertResultKeysToDocument = require('./transaction/convert-result-keys-to-document.js');
var createSchema = require('./graphql/create-schema.js');
var listField = require('./validations/validators/list-field.js');
require('graphql');
var nullDocument = require('./document/null-document.js');
var objectField = require('./validations/validators/object-field.js');
var scalarField = require('./validations/validators/scalar-field.js');
var uniqueId = require('./validations/validators/unique-id.js');
var serializeStore = require('./serialization-deserialization/serialize-store.js');
var getDocumentKey = require('./document/get-document-key.js');
require('./document/generate-document-key.js');
var deserializeStore = require('./serialization-deserialization/deserialize-store.js');
var validateStore = require('./validations/validate-store.js');
var isSerializedPaper = require('./serialization-deserialization/is-serialized-paper.js');
// Auto Freezing needs to be disabled because it interfers with using
// of using js a `Proxy` on the resulting data, see:
// > 18.5.5.7 Example: non-writable non-configurable target properties
// > must be represented faithfully
// > https://exploringjs.com/deep-js/ch_proxies.html
immer.setAutoFreeze(false);
// Paper `Document` objects uses getters and non-enumerables, in order
// to preserve this in immer >= 10 `setUseStrictShallowCopy(true)` is
// required
immer.setUseStrictShallowCopy(true);
class Paper {
static deserialize(graphqlSchema, serializedPaper, options) {
isSerializedPaper.assertValidSerializedPaper(serializedPaper);
const paper = new Paper(graphqlSchema, options);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
paper.current = deserializeStore.deserialize(serializedPaper.store, serializedPaper.__meta__);
paper.validate();
return paper;
}
constructor(graphqlSchema, options) {
_defineProperty(this, "history", []);
_defineProperty(this, "current", void 0);
_defineProperty(this, "sourceGraphQLSchema", void 0);
_defineProperty(this, "operations", void 0);
_defineProperty(this, "events", new EventTarget());
_defineProperty(this, "validators", {
document: [documentPropertyExistsAsFieldOnType.documentPropertyExistsAsFieldOnTypeValidator],
field: [listField.listFieldValidator, objectField.objectFieldValidator, scalarField.scalarFieldValidator, uniqueId.uniqueIdFieldValidator]
});
_defineProperty(this, "hooks", {
beforeTransaction: [],
afterTransaction: []
});
const schema = createSchema.createSchema(graphqlSchema);
this.current = createDocumentStore.createDocumentStore(schema);
this.sourceGraphQLSchema = schema;
this.operations = _objectSpread(_objectSpread({}, options === null || options === void 0 ? void 0 : options.operations), index);
// validate that anything that was deserialized is actually valid
this.validate();
}
get data() {
return proxyWrap.proxyWrap(this.sourceGraphQLSchema, this.current);
}
find(documentOrKey) {
return findDocument.findDocument(this.data, documentOrKey);
}
clear() {
this.current = createDocumentStore.createDocumentStore(this.sourceGraphQLSchema);
this.history = [];
}
serialize() {
return {
store: serializeStore.serialize(this.current),
__meta__: {
NULL_DOCUMENT_KEY: getDocumentKey.getDocumentKey(nullDocument.nullDocument)
}
};
}
validate(_store) {
const store = _store !== null && _store !== void 0 ? _store : this.current;
validateStore.validateStore(this.sourceGraphQLSchema, store, this.validators);
}
dispatchEvents(events) {
const eventsTarget = this.events;
events.forEach(event => eventsTarget.dispatchEvent(event));
}
mutate(fn) {
const schema = this.sourceGraphQLSchema;
const current = this.current;
const hooks = this.hooks;
const operations = this.operations;
const draft = immer.createDraft(current);
const {
transactionResult,
eventQueue: customEvents
} = transaction.transaction(draft, schema, operations, hooks, fn);
const resultKeys = captureTransactionResultKeys.captureTransactionResultKeys(transactionResult);
const next = immer.finishDraft(draft);
this.validate(next);
const storeEvents = dispatch.createStoreEvents(current, next);
this.dispatchEvents([...storeEvents, ...customEvents]);
this.current = next;
this.history.push(next);
const mutateResult = convertResultKeysToDocument.convertResultKeysToDocument(schema, next, resultKeys);
return mutateResult;
}
}
exports.Paper = Paper;
//# sourceMappingURL=paper.js.map