Skip to content

Add caseInsensitive field on string filters #6200

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

Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions .changeset/curvy-tires-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"@neo4j/graphql": minor
---

Add support for case insensitive string filters. These can be enabled with the option `CASE_INSENSITIVE` in features:

```javascript
const neoSchema = new Neo4jGraphQL({
features: {
filters: {
String: {
CASE_INSENSITIVE: true,
},
},
},
});
```

This enables the field `caseInsensitive` on string filters:

```graphql
query {
movies(where: { title: { caseInsensitive: { eq: "the matrix" } } }) {
title
}
}
```

This generates the following Cypher:

```cypher
MATCH (this:Movie)
WHERE toLower(this.title) = toLower($param0)
RETURN this { .title } AS this
```
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export function getStringScalarFilters(features?: Neo4jFeaturesSettings): GraphQ
case "LTE":
fields["lte"] = { type: GraphQLString };
break;
case "CASE_INSENSITIVE": {
const CaseInsensitiveFilters = getCaseInsensitiveStringScalarFilters(features);
fields["caseInsensitive"] = { type: CaseInsensitiveFilters };
}
}
}
}
Expand All @@ -59,6 +63,45 @@ export function getStringScalarFilters(features?: Neo4jFeaturesSettings): GraphQ
});
}

function getCaseInsensitiveStringScalarFilters(features?: Neo4jFeaturesSettings): GraphQLInputObjectType {
const fields = {
eq: {
type: GraphQLString,
},
in: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
contains: { type: GraphQLString },
endsWith: { type: GraphQLString },
startsWith: { type: GraphQLString },
};
for (const filter of Object.entries(features?.filters?.String ?? {})) {
const [filterName, isEnabled] = filter;
if (isEnabled) {
switch (filterName) {
case "MATCHES":
fields["matches"] = { type: GraphQLString };
break;
case "GT":
fields["gt"] = { type: GraphQLString };
break;
case "GTE":
fields["gte"] = { type: GraphQLString };
break;
case "LT":
fields["lt"] = { type: GraphQLString };
break;
case "LTE":
fields["lte"] = { type: GraphQLString };
break;
}
}
}
return new GraphQLInputObjectType({
name: "CaseInsensitiveStringScalarFilters",
description: "Case insensitive String filters",
fields,
});
}

export const StringListFilters = new GraphQLInputObjectType({
name: "StringListFilters",
description: "String list filters",
Expand Down
12 changes: 8 additions & 4 deletions packages/graphql/src/schema/get-where-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ export function getWhereFieldsForAttributes({
}
);
stringWhereOperators.forEach(({ comparator, typeName }) => {
result[`${field.name}_${comparator}`] = {
type: typeName,
directives: getAttributeDeprecationDirective(deprecatedDirectives, field, comparator),
};
const excludedComparators = ["CASE_INSENSITIVE"];
if (!excludedComparators.includes(comparator)) {
result[`${field.name}_${comparator}`] = {
type: typeName,
directives: getAttributeDeprecationDirective(deprecatedDirectives, field, comparator),
};
}
});
}
}
Expand All @@ -189,6 +192,7 @@ function getAttributeDeprecationDirective(
if (deprecatedDirectives.length) {
return deprecatedDirectives;
}

switch (comparator) {
case "DISTANCE":
case "LT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,39 @@ export class PropertyFilter extends Filter {
protected comparisonValue: unknown;
protected operator: FilterOperator;
protected attachedTo: "node" | "relationship";
protected caseInsensitive: boolean;

constructor({
attribute,
relationship,
comparisonValue,
operator,
attachedTo = "node",
caseInsensitive = false,
}: {
attribute: AttributeAdapter;
relationship?: RelationshipAdapter;
comparisonValue: unknown;
operator: FilterOperator;
attachedTo?: "node" | "relationship";
caseInsensitive?: boolean;
}) {
super();
this.attribute = attribute;
this.relationship = relationship;
this.comparisonValue = comparisonValue;
this.operator = operator;
this.attachedTo = attachedTo;
this.caseInsensitive = caseInsensitive;
}

public getChildren(): QueryASTNode[] {
return [];
}

public print(): string {
return `${super.print()} [${this.attribute.name}] <${this.operator}>`;
const caseInsensitiveStr = this.caseInsensitive ? "CASE INSENSITIVE " : "";
return `${super.print()} [${this.attribute.name}] <${caseInsensitiveStr}${this.operator}>`;
}

public getPredicate(queryASTContext: QueryASTContext): Cypher.Predicate {
Expand Down Expand Up @@ -148,6 +153,21 @@ export class PropertyFilter extends Filter {
}): Cypher.ComparisonOp {
const coalesceProperty = coalesceValueIfNeeded(this.attribute, property);

return createComparisonOperation({ operator, property: coalesceProperty, param });
if (this.caseInsensitive) {
// Need to map all the items in the list to make case insensitive checks for lists
if (operator === "IN") {
const x = new Cypher.Variable();
const lowercaseList = new Cypher.ListComprehension(x, param).map(Cypher.toLower(x));
return Cypher.in(Cypher.toLower(coalesceProperty), lowercaseList);
}

return createComparisonOperation({
operator,
property: Cypher.toLower(coalesceProperty),
param: Cypher.toLower(param),
});
} else {
return createComparisonOperation({ operator, property: coalesceProperty, param });
}
}
}
17 changes: 13 additions & 4 deletions packages/graphql/src/translate/queryAST/factory/FilterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,14 @@ export class FilterFactory {
comparisonValue,
operator,
attachedTo,
caseInsensitive,
}: {
attribute: AttributeAdapter;
relationship?: RelationshipAdapter;
comparisonValue: GraphQLWhereArg;
operator: FilterOperator | undefined;
attachedTo?: "node" | "relationship";
caseInsensitive?: boolean;
}): Filter | Filter[] {
if (attribute.annotations.cypher) {
return this.createCypherFilter({
Expand Down Expand Up @@ -294,6 +296,7 @@ export class FilterFactory {
comparisonValue,
operator,
attachedTo,
caseInsensitive,
});
}

Expand Down Expand Up @@ -560,10 +563,11 @@ export class FilterFactory {
entity: ConcreteEntityAdapter | RelationshipAdapter | InterfaceEntityAdapter,
fieldName: string,
value: Record<string, any>,
relationship?: RelationshipAdapter
relationship?: RelationshipAdapter,
caseInsensitive?: boolean
): Filter | Filter[] {
const genericFilters = Object.entries(value).flatMap((filterInput) => {
return this.parseGenericFilter(entity, fieldName, filterInput, relationship);
return this.parseGenericFilter(entity, fieldName, filterInput, relationship, caseInsensitive);
});
return this.wrapMultipleFiltersInLogical(genericFilters);
}
Expand All @@ -572,12 +576,13 @@ export class FilterFactory {
entity: ConcreteEntityAdapter | RelationshipAdapter | InterfaceEntityAdapter,
fieldName: string,
filterInput: [string, any],
relationship?: RelationshipAdapter
relationship?: RelationshipAdapter,
caseInsensitive?: boolean
): Filter | Filter[] {
const [rawOperator, value] = filterInput;
if (isLogicalOperator(rawOperator)) {
const nestedFilters = asArray(value).flatMap((nestedWhere) => {
return this.parseGenericFilter(entity, fieldName, nestedWhere, relationship);
return this.parseGenericFilter(entity, fieldName, nestedWhere, relationship, caseInsensitive);
});
return new LogicalFilter({
operation: rawOperator,
Expand All @@ -591,6 +596,9 @@ export class FilterFactory {
return this.parseGenericFilters(entity, fieldName, desugaredInput, relationship);
}

if (rawOperator === "caseInsensitive") {
return this.parseGenericFilters(entity, fieldName, value, relationship, true);
}
const operator = this.parseGenericOperator(rawOperator);

const attribute = entity.findAttribute(fieldName);
Expand All @@ -611,6 +619,7 @@ export class FilterFactory {
operator,
attachedTo,
relationship,
caseInsensitive,
});
return this.wrapMultipleFiltersInLogical(asArray(filters));
}
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ export interface Neo4jStringFiltersSettings {
LT?: boolean;
LTE?: boolean;
MATCHES?: boolean;
CASE_INSENSITIVE?: boolean;
}

export interface Neo4jIDFiltersSettings {
Expand Down
Loading
Loading