UNPKG

@grapi/server

Version:

Grapi Schema Generator For GraphQL Server

80 lines (79 loc) 3.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dataModel_1 = require("../dataModel"); const lodash_1 = require("../lodash"); const baseType_1 = __importDefault(require("./baseType")); const orderInput_1 = __importDefault(require("./orderInput")); const whereInput_1 = __importDefault(require("./whereInput")); const parsePaginationFromArgs = (args) => { if (!args) { return null; } return (0, lodash_1.pick)(args, ['first', 'last', 'skip', 'take', 'before', 'after']); }; class QueryPlugin { whereInputPlugin; orderInputPlugin; baseTypePlugin; setPlugins(plugins) { this.whereInputPlugin = plugins.find(plugin => plugin instanceof whereInput_1.default); this.orderInputPlugin = plugins.find(plugin => plugin instanceof orderInput_1.default); this.baseTypePlugin = plugins.find(plugin => plugin instanceof baseType_1.default); } visitModel(model, context) { const { root } = context; const modelType = this.baseTypePlugin.getTypename(model); const directives = model.getDirectives(dataModel_1.DirectiveModelAction.Read); const findOneQueryName = QueryPlugin.createFindOneQueryName(model); const whereUniqueInputName = this.whereInputPlugin.getWhereUniqueInputName(model); root.addQuery(`${findOneQueryName}( where: ${whereUniqueInputName}! ): ${modelType}!${directives}`); const findManyQueryName = QueryPlugin.createFindQueryName(model); const whereInputName = this.whereInputPlugin.getWhereInputName(model); const orderInputName = this.orderInputPlugin.getOrderInputName(model); const argsOnFindMany = [ `where: ${whereInputName}`, `first: Int`, `last: Int`, `skip: Int`, `take: Int`, `before: String`, `after: String`, `orderBy: ${orderInputName}` ]; root.addQuery(`${findManyQueryName} ( ${argsOnFindMany.join(`,`)} ): [ ${modelType} ! ] !${directives}`); } resolveInQuery({ model, dataSource, }) { const findOneQueryName = QueryPlugin.createFindOneQueryName(model); const findManyQueryName = QueryPlugin.createFindQueryName(model); return { [findOneQueryName]: async (root, args, context) => { const where = this.whereInputPlugin.parseUniqueWhere(args.where); if ((0, lodash_1.isUndefined)(where) === false && (0, lodash_1.isEmpty)(where)) { throw new Error(`You provided an invalid argument for the where selector on ${(0, lodash_1.capitalize)(model.getName())}. Please provide exactly one unique field and value.`); } const data = await dataSource.findOne({ where }, context); if (data) { return data; } throw new Error(`No Node for the model ${(0, lodash_1.capitalize)(model.getName())} with unique field.`); }, [findManyQueryName]: async (root, args, context) => { const where = this.whereInputPlugin.parseWhere(args.where, model); const pagination = parsePaginationFromArgs(args); const orderBy = this.orderInputPlugin.parseOrder(args.orderBy); const response = await dataSource.find({ where, pagination, orderBy }, context); return response.data; }, }; } static createFindQueryName(model) { return model.getNamings().plural; } static createFindOneQueryName(model) { return model.getNamings().singular; } } exports.default = QueryPlugin;