Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom filter name via connectionFilterName #200

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -72,6 +72,18 @@ To allow `null` and `{}` in inputs, use the `connectionFilterAllowNullInput` and

When using PostGraphile as a library, the following plugin options can be passed via `graphileBuildOptions`:

#### connectionFilterName

Specify the name of the filter:

```js
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
connectionFilterName: "filter" // default value
},
});
```

#### connectionFilterAllowedOperators

Restrict filtering to specific operators:
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"postinstall": "npm run build",
"format": "prettier --ignore-path ./.eslintignore",
"format:all": "yarn format '**/*.{json,md,html,js,jsx,ts,tsx}'",
"format:fix": "yarn format:all --write",
10 changes: 7 additions & 3 deletions src/ConnectionArgFilterPlugin.ts
Original file line number Diff line number Diff line change
@@ -2,15 +2,19 @@ import type { Plugin } from "graphile-build";

const ConnectionArgFilterPlugin: Plugin = (builder) => {
builder.hook("inflection", (inflection) => {
const {
connectionFilterName = "where"
} = inflection;
const camcelCasedFilterName = inflection.upperCamelCase(connectionFilterName);
return Object.assign(inflection, {
filterType(typeName: string) {
return `${typeName}Filter`;
return `${typeName}${camcelCasedFilterName}`;
},
filterFieldType(typeName: string) {
return `${typeName}Filter`;
return `${typeName}${camcelCasedFilterName}`;
},
filterFieldListType(typeName: string) {
return `${typeName}ListFilter`;
return `${typeName}List${camcelCasedFilterName}`;
},
});
});
24 changes: 12 additions & 12 deletions src/PgConnectionArgFilterBackwardRelationsPlugin.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { ConnectionFilterResolver } from "./PgConnectionArgFilterPlugin";

const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
builder,
{ pgSimpleCollections, pgOmitListSuffix, connectionFilterUseListInflectors }
{ pgSimpleCollections, pgOmitListSuffix, connectionFilterUseListInflectors, connectionFilterName = "filter" }
) => {
const hasConnections = pgSimpleCollections !== "only";
const simpleInflectorsAreShorter = pgOmitListSuffix === true;
@@ -28,7 +28,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
return (this as any).upperCamelCase(
`${(this as any).tableType(table)}-to-many-${(this as any).tableType(
foreignTable
)}-filter`
)}-${connectionFilterName}`
);
},
filterBackwardSingleRelationExistsFieldName(relationFieldName: string) {
@@ -79,7 +79,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
.reduce((memo: BackwardRelationSpec[], foreignConstraint) => {
if (
omit(foreignConstraint, "read") ||
omit(foreignConstraint, "filter")
omit(foreignConstraint, connectionFilterName)
) {
return memo;
}
@@ -90,7 +90,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
`Could not find the foreign table (constraint: ${foreignConstraint.name})`
);
}
if (omit(foreignTable, "read") || omit(foreignTable, "filter")) {
if (omit(foreignTable, "read") || omit(foreignTable, connectionFilterName)) {
return memo;
}
const attributes = (
@@ -309,7 +309,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
GraphQLInputObjectType,
{
name: filterManyTypeName,
description: `A filter to be used against many \`${foreignTableTypeName}\` object types. All fields are combined with a logical ‘and.’`,
description: `A ${connectionFilterName} to be used against many \`${foreignTableTypeName}\` object types. All fields are combined with a logical ‘and.’`,
},
{
foreignTable,
@@ -340,7 +340,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
FilterManyType,
makeResolveMany(spec),
spec,
`Adding connection filter backward relation field from ${describePgEntity(
`Adding connection ${connectionFilterName} backward relation field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
@@ -353,7 +353,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
GraphQLBoolean,
resolveExists,
spec,
`Adding connection filter backward relation exists field from ${describePgEntity(
`Adding connection ${connectionFilterName} backward relation exists field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
@@ -373,7 +373,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
ForeignTableFilterType,
resolveSingle,
spec,
`Adding connection filter backward relation field from ${describePgEntity(
`Adding connection ${connectionFilterName} backward relation field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
@@ -386,7 +386,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
GraphQLBoolean,
resolveExists,
spec,
`Adding connection filter backward relation exists field from ${describePgEntity(
`Adding connection ${connectionFilterName} backward relation exists field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
@@ -431,7 +431,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
every: fieldWithHooks(
"every",
{
description: `Every related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
description: `Every related \`${foreignTableTypeName}\` matches the ${connectionFilterName} criteria. All fields are combined with a logical ‘and.’`,
type: FilterType,
},
{
@@ -441,7 +441,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
some: fieldWithHooks(
"some",
{
description: `Some related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
description: `Some related \`${foreignTableTypeName}\` matches the ${connectionFilterName} criteria. All fields are combined with a logical ‘and.’`,
type: FilterType,
},
{
@@ -451,7 +451,7 @@ const PgConnectionArgFilterBackwardRelationsPlugin: Plugin = (
none: fieldWithHooks(
"none",
{
description: `No related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
description: `No related \`${foreignTableTypeName}\` matches the ${connectionFilterName} criteria. All fields are combined with a logical ‘and.’`,
type: FilterType,
},
{
3 changes: 2 additions & 1 deletion src/PgConnectionArgFilterColumnsPlugin.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ const PgConnectionArgFilterColumnsPlugin: Plugin = (builder) => {
pgColumnFilter,
pgOmit: omit,
inflection,
connectionFilterName = "where",
connectionFilterOperatorsType,
connectionFilterRegisterResolver,
connectionFilterResolve,
@@ -32,7 +33,7 @@ const PgConnectionArgFilterColumnsPlugin: Plugin = (builder) => {
)
.filter((attr) => attr.classId === table.id)
.filter((attr) => pgColumnFilter(attr, build, context))
.filter((attr) => !omit(attr, "filter"))
.filter((attr) => !omit(attr, connectionFilterName))
.reduce((memo: { [fieldName: string]: PgAttribute }, attr) => {
const fieldName: string = inflection.column(attr);
memo[fieldName] = attr;
3 changes: 2 additions & 1 deletion src/PgConnectionArgFilterCompositeTypeColumnsPlugin.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ const PgConnectionArgFilterCompositeTypeColumnsPlugin: Plugin = (
pgColumnFilter,
pgOmit: omit,
inflection,
connectionFilterName = "where",
connectionFilterRegisterResolver,
connectionFilterResolve,
connectionFilterType,
@@ -36,7 +37,7 @@ const PgConnectionArgFilterCompositeTypeColumnsPlugin: Plugin = (
)
.filter((attr) => attr.classId === table.id)
.filter((attr) => pgColumnFilter(attr, build, context))
.filter((attr) => !omit(attr, "filter"))
.filter((attr) => !omit(attr, connectionFilterName))
.filter(
(attr) =>
attr.type &&
3 changes: 2 additions & 1 deletion src/PgConnectionArgFilterComputedColumnsPlugin.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ const PgConnectionArgFilterComputedColumnsPlugin: Plugin = (
pgOmit: omit,
pgSql: sql,
inflection,
connectionFilterName = "where",
connectionFilterOperatorsType,
connectionFilterRegisterResolver,
connectionFilterResolve,
@@ -40,7 +41,7 @@ const PgConnectionArgFilterComputedColumnsPlugin: Plugin = (

// Must not be omitted
if (omit(proc, "execute")) return memo;
if (omit(proc, "filter")) return memo;
if (omit(proc, connectionFilterName)) return memo;

// Must be a computed column
const computedColumnDetails = getComputedColumnDetails(
9 changes: 5 additions & 4 deletions src/PgConnectionArgFilterForwardRelationsPlugin.ts
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ const PgConnectionArgFilterForwardRelationsPlugin: Plugin = (builder) => {
pgOmit: omit,
pgSql: sql,
pgIntrospectionResultsByKind: introspectionResultsByKind,
connectionFilterName = "where",
connectionFilterResolve,
connectionFilterRegisterResolver,
connectionFilterTypesByTypeName,
@@ -44,7 +45,7 @@ const PgConnectionArgFilterForwardRelationsPlugin: Plugin = (builder) => {
.filter((con) => con.type === "f")
.filter((con) => con.classId === table.id)
.reduce((memo: ForwardRelationSpec[], constraint) => {
if (omit(constraint, "read") || omit(constraint, "filter")) {
if (omit(constraint, "read") || omit(constraint, connectionFilterName)) {
return memo;
}
const foreignTable = constraint.foreignClassId
@@ -55,7 +56,7 @@ const PgConnectionArgFilterForwardRelationsPlugin: Plugin = (builder) => {
`Could not find the foreign table (constraint: ${constraint.name})`
);
}
if (omit(foreignTable, "read") || omit(foreignTable, "filter")) {
if (omit(foreignTable, "read") || omit(foreignTable, connectionFilterName)) {
return memo;
}
const attributes = (
@@ -239,7 +240,7 @@ const PgConnectionArgFilterForwardRelationsPlugin: Plugin = (builder) => {
ForeignTableFilterType,
resolve,
spec,
`Adding connection filter forward relation field from ${describePgEntity(
`Adding connection ${connectionFilterName} forward relation field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
@@ -254,7 +255,7 @@ const PgConnectionArgFilterForwardRelationsPlugin: Plugin = (builder) => {
GraphQLBoolean,
resolveExists,
spec,
`Adding connection filter forward relation exists field from ${describePgEntity(
`Adding connection ${connectionFilterName} forward relation exists field from ${describePgEntity(
table
)} to ${describePgEntity(foreignTable)}`
);
21 changes: 11 additions & 10 deletions src/PgConnectionArgFilterPlugin.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import { BackwardRelationSpec } from "./PgConnectionArgFilterBackwardRelationsPl
const PgConnectionArgFilterPlugin: Plugin = (
builder,
{
connectionFilterName = "where",
connectionFilterAllowedFieldTypes,
connectionFilterArrays,
connectionFilterSetofFunctions,
@@ -57,7 +58,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
if (!shouldAddFilter) return args;

if (!source) return args;
if (omit(source, "filter")) return args;
if (omit(source, connectionFilterName)) return args;

if (source.kind === "procedure") {
if (!(source.tags.filterable || connectionFilterSetofFunctions)) {
@@ -104,9 +105,9 @@ const PgConnectionArgFilterPlugin: Plugin = (
addArgDataGenerator(function connectionFilter(args: any) {
return {
pgQuery: (queryBuilder: QueryBuilder) => {
if (Object.prototype.hasOwnProperty.call(args, "filter")) {
if (Object.prototype.hasOwnProperty.call(args, connectionFilterName)) {
const sqlFragment = connectionFilterResolve(
args.filter,
args[connectionFilterName],
queryBuilder.getTableAlias(),
filterTypeName,
queryBuilder,
@@ -124,13 +125,13 @@ const PgConnectionArgFilterPlugin: Plugin = (
return extend(
args,
{
filter: {
[connectionFilterName as string]: {
description:
"A filter to be used in determining which values should be returned by the collection.",
type: FilterType,
},
},
`Adding connection filter arg to field '${field.name}' of '${Self.name}'`
`Adding connection ${connectionFilterName} arg to field '${field.name}' of '${Self.name}'`
);
}
);
@@ -154,7 +155,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
const handleNullInput = () => {
if (!connectionFilterAllowNullInput) {
throw new Error(
"Null literals are forbidden in filter argument input."
`Null literals are forbidden in ${connectionFilterName} argument input.`
);
}
return null;
@@ -163,7 +164,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
const handleEmptyObjectInput = () => {
if (!connectionFilterAllowEmptyObjectInput) {
throw new Error(
"Empty objects are forbidden in filter argument input."
`Empty objects are forbidden in ${connectionFilterName} argument input.`
);
}
return null;
@@ -217,7 +218,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
parentFieldInfo,
});
}
throw new Error(`Unable to resolve filter field '${key}'`);
throw new Error(`Unable to resolve ${connectionFilterName} field '${key}'`);
})
.filter((x) => x != null);

@@ -371,7 +372,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
GraphQLInputObjectType,
{
name: operatorsTypeName,
description: `A filter to be used against ${namedType.name}${
description: `A ${connectionFilterName} to be used against ${namedType.name}${
isListType ? " List" : ""
} fields. All fields are combined with a logical ‘and.’`,
},
@@ -410,7 +411,7 @@ const PgConnectionArgFilterPlugin: Plugin = (
return newWithHooks(
GraphQLInputObjectType,
{
description: `A filter to be used against \`${nodeTypeName}\` object types. All fields are combined with a logical ‘and.’`,
description: `A ${connectionFilterName} to be used against \`${nodeTypeName}\` object types. All fields are combined with a logical ‘and.’`,
name: filterTypeName,
},
{
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ const PostGraphileConnectionFilterPlugin: Plugin = (builder, configOptions) => {
const defaultOptions = {
//connectionFilterAllowedOperators,
//connectionFilterAllowedFieldTypes,
connectionFilterName: "filter",
connectionFilterArrays: true,
connectionFilterComputedColumns: true,
//connectionFilterOperatorNames,
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
"importHelpers": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"strictNullChecks": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,