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

Generic filtering with interfaces and cypher fields. #5855

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
27 changes: 17 additions & 10 deletions packages/graphql/src/translate/queryAST/factory/FilterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ export class FilterFactory {
if (!attribute) {
throw new Error(`Attribute ${fieldName} not found`);
}
if (!operator) {
const genericFilters = Object.entries(value).flatMap((filterInput) => {
return this.parseGenericFilter(entity, fieldName, filterInput);
});
return this.wrapMultipleFiltersInLogical(genericFilters);
}

return this.createPropertyFilter({
attribute,
Expand Down Expand Up @@ -506,7 +512,7 @@ export class FilterFactory {
}

// This is a bit hacky, basically skipping cypher fields and federation strings being passed to filterFactory
if (!operator && !attribute.annotations.cypher && typeof value === "object") {
if (!operator && !attribute.annotations.cypher?.targetEntity && typeof value === "object") {
const genericFilters = Object.entries(value).flatMap((filterInput) => {
return this.parseGenericFilter(entity, fieldName, filterInput);
});
Expand All @@ -526,7 +532,7 @@ export class FilterFactory {
}

private parseGenericFilter(
entity: ConcreteEntityAdapter | RelationshipAdapter,
entity: ConcreteEntityAdapter | RelationshipAdapter | InterfaceEntityAdapter,
fieldName: string,
filterInput: [string, any]
): Filter | Filter[] {
Expand Down Expand Up @@ -554,8 +560,8 @@ export class FilterFactory {
const attribute = entity.findAttribute(fieldName);

if (!attribute) {
if (isRelationshipEntity(entity)) {
throw new Error("Transpilation error: Expected a Concrete Entity found a Relationship");
if (isRelationshipEntity(entity) || isInterfaceEntity(entity)) {
throw new Error("Transpilation error: Expected concrete entity");
}
if (fieldName === "id" && entity.globalIdField) {
return this.createRelayIdPropertyFilter(entity, false, operator, value);
Expand Down Expand Up @@ -696,19 +702,20 @@ export class FilterFactory {
});
}
const { fieldName, operator, isNot } = parseWhereField(key);
if (!operator) {
const genericFilters = Object.entries(value).flatMap((filterInput) => {
return this.parseGenericFilter(relationship, fieldName, filterInput);
});
return this.wrapMultipleFiltersInLogical(genericFilters);
}

const attribute = relationship.findAttribute(fieldName);
if (!attribute) {
if (fieldName === relationship.propertiesTypeName) {
return this.createEdgeFilters(relationship, value);
}
return;
}
if (!operator) {
const genericFilters = Object.entries(value).flatMap((filterInput) => {
return this.parseGenericFilter(relationship, fieldName, filterInput);
});
return this.wrapMultipleFiltersInLogical(genericFilters);
}

return this.createPropertyFilter({
attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

import { generate } from "randomstring";
import type { UniqueType } from "../../utils/graphql-types";
import { TestHelper } from "../../utils/tests-helper";
import type { UniqueType } from "../../../utils/graphql-types";
import { TestHelper } from "../../../utils/tests-helper";

describe("Advanced Filtering - deprecated", () => {
const testHelper = new TestHelper();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { UniqueType } from "../../../utils/graphql-types";
import { TestHelper } from "../../../utils/tests-helper";

describe("cypher directive filtering - Scalar - deprecated", () => {
let CustomType: UniqueType;

const testHelper = new TestHelper();

afterEach(async () => {
await testHelper.close();
});

beforeEach(() => {
CustomType = testHelper.createUniqueType("CustomType");
});

test.each([
{
title: "Int cypher field: exact match",
filter: `special_count_EQ: 1`,
},
{
title: "Int cypher field: GT",
filter: `special_count_GT: 0`,
},
{
title: "Int cypher field: GTE",
filter: `special_count_GTE: 1`,
},
{
title: "Int cypher field: LT",
filter: `special_count_LT: 2`,
},
{
title: "Int cypher field: LTE",
filter: `special_count_LTE: 2`,
},
{
title: "Int cypher field: IN",
filter: `special_count_IN: [1, 2, 3]`,
},
] as const)("$title", async ({ filter }) => {
const typeDefs = /* GraphQL */ `
type ${CustomType} @node {
title: String
special_count: Int
@cypher(
statement: """
MATCH (m:${CustomType})
RETURN count(m) as c
"""
columnName: "c"
)
}
`;

await testHelper.initNeo4jGraphQL({ typeDefs });
await testHelper.executeCypher(`CREATE (m:${CustomType} { title: "test" })`, {});

const query = /* GraphQL */ `
query {
${CustomType.plural}(where: { ${filter} }) {
special_count
}
}
`;

const gqlResult = await testHelper.executeGraphQL(query);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult?.data).toEqual({
[CustomType.plural]: [
{
special_count: 1,
},
],
});
});

test.each([
{
title: "String cypher field: exact match",
filter: `special_word_EQ: "test"`,
},
{
title: "String cypher field: CONTAINS",
filter: `special_word_CONTAINS: "es"`,
},
{
title: "String cypher field: ENDS_WITH",
filter: `special_word_ENDS_WITH: "est"`,
},
{
title: "String cypher field: STARTS_WITH",
filter: `special_word_STARTS_WITH: "tes"`,
},
{
title: "String cypher field: IN",
filter: `special_word_IN: ["test", "test2"]`,
},
] as const)("$title", async ({ filter }) => {
const typeDefs = /* GraphQL */ `
type ${CustomType} @node {
title: String
special_word: String
@cypher(
statement: """
RETURN "test" as s
"""
columnName: "s"
)
}
`;

await testHelper.initNeo4jGraphQL({ typeDefs });
await testHelper.executeCypher(`CREATE (m:${CustomType} { title: "test" })`, {});

const query = /* GraphQL */ `
query {
${CustomType.plural}(where: { ${filter} }) {
title
}
}
`;

const gqlResult = await testHelper.executeGraphQL(query);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult?.data).toEqual({
[CustomType.plural]: [
{
title: "test",
},
],
});
});

test("Int cypher field AND String title field", async () => {
const typeDefs = /* GraphQL */ `
type ${CustomType} @node {
title: String
special_count: Int
@cypher(
statement: """
MATCH (m:${CustomType})
RETURN count(m) as c
"""
columnName: "c"
)
}
`;

await testHelper.initNeo4jGraphQL({ typeDefs });
await testHelper.executeCypher(
`
UNWIND [
{title: 'CustomType One' },
{title: 'CustomType Two' },
{title: 'CustomType Three' }
] AS CustomTypeData
CREATE (m:${CustomType})
SET m = CustomTypeData;
`,
{}
);

const query = /* GraphQL */ `
query {
${CustomType.plural}(where: { special_count_GTE: 1, title_EQ: "CustomType One" }) {
special_count
}
}
`;

const gqlResult = await testHelper.executeGraphQL(query);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult?.data).toEqual({
[CustomType.plural]: [
{
special_count: 3,
},
],
});
});

test("unmatched Int cypher field AND String title field", async () => {
const typeDefs = /* GraphQL */ `
type ${CustomType} @node {
title: String
special_count: Int
@cypher(
statement: """
MATCH (m:${CustomType})
RETURN count(m) as c
"""
columnName: "c"
)
}
`;

await testHelper.initNeo4jGraphQL({ typeDefs });
await testHelper.executeCypher(
`
UNWIND [
{title: 'CustomType One' },
{title: 'CustomType Two' },
{title: 'CustomType Three' }
] AS CustomTypeData
CREATE (m:${CustomType})
SET m = CustomTypeData;
`,
{}
);

const query = /* GraphQL */ `
query {
${CustomType.plural}(where: { special_count_GTE: 1, title_EQ: "CustomType Unknown" }) {
special_count
}
}
`;

const gqlResult = await testHelper.executeGraphQL(query);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult?.data).toEqual({
[CustomType.plural]: [],
});
});

test("Int cypher field, selecting String title field", async () => {
const typeDefs = /* GraphQL */ `
type ${CustomType} @node {
title: String
special_count: Int
@cypher(
statement: """
MATCH (m:${CustomType})
RETURN count(m) as c
"""
columnName: "c"
)
}
`;

await testHelper.initNeo4jGraphQL({ typeDefs });
await testHelper.executeCypher(`CREATE (m:${CustomType} { title: "test" })`, {});

const query = /* GraphQL */ `
query {
${CustomType.plural}(where: { special_count_GTE: 1 }) {
title
}
}
`;

const gqlResult = await testHelper.executeGraphQL(query);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult?.data).toEqual({
[CustomType.plural]: [
{
title: "test",
},
],
});
});
});
Loading