houdini-svelte
Version:
The svelte plugin for houdini
270 lines (269 loc) • 9.37 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var fragment_exports = {};
__export(fragment_exports, {
FragmentStore: () => FragmentStore,
FragmentStoreCursor: () => FragmentStoreCursor,
FragmentStoreOffset: () => FragmentStoreOffset
});
module.exports = __toCommonJS(fragment_exports);
var import_cache = __toESM(require("$houdini/runtime/cache"), 1);
var import_config = require("$houdini/runtime/lib/config");
var import_constants = require("$houdini/runtime/lib/constants");
var import_pageInfo = require("$houdini/runtime/lib/pageInfo");
var import_pagination = require("$houdini/runtime/lib/pagination");
var import_scalars = require("$houdini/runtime/lib/scalars");
var import_types = require("$houdini/runtime/lib/types");
var import_store = require("svelte/store");
var import_adapter = require("../adapter");
var import_client = require("../client");
var import_session = require("../session");
var import_base = require("./base");
class FragmentStore {
artifact;
name;
kind = import_types.CompiledFragmentKind;
context = null;
constructor({ artifact, storeName }) {
this.artifact = artifact;
this.name = storeName;
}
get(initialValue) {
const { variables, parent } = initialValue?.[import_types.fragmentKey]?.values?.[this.artifact.name] ?? {};
const { loading } = initialValue?.[import_types.fragmentKey] ?? {};
if (!loading && initialValue && import_types.fragmentKey in initialValue && (!variables || !parent) && import_adapter.isBrowser) {
console.warn(
`\u26A0\uFE0F Parent does not contain the information for this fragment. Something is wrong.
Please ensure that you have passed a record that has ${this.artifact.name} mixed into it.`
);
}
let data = initialValue;
if (loading || initialValue && parent && import_adapter.isBrowser) {
data = import_cache.default.read({
selection: this.artifact.selection,
parent,
variables,
loading
}).data;
}
const store = new import_base.BaseStore({
artifact: this.artifact,
initialValue: data
});
if (!loading && parent) {
store.observer.send({ variables, setup: true, stuff: { parentID: parent } });
}
return {
initialValue: data,
variables: (0, import_scalars.marshalInputs)({
artifact: this.artifact,
input: variables,
config: (0, import_config.getCurrentConfig)(),
rootType: this.artifact.rootType
}),
kind: import_types.CompiledFragmentKind,
subscribe: (0, import_store.derived)([store], ([$store]) => $store.data).subscribe
};
}
}
class BasePaginatedFragmentStore {
paginated = true;
paginationArtifact;
name;
kind = import_types.CompiledFragmentKind;
artifact;
constructor(config) {
this.paginationArtifact = config.paginationArtifact;
this.name = config.storeName;
this.artifact = config.artifact;
}
queryVariables(getState) {
const config = (0, import_config.getCurrentConfig)();
const { targetType } = this.paginationArtifact.refetch || {};
const typeConfig = config.types?.[targetType || ""];
if (!typeConfig) {
throw new Error(
`Missing type refetch configuration for ${targetType}. For more information, see ${import_constants.siteURL}/guides/pagination#paginated-fragments`
);
}
let idVariables = {};
const value = getState();
if (typeConfig.resolve?.arguments) {
idVariables = typeConfig.resolve.arguments?.(value) || {};
} else {
const keys = (0, import_config.keyFieldsForType)(config, targetType || "");
idVariables = Object.fromEntries(keys.map((key) => [key, value[key]]));
}
return {
...idVariables
};
}
}
class FragmentStoreCursor extends BasePaginatedFragmentStore {
get(initialValue) {
const base = new FragmentStore({
artifact: this.artifact,
storeName: this.name
});
const store = base.get(initialValue);
const paginationStore = (0, import_client.getClient)().observe({
artifact: this.paginationArtifact,
initialValue: store.initialValue
});
const handlers = this.storeHandlers(
paginationStore,
initialValue,
() => (0, import_store.get)(store),
() => store.variables
);
const subscribe = (run, invalidate) => {
const combined = (0, import_store.derived)([store, paginationStore], ([$parent, $pagination]) => {
return {
...$pagination,
data: $parent,
pageInfo: (0, import_pageInfo.extractPageInfo)($parent, this.paginationArtifact.refetch.path)
};
});
return combined.subscribe(run, invalidate);
};
return {
kind: import_types.CompiledFragmentKind,
subscribe,
fetch: handlers.fetch,
loadNextPage: handlers.loadNextPage,
loadPreviousPage: handlers.loadPreviousPage
};
}
storeHandlers(observer, initialValue, getState, getVariables) {
return (0, import_pagination.cursorHandlers)({
getState,
getVariables,
artifact: this.paginationArtifact,
fetchUpdate: async (args, updates) => {
await (0, import_client.initClient)();
return observer.send({
session: await (0, import_session.getSession)(),
...args,
variables: {
...args?.variables,
...this.queryVariables(getState)
},
cacheParams: {
applyUpdates: updates,
disableSubscriptions: true
}
});
},
fetch: async (args) => {
await (0, import_client.initClient)();
return await observer.send({
session: await (0, import_session.getSession)(),
...args,
variables: {
...args?.variables,
...this.queryVariables(getState)
},
cacheParams: {
disableSubscriptions: true
}
});
},
getSession: import_session.getSession
});
}
}
class FragmentStoreOffset extends BasePaginatedFragmentStore {
get(initialValue) {
const base = new FragmentStore({
artifact: this.artifact,
storeName: this.name
});
const store = base.get(initialValue);
const paginationStore = (0, import_client.getClient)().observe({
artifact: this.paginationArtifact,
initialValue: store.initialValue
});
const getState = () => (0, import_store.get)(store);
const handlers = (0, import_pagination.offsetHandlers)({
getState,
getVariables: () => store.variables,
artifact: this.paginationArtifact,
fetch: async (args) => {
await (0, import_client.initClient)();
return paginationStore.send({
...args,
session: await (0, import_session.getSession)(),
variables: {
...this.queryVariables(getState),
...args?.variables
},
cacheParams: {
disableSubscriptions: true
}
});
},
fetchUpdate: async (args) => {
await (0, import_client.initClient)();
return paginationStore.send({
session: await (0, import_session.getSession)(),
...args,
variables: {
...this.queryVariables(getState),
...args?.variables
},
cacheParams: {
disableSubscriptions: true,
applyUpdates: ["append"]
}
});
},
getSession: import_session.getSession,
storeName: this.name
});
const subscribe = (run, invalidate) => {
const combined = (0, import_store.derived)([store, paginationStore], ([$parent, $pagination]) => {
return {
...$pagination,
data: $parent
};
});
return combined.subscribe(run, invalidate);
};
return {
kind: import_types.CompiledFragmentKind,
data: (0, import_store.derived)(paginationStore, ($value) => $value.data),
subscribe,
fetch: handlers.fetch,
loadNextPage: handlers.loadNextPage,
fetching: (0, import_store.derived)(paginationStore, ($store) => $store.fetching)
};
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FragmentStore,
FragmentStoreCursor,
FragmentStoreOffset
});