postgraphile-plugin-connection-filter
Version:
Filtering on PostGraphile connections
74 lines • 3.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PgConnectionArgFilterLogicalOperatorsPlugin = void 0;
const utils_1 = require("./utils");
const version_1 = require("./version");
exports.PgConnectionArgFilterLogicalOperatorsPlugin = {
name: "PgConnectionArgFilterLogicalOperatorsPlugin",
version: version_1.version,
schema: {
hooks: {
GraphQLInputObjectType_fields(fields, build, context) {
const { extend, graphql: { GraphQLList, GraphQLNonNull }, EXPORTABLE, } = build;
const { fieldWithHooks, scope: { isPgConnectionFilter }, Self, } = context;
if (!isPgConnectionFilter)
return fields;
if (Object.keys(fields).length === 0) {
// Skip adding these operators if they would be the only fields
return fields;
}
const assertAllowed = (0, utils_1.makeAssertAllowed)(build);
const logicalOperatorFields = {
and: fieldWithHooks({
fieldName: "and",
isPgConnectionFilterOperatorLogical: true,
}, {
description: `Checks for all expressions in this list.`,
type: new GraphQLList(new GraphQLNonNull(Self)),
apply: EXPORTABLE((assertAllowed) => function ($where, value) {
assertAllowed(value, "list");
if (value == null)
return;
const $and = $where.andPlan();
// No need for this more correct form, easier to read if it's flatter.
// fieldArgs.apply(() => $and.andPlan());
return $and;
}, [assertAllowed]),
}),
or: fieldWithHooks({
fieldName: "or",
isPgConnectionFilterOperatorLogical: true,
}, {
description: `Checks for any expressions in this list.`,
type: new GraphQLList(new GraphQLNonNull(Self)),
apply: EXPORTABLE((assertAllowed) => function ($where, value) {
assertAllowed(value, "list");
if (value == null)
return;
const $or = $where.orPlan();
// Every entry is added to the `$or`, but the entries themselves should use an `and`.
return () => $or.andPlan();
}, [assertAllowed]),
}),
not: fieldWithHooks({
fieldName: "not",
isPgConnectionFilterOperatorLogical: true,
}, {
description: `Negates the expression.`,
type: Self,
apply: EXPORTABLE((assertAllowed) => function ($where, value) {
assertAllowed(value, "object");
if (value == null)
return;
const $not = $where.notPlan();
const $and = $not.andPlan();
return $and;
}, [assertAllowed]),
}),
};
return extend(fields, logicalOperatorFields, "");
},
},
},
};
//# sourceMappingURL=PgConnectionArgFilterLogicalOperatorsPlugin.js.map