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

Rework full-text functionality #5762

Merged
merged 7 commits into from
Nov 8, 2024
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
69 changes: 69 additions & 0 deletions .changeset/loud-phones-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
"@neo4j/graphql": major
---

There have been major changes to the way that full-text search operates.

The directive now requires the specification of an index name, query name, and indexed fields.

```graphql
input FulltextInput {
indexName: String!
queryName: String!
fields: [String]!
}

"""
Informs @neo4j/graphql that there should be a fulltext index in the database, allows users to search by the index in the generated schema.
"""
directive @fulltext(indexes: [FulltextInput]!) on OBJECT
```

Here is an example of how this might be used:

```graphql
type Movie @node @fulltext(indexName: "movieTitleIndex", queryName: "moviesByTitle", fields: ["title"]) {
title: String!
}
```

Full-text search was previously available in two different locations.

The following form has now been completely removed:

```graphql
# Removed
{
movies(fulltext: { movieTitleIndex: { phrase: "The Matrix" } }) {
title
}
}
```

The following form as a root-level query has been changed:

```graphql
# Old query
query {
moviesByTitle(phrase: "The Matrix") {
score
movies {
title
}
}
}

# New query
query {
moviesByTitle(phrase: "The Matrix") {
edges {
score
node {
title
}
}
}
}
```

The new form is as a Relay connection, which allows for pagination using cursors and access to the `pageInfo` field.
2 changes: 0 additions & 2 deletions packages/graphql/src/classes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class Node extends GraphElement {
public interfaces: NamedTypeNode[];
public objectFields: ObjectField[];
public nodeDirective?: NodeDirective;
public fulltextDirective?: FullText;
public description?: string;
public limit?: LimitDirective;
public singular: string;
Expand All @@ -137,7 +136,6 @@ class Node extends GraphElement {
this.interfaces = input.interfaces;
this.objectFields = input.objectFields;
this.nodeDirective = input.nodeDirective;
this.fulltextDirective = input.fulltextDirective;
this.limit = input.limitDirective;
this.isGlobalNode = input.isGlobalNode;
this._idField = input.globalIdField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ async function checkIndexesAndConstraints({
for (const entity of schemaModel.concreteEntities) {
if (entity.annotations.fulltext) {
entity.annotations.fulltext.indexes.forEach((index) => {
const indexName = index.indexName || index.name; // TODO remove indexName assignment and undefined check once the name argument has been removed.
if (indexName === undefined) {
throw new Error("The name of the fulltext index should be defined using the indexName argument.");
}
const indexName = index.indexName;

const existingIndex = existingIndexes[indexName];
if (!existingIndex) {
Expand Down
11 changes: 1 addition & 10 deletions packages/graphql/src/graphql/directives/fulltext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import {
GraphQLString,
} from "graphql";

const deprecationReason =
"The name argument has been deprecated and will be removed in future versions " +
"Please use indexName instead. More information about the changes to @fulltext can be found at " +
"https://neo4j.com/docs/graphql-manual/current/guides/v4-migration/#_fulltext_changes.";

export const fulltextDirective = new GraphQLDirective({
name: "fulltext",
description:
Expand All @@ -40,12 +35,8 @@ export const fulltextDirective = new GraphQLDirective({
type: new GraphQLNonNull(
new GraphQLList(
new GraphQLInputObjectType({
name: "FullTextInput",
name: "FulltextInput",
fields: {
name: {
deprecationReason,
type: GraphQLString,
},
fields: {
type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
},
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql/src/schema-model/annotation/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { parseCustomResolverAnnotation } from "../parser/annotations-parser/cust
import { parseCypherAnnotation } from "../parser/annotations-parser/cypher-annotation";
import { parseDefaultAnnotation } from "../parser/annotations-parser/default-annotation";
import { parseFilterableAnnotation } from "../parser/annotations-parser/filterable-annotation";
import { parseFullTextAnnotation } from "../parser/annotations-parser/full-text-annotation";
import { parseFulltextAnnotation } from "../parser/annotations-parser/full-text-annotation";
import { parseJWTClaimAnnotation } from "../parser/annotations-parser/jwt-claim-annotation";
import { parseKeyAnnotation } from "../parser/annotations-parser/key-annotation";
import { parseLimitAnnotation } from "../parser/annotations-parser/limit-annotation";
Expand All @@ -46,7 +46,7 @@ import type { CustomResolverAnnotation } from "./CustomResolverAnnotation";
import type { CypherAnnotation } from "./CypherAnnotation";
import type { DefaultAnnotation } from "./DefaultAnnotation";
import type { FilterableAnnotation } from "./FilterableAnnotation";
import type { FullTextAnnotation } from "./FullTextAnnotation";
import type { FulltextAnnotation } from "./FulltextAnnotation";
import { IDAnnotation } from "./IDAnnotation";
import type { JWTClaimAnnotation } from "./JWTClaimAnnotation";
import { JWTPayloadAnnotation } from "./JWTPayloadAnnotation";
Expand Down Expand Up @@ -82,7 +82,7 @@ export type Annotations = CheckAnnotationName<{
cypher: CypherAnnotation;
default: DefaultAnnotation;
filterable: FilterableAnnotation;
fulltext: FullTextAnnotation;
fulltext: FulltextAnnotation;
vector: VectorAnnotation;
id: IDAnnotation;
jwt: JWTPayloadAnnotation;
Expand Down Expand Up @@ -115,7 +115,7 @@ export const annotationsParsers: { [key in keyof Annotations]: AnnotationParser<
cypher: parseCypherAnnotation,
default: parseDefaultAnnotation,
filterable: parseFilterableAnnotation,
fulltext: parseFullTextAnnotation,
fulltext: parseFulltextAnnotation,
id: () => new IDAnnotation(),
jwtClaim: parseJWTClaimAnnotation,
jwt: () => new JWTPayloadAnnotation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@

import type { Annotation } from "./Annotation";

export type FullTextField = {
name?: string;
fields: string[];
queryName?: string;
export type FulltextField = {
indexName: string;
queryName: string;
fields: string[];
};

export class FullTextAnnotation implements Annotation {
export class FulltextAnnotation implements Annotation {
readonly name = "fulltext";
public readonly indexes: FullTextField[];
public readonly indexes: FulltextField[];

constructor({ indexes }: { indexes: FullTextField[] }) {
constructor({ indexes }: { indexes: FulltextField[] }) {
this.indexes = indexes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/

import { upperFirst } from "../../../utils/upper-first";
import type { ConcreteEntityAdapter } from "./ConcreteEntityAdapter";
import type { RootTypeFieldNames as ImplementingTypeRootTypeFieldNames } from "./ImplementingEntityOperations";
import { ImplementingEntityOperations } from "./ImplementingEntityOperations";
Expand All @@ -31,13 +30,7 @@ type RootTypeFieldNames = ImplementingTypeRootTypeFieldNames & {
};
};

type FulltextTypeNames = {
result: string;
where: string;
sort: string;
};

type VectorTypeNames = {
type IndexTypeNames = {
connection: string;
edge: string;
where: string;
Expand All @@ -49,18 +42,6 @@ export class ConcreteEntityOperations extends ImplementingEntityOperations<Concr
super(concreteEntityAdapter);
}

public get fullTextInputTypeName(): string {
return `${this.entityAdapter.name}Fulltext`;
}

public getFullTextIndexInputTypeName(indexName: string): string {
return `${this.entityAdapter.name}${upperFirst(indexName)}Fulltext`;
}

public getFullTextIndexQueryFieldName(indexName: string): string {
return `${this.entityAdapter.plural}Fulltext${upperFirst(indexName)}`;
}

public get relationshipsSubscriptionWhereInputTypeName(): string {
return `${this.entityAdapter.name}RelationshipsSubscriptionWhere`;
}
Expand All @@ -85,20 +66,21 @@ export class ConcreteEntityOperations extends ImplementingEntityOperations<Concr
};
}

public get fulltextTypeNames(): FulltextTypeNames {
public get fulltextTypeNames(): IndexTypeNames {
return {
result: `${this.pascalCaseSingular}FulltextResult`,
where: `${this.pascalCaseSingular}FulltextWhere`,
sort: `${this.pascalCaseSingular}FulltextSort`,
connection: `${this.pascalCasePlural}IndexConnection`,
edge: `${this.pascalCaseSingular}IndexEdge`,
where: `${this.pascalCaseSingular}IndexWhere`,
sort: `${this.pascalCaseSingular}IndexSort`,
};
}

public get vectorTypeNames(): VectorTypeNames {
public get vectorTypeNames(): IndexTypeNames {
return {
connection: `${this.pascalCasePlural}VectorConnection`,
edge: `${this.pascalCaseSingular}VectorEdge`,
where: `${this.pascalCaseSingular}VectorWhere`,
sort: `${this.pascalCaseSingular}VectorSort`,
connection: `${this.pascalCasePlural}IndexConnection`,
edge: `${this.pascalCaseSingular}IndexEdge`,
where: `${this.pascalCaseSingular}IndexWhere`,
sort: `${this.pascalCaseSingular}IndexSort`,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/

import { upperFirst } from "graphql-compose";
import { ImplementingEntityOperations } from "./ImplementingEntityOperations";
import type { InterfaceEntityAdapter } from "./InterfaceEntityAdapter";

Expand All @@ -34,14 +33,6 @@ export class InterfaceEntityOperations extends ImplementingEntityOperations<Inte
return `${this.entityAdapter.name}ImplementationsSubscriptionWhere`;
}

public get fullTextInputTypeName(): string {
return `${this.entityAdapter.name}Fulltext`;
}

public getFullTextIndexInputTypeName(indexName: string): string {
return `${this.entityAdapter.name}${upperFirst(indexName)}Fulltext`;
}

public get connectionFieldTypename(): string {
return `${this.pascalCasePlural}Connection`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import { makeDirectiveNode } from "@graphql-tools/utils";
import type { DirectiveNode } from "graphql";
import { parseFullTextAnnotation } from "./full-text-annotation";
import { fulltextDirective } from "../../../graphql/directives";
import { parseFulltextAnnotation } from "./full-text-annotation";

describe("parseFullTextAnnotation", () => {
it("should parse correctly", () => {
Expand All @@ -29,7 +29,7 @@ describe("parseFullTextAnnotation", () => {
{ indexes: [{ indexName: "ProductName", fields: ["name"] }] },
fulltextDirective
);
const fullTextAnnotation = parseFullTextAnnotation(directive);
const fullTextAnnotation = parseFulltextAnnotation(directive);
expect(fullTextAnnotation).toEqual({
name: "fulltext",
indexes: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
*/
import type { DirectiveNode } from "graphql";
import { fulltextDirective } from "../../../graphql/directives";
import type { FullTextField } from "../../annotation/FullTextAnnotation";
import { FullTextAnnotation } from "../../annotation/FullTextAnnotation";
import { FulltextAnnotation, type FulltextField } from "../../annotation/FulltextAnnotation";
import { parseArguments } from "../parse-arguments";

export function parseFullTextAnnotation(directive: DirectiveNode): FullTextAnnotation {
const { indexes } = parseArguments<{ indexes: FullTextField[] }>(fulltextDirective, directive);
export function parseFulltextAnnotation(directive: DirectiveNode): FulltextAnnotation {
const { indexes } = parseArguments<{ indexes: FulltextField[] }>(fulltextDirective, directive);

return new FullTextAnnotation({
return new FulltextAnnotation({
indexes,
});
}
Loading
Loading