UNPKG

@paroicms/server

Version:
133 lines 5.76 kB
import { siteReadyGuard } from "../../graphql/graphql.types.js"; import { authGuard } from "../auth/auth.helper.js"; import { permissionGuard } from "../auth/authorization.helper.js"; import { getTypeNameOf } from "../node/node.queries.js"; import { createSinglePartFieldNode } from "./create-part-field-node.js"; import { getFieldValuesOfLNode, getFieldValuesOfOneList } from "./load-fields-of.queries.js"; import { getFieldTypesByNames, loadFieldValues, } from "./load-fields.queries.js"; import { parseFieldValues, saveFieldValues } from "./save-fields.queries.js"; export const fieldResolvers = { Mutation: { createPartFieldNode: async (_parent, { nodeId, fieldName, language }, { siteContext, httpContext }) => { siteReadyGuard(siteContext); await permissionGuard(siteContext, httpContext, "document.edit"); return await createSinglePartFieldNode(siteContext, { nodeId, fieldName, language }); }, setFieldValues: async (_parent, { nodeId, language, values }, { siteContext, httpContext }) => { siteReadyGuard(siteContext); await permissionGuard(siteContext, httpContext, "document.edit"); const typeName = await getTypeNameOf(siteContext, nodeId); await saveFieldValues(siteContext, { typeName, nodeId, language: language ?? undefined, values: parseFieldValues(values), }); return true; }, setSiteFieldValues: async (_parent, { language, values }, { siteContext, httpContext }) => { siteReadyGuard(siteContext); await permissionGuard(siteContext, httpContext, "site.editProperties"); await saveFieldValues(siteContext, { typeName: "_site", nodeId: siteContext.homeRoutingCluster.siteNodeId, language: language ?? undefined, values: parseFieldValues(values), }); return true; }, }, Query: { fieldValues: async (_parent, { lNodeId, fieldNames, preprocess }, { siteContext, httpContext }) => { authGuard(httpContext); siteReadyGuard(siteContext); const fieldValues = await getFieldValuesOfLNode(siteContext, { lNodeId, fieldNames: fieldNames ?? undefined, preprocess: formatPreprocessedFields(preprocess), publishedOnly: false, }); return formatProcessedFieldValues(fieldValues); }, fieldValuesOfList: async (_parent, { parentLNodeId, listName, fieldNames, preprocess }, { siteContext, httpContext }) => { authGuard(httpContext); siteReadyGuard(siteContext); const map = await getFieldValuesOfOneList(siteContext, { parentLNodeId, listName: listName ?? undefined, fieldNames: fieldNames ?? undefined, preprocess: formatPreprocessedFields(preprocess), publishedOnly: false, }); return Array.from(map.entries()).map(([lNodeId, fieldValues]) => ({ lNodeId, fieldValues: formatProcessedFieldValues(fieldValues), })); }, siteFieldValues: async (_parent, { language, fieldNames, preprocess }, { siteContext, httpContext }) => { authGuard(httpContext); siteReadyGuard(siteContext); return await resolveFieldValues(siteContext, { typeName: "_site", nodeId: siteContext.homeRoutingCluster.siteNodeId, fieldNames: fieldNames ?? undefined, language: language ?? undefined, preprocess: formatPreprocessedFields(preprocess), }); }, }, }; export const extendDocumentWithFieldValuesResolvers = { Document: { fieldValues: async ({ nodeId, language }, { preprocess, fieldNames }, { siteContext }) => { siteReadyGuard(siteContext); const typeName = await getTypeNameOf(siteContext, nodeId); return await resolveFieldValues(siteContext, { typeName, nodeId, language, fieldNames: fieldNames ?? undefined, preprocess: formatPreprocessedFields(preprocess), }); }, }, }; export const extendPartWithFieldValuesResolver = { Part: { fieldValues: async ({ nodeId, language }, { preprocess, fieldNames }, { siteContext }) => { siteReadyGuard(siteContext); const typeName = await getTypeNameOf(siteContext, nodeId); return await resolveFieldValues(siteContext, { typeName, nodeId, language, fieldNames: fieldNames ?? undefined, preprocess: formatPreprocessedFields(preprocess), }); }, }, }; async function resolveFieldValues(siteContext, options) { const { typeName, nodeId, language, fieldNames, preprocess } = options; const fieldTypes = getFieldTypesByNames(siteContext, { typeName, fieldNames, }); const values = await loadFieldValues(siteContext, { nodeId, language, fieldTypes, publishedOnly: false, preprocess, }); return formatProcessedFieldValues(values); } function formatProcessedFieldValues(values) { return JSON.stringify(values); } function formatPreprocessedFields(preprocess) { return preprocess ? { replaceWithExcerpt: preprocess.replaceWithExcerpt ?? undefined } : undefined; } //# sourceMappingURL=fields.resolver.js.map