UNPKG

@pallad/graphql-command-bus

Version:
89 lines (88 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryHelper = void 0; const graphql_1 = require("graphql"); const GQL_1 = require("./GQL"); const SORT_DIRECTION = new graphql_1.GraphQLEnumType({ name: 'SortDirection', values: { ASC: { value: 'ASC' }, DESC: { value: 'DESC' } } }); function createSortByDefinitionType(prefix, fields) { return new graphql_1.GraphQLInputObjectType({ name: prefix + '_Query_Sort', fields: { direction: { type: SORT_DIRECTION }, field: { type: new graphql_1.GraphQLEnumType({ name: prefix + '_Query_Sort_Field', values: fields.reduce((result, field) => { result[field] = { value: field }; return result; }, {}) }) } } }); } var QueryHelper; (function (QueryHelper) { function createQueryType(options) { const prefix = options.filters.name .replace(/_Filters/i, '') .replace(/_Query/i, ''); const fields = { filters: { type: new graphql_1.GraphQLNonNull(options.filters) } }; if (options.sortableFields) { fields.sortBy = { type: options.singleSortable ? createSortByDefinitionType(prefix, options.sortableFields) : new graphql_1.GraphQLList(createSortByDefinitionType(prefix, options.sortableFields)) }; } if (options.pagination) { fields.limit = { type: graphql_1.GraphQLInt }; if ('byOffset' in options.pagination) { fields.offset = { type: graphql_1.GraphQLInt }; } else if ('byNode' in options.pagination) { fields.after = { type: graphql_1.GraphQLString }; fields.before = { type: graphql_1.GraphQLString }; } } return new graphql_1.GraphQLInputObjectType({ name: prefix + '_Query', fields }); } QueryHelper.createQueryType = createQueryType; function createQueryResultType(type, pagination) { const metaType = getMetaTypeForPaginationOptions(pagination); return new graphql_1.GraphQLObjectType({ name: type.getTypeName() + '_Query_Result', fields: { results: { type: type.getTypePlural().getType() }, ...(metaType ? { meta: { type: metaType } } : {}) } }); } QueryHelper.createQueryResultType = createQueryResultType; })(QueryHelper = exports.QueryHelper || (exports.QueryHelper = {})); function getMetaTypeForPaginationOptions(pagination) { if (!pagination) { return; } if ('byNode' in pagination && pagination.byNode) { return GQL_1.QueryResultMetaPaginationByNode; } if ('byOffset' in pagination && pagination.byOffset) { return GQL_1.QueryResultMetaPaginationByOffset; } }