"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryBuilder = void 0; exports.buildSelections = buildSelections; exports.buildFindManyDocument = buildFindManyDocument; exports.buildFindFirstDocument = buildFindFirstDocument; exports.buildCreateDocument = buildCreateDocument; exports.buildUpdateDocument = buildUpdateDocument; exports.buildUpdateByPkDocument = buildUpdateByPkDocument; exports.buildFindOneDocument = buildFindOneDocument; exports.buildDeleteDocument = buildDeleteDocument; exports.buildDeleteByPkDocument = buildDeleteByPkDocument; exports.buildJunctionRemoveDocument = buildJunctionRemoveDocument; exports.buildCustomDocument = buildCustomDocument; exports.buildBulkInsertDocument = buildBulkInsertDocument; exports.buildBulkUpsertDocument = buildBulkUpsertDocument; exports.buildBulkUpdateDocument = buildBulkUpdateDocument; exports.buildBulkDeleteDocument = buildBulkDeleteDocument; /** * Query Builder - Builds and executes GraphQL operations * @generated by @constructive-io/graphql-codegen * DO NOT EDIT - changes will be overwritten */ const runtime_1 = require("@constructive-io/graphql-query/runtime"); const t = __importStar(require("gql-ast")); const client_1 = require("./client"); class QueryBuilder { constructor(config) { this.config = config; } /** * Execute the query and return a discriminated union result * Use result.ok to check success, or .unwrap() to throw on error */ async execute() { const rawResult = await this.config.client.execute(this.config.document, this.config.variables); if (!rawResult.ok) { return rawResult; } if (!this.config.transform) { return rawResult; } return { ok: true, data: this.config.transform(rawResult.data), errors: undefined, }; } /** * Execute and unwrap the result, throwing GraphQLRequestError on failure * @throws {GraphQLRequestError} If the query returns errors */ async unwrap() { const result = await this.execute(); if (!result.ok) { throw new client_1.GraphQLRequestError(result.errors, result.data); } return result.data; } /** * Execute and unwrap, returning defaultValue on error instead of throwing */ async unwrapOr(defaultValue) { const result = await this.execute(); if (!result.ok) { return defaultValue; } return result.data; } /** * Execute and unwrap, calling onError callback on failure */ async unwrapOrElse(onError) { const result = await this.execute(); if (!result.ok) { return onError(result.errors); } return result.data; } toGraphQL() { return this.config.document; } getVariables() { return this.config.variables; } } exports.QueryBuilder = QueryBuilder; const OP_QUERY = 'query'; const OP_MUTATION = 'mutation'; const ENUM_VALUE_KIND = 'EnumValue'; // ============================================================================ // Selection Builders // ============================================================================ function buildSelections(select, connectionFieldsMap, entityType) { if (!select) { return []; } const fields = []; const entityConnections = entityType ? connectionFieldsMap?.[entityType] : undefined; for (const [key, value] of Object.entries(select)) { if (value === false || value === undefined) { continue; } if (value === true) { fields.push(t.field({ name: key })); continue; } if (typeof value === 'object' && value !== null) { const nested = value; // Field with arguments (e.g. requestUploadUrl on bucket types) if (nested.args && typeof nested.args === 'object') { const fieldArgs = Object.entries(nested.args).map(([argName, argValue]) => t.argument({ name: argName, value: buildValueAst(argValue) })); const nestedSelect = nested.select; if (nestedSelect && typeof nestedSelect === 'object') { const subSelections = Object.entries(nestedSelect) .filter(([, v]) => v) .map(([name]) => t.field({ name })); fields.push(t.field({ name: key, args: fieldArgs.length ? fieldArgs : undefined, selectionSet: subSelections.length ? t.selectionSet({ selections: subSelections }) : undefined, })); } else { fields.push(t.field({ name: key, args: fieldArgs.length ? fieldArgs : undefined, })); } continue; } if (!nested.select || typeof nested.select !== 'object') { throw new Error(`Invalid selection for field "${key}": nested selections must include a "select" object.`); } const relatedEntityType = entityConnections?.[key]; const nestedSelections = buildSelections(nested.select, connectionFieldsMap, relatedEntityType); const isConnection = nested.connection === true || nested.first !== undefined || nested.filter !== undefined || relatedEntityType !== undefined; const args = buildArgs([ buildOptionalArg('first', nested.first), nested.filter ? t.argument({ name: 'filter', value: buildValueAst(nested.filter), }) : null, buildEnumListArg('orderBy', nested.orderBy), ]); if (isConnection) { fields.push(t.field({ name: key, args, selectionSet: t.selectionSet({ selections: buildConnectionSelections(nestedSelections), }), })); } else { fields.push(t.field({ name: key, args, selectionSet: t.selectionSet({ selections: nestedSelections }), })); } } } return fields; } // ============================================================================ // Document Builders // ============================================================================ function buildFindManyDocument(operationName, queryField, select, args, filterTypeName, orderByTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; const variableDefinitions = []; const queryArgs = []; const variables = {}; addVariable({ varName: 'where', typeName: filterTypeName, value: args.where, }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'orderBy', typeName: '[' + orderByTypeName + '!]', value: args.orderBy?.length ? args.orderBy : undefined, }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'first', typeName: 'Int', value: args.first }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'last', typeName: 'Int', value: args.last }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'after', typeName: 'Cursor', value: args.after }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'before', typeName: 'Cursor', value: args.before }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'offset', typeName: 'Int', value: args.offset }, variableDefinitions, queryArgs, variables); const document = t.document({ definitions: [ t.operationDefinition({ operation: OP_QUERY, name: operationName + 'Query', variableDefinitions: variableDefinitions.length ? variableDefinitions : undefined, selectionSet: t.selectionSet({ selections: [ t.field({ name: queryField, args: queryArgs.length ? queryArgs : undefined, selectionSet: t.selectionSet({ selections: buildConnectionSelections(selections), }), }), ], }), }), ], }); return { document: (0, runtime_1.print)(document), variables }; } function buildFindFirstDocument(operationName, queryField, select, args, filterTypeName, orderByTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; const variableDefinitions = []; const queryArgs = []; const variables = {}; // Always add first: 1 for findFirst addVariable({ varName: 'first', typeName: 'Int', value: 1 }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'where', typeName: filterTypeName, value: args.where, }, variableDefinitions, queryArgs, variables); addVariable({ varName: 'orderBy', typeName: '[' + orderByTypeName + '!]', value: args.orderBy?.length ? args.orderBy : undefined, }, variableDefinitions, queryArgs, variables); const document = t.document({ definitions: [ t.operationDefinition({ operation: OP_QUERY, name: operationName + 'Query', variableDefinitions, selectionSet: t.selectionSet({ selections: [ t.field({ name: queryField, args: queryArgs, selectionSet: t.selectionSet({ selections: [ t.field({ name: 'nodes', selectionSet: t.selectionSet({ selections }), }), ], }), }), ], }), }), ], }); return { document: (0, runtime_1.print)(document), variables }; } function buildCreateDocument(operationName, mutationField, entityField, select, data, inputTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: entityField, selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { [entityField]: data, }, }, }; } function buildUpdateDocument(operationName, mutationField, entityField, select, where, data, inputTypeName, patchFieldName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: entityField, selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { id: where.id, [patchFieldName]: data, }, }, }; } function buildUpdateByPkDocument(operationName, mutationField, entityField, select, id, data, inputTypeName, idFieldName, patchFieldName, connectionFieldsMap, extraKeys) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: entityField, selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { [idFieldName]: id, ...extraKeys, [patchFieldName]: data, }, }, }; } function buildFindOneDocument(operationName, queryField, id, select, idArgName, idTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; const variableDefinitions = [ t.variableDefinition({ variable: t.variable({ name: idArgName }), type: (0, runtime_1.parseType)(idTypeName), }), ]; const queryArgs = [ t.argument({ name: idArgName, value: t.variable({ name: idArgName }), }), ]; const document = t.document({ definitions: [ t.operationDefinition({ operation: OP_QUERY, name: operationName + 'Query', variableDefinitions, selectionSet: t.selectionSet({ selections: [ t.field({ name: queryField, args: queryArgs, selectionSet: t.selectionSet({ selections }), }), ], }), }), ], }); return { document: (0, runtime_1.print)(document), variables: { [idArgName]: id }, }; } function buildDeleteDocument(operationName, mutationField, entityField, where, inputTypeName, select, connectionFieldsMap) { const entitySelections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: entityField, selectionSet: t.selectionSet({ selections: entitySelections, }), }), ], }), variables: { input: { id: where.id, }, }, }; } function buildDeleteByPkDocument(operationName, mutationField, entityField, keys, inputTypeName, select, connectionFieldsMap) { const entitySelections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: entityField, selectionSet: t.selectionSet({ selections: entitySelections }), }), ], }), variables: { input: keys, }, }; } function buildJunctionRemoveDocument(operationName, mutationField, keys, inputTypeName) { return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [t.field({ name: 'clientMutationId' })], }), variables: { input: keys, }, }; } function buildCustomDocument(operationType, operationName, fieldName, select, args, variableDefinitions, connectionFieldsMap, entityType) { let actualSelect = select; let isConnection = false; if (isCustomSelectionWrapper(select)) { actualSelect = select.select; isConnection = select.connection === true; } const selections = actualSelect ? buildSelections(actualSelect, connectionFieldsMap, entityType) : []; const variableDefs = variableDefinitions.map((definition) => t.variableDefinition({ variable: t.variable({ name: definition.name }), type: (0, runtime_1.parseType)(definition.type), })); const fieldArgs = variableDefinitions.map((definition) => t.argument({ name: definition.name, value: t.variable({ name: definition.name }), })); const fieldSelections = isConnection ? buildConnectionSelections(selections) : selections; const document = t.document({ definitions: [ t.operationDefinition({ operation: operationType === 'mutation' ? OP_MUTATION : OP_QUERY, name: operationName, variableDefinitions: variableDefs.length ? variableDefs : undefined, selectionSet: t.selectionSet({ selections: [ t.field({ name: fieldName, args: fieldArgs.length ? fieldArgs : undefined, selectionSet: fieldSelections.length ? t.selectionSet({ selections: fieldSelections }) : undefined, }), ], }), }), ], }); return { document: (0, runtime_1.print)(document), variables: (args ?? {}), }; } function isCustomSelectionWrapper(value) { if (!value || typeof value !== 'object' || Array.isArray(value)) { return false; } const record = value; const keys = Object.keys(record); if (!keys.includes('select') || !keys.includes('connection')) { return false; } if (keys.some((key) => key !== 'select' && key !== 'connection')) { return false; } return (!!record.select && typeof record.select === 'object' && !Array.isArray(record.select)); } // ============================================================================ // Helper Functions // ============================================================================ function buildArgs(args) { return args.filter((arg) => arg !== null); } function buildOptionalArg(name, value) { if (value === undefined) { return null; } const valueNode = typeof value === 'number' ? t.intValue({ value: value.toString() }) : t.stringValue({ value }); return t.argument({ name, value: valueNode }); } function buildEnumListArg(name, values) { if (!values || values.length === 0) { return null; } return t.argument({ name, value: t.listValue({ values: values.map((value) => buildEnumValue(value)), }), }); } function buildEnumValue(value) { return { kind: ENUM_VALUE_KIND, value, }; } function buildPageInfoSelections() { return [ t.field({ name: 'hasNextPage' }), t.field({ name: 'hasPreviousPage' }), t.field({ name: 'startCursor' }), t.field({ name: 'endCursor' }), ]; } function buildConnectionSelections(nodeSelections) { return [ t.field({ name: 'nodes', selectionSet: t.selectionSet({ selections: nodeSelections }), }), t.field({ name: 'totalCount' }), t.field({ name: 'pageInfo', selectionSet: t.selectionSet({ selections: buildPageInfoSelections() }), }), ]; } function buildInputMutationDocument(config) { const document = t.document({ definitions: [ t.operationDefinition({ operation: OP_MUTATION, name: config.operationName + 'Mutation', variableDefinitions: [ t.variableDefinition({ variable: t.variable({ name: 'input' }), type: (0, runtime_1.parseType)(config.inputTypeName + '!'), }), ], selectionSet: t.selectionSet({ selections: [ t.field({ name: config.mutationField, args: [ t.argument({ name: 'input', value: t.variable({ name: 'input' }), }), ], selectionSet: t.selectionSet({ selections: config.resultSelections, }), }), ], }), }), ], }); return (0, runtime_1.print)(document); } function addVariable(spec, definitions, args, variables) { if (spec.value === undefined || !spec.typeName) return; definitions.push(t.variableDefinition({ variable: t.variable({ name: spec.varName }), type: (0, runtime_1.parseType)(spec.typeName), })); args.push(t.argument({ name: spec.argName ?? spec.varName, value: t.variable({ name: spec.varName }), })); variables[spec.varName] = spec.value; } function buildValueAst(value) { if (value === null) { return t.nullValue(); } if (typeof value === 'boolean') { return t.booleanValue({ value }); } if (typeof value === 'number') { return Number.isInteger(value) ? t.intValue({ value: value.toString() }) : t.floatValue({ value: value.toString() }); } if (typeof value === 'string') { return t.stringValue({ value }); } if (Array.isArray(value)) { return t.listValue({ values: value.map((item) => buildValueAst(item)), }); } if (typeof value === 'object' && value !== null) { const obj = value; return t.objectValue({ fields: Object.entries(obj).map(([key, val]) => t.objectField({ name: key, value: buildValueAst(val), })), }); } throw new Error('Unsupported value type: ' + typeof value); } // ============================================================================ // Bulk Mutation Document Builders // ============================================================================ function buildBulkInsertDocument(operationName, mutationField, select, data, inputTypeName, onConflict, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: 'affectedCount' }), t.field({ name: 'returning', selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { values: data, ...(onConflict ? { onConflict } : {}), }, }, }; } function buildBulkUpsertDocument(operationName, mutationField, select, data, inputTypeName, onConflict, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: 'affectedCount' }), t.field({ name: 'returning', selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { values: data, onConflict, }, }, }; } function buildBulkUpdateDocument(operationName, mutationField, select, where, data, inputTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: 'affectedCount' }), t.field({ name: 'returning', selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { where, patch: data, }, }, }; } function buildBulkDeleteDocument(operationName, mutationField, select, where, inputTypeName, connectionFieldsMap) { const selections = select ? buildSelections(select, connectionFieldsMap, operationName) : [t.field({ name: 'id' })]; return { document: buildInputMutationDocument({ operationName, mutationField, inputTypeName, resultSelections: [ t.field({ name: 'affectedCount' }), t.field({ name: 'returning', selectionSet: t.selectionSet({ selections }), }), ], }), variables: { input: { where, }, }, }; }