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

Remove deprecated label and additonalLabels arguments #2834

Merged
merged 10 commits into from
Feb 6, 2023
6 changes: 6 additions & 0 deletions .changeset/dry-doors-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@neo4j/introspector": major
"@neo4j/graphql": major
---

Deprecated @node directive arguments `label` and `additionalLabels` have been removed. Please use the `labels` argument.
5 changes: 5 additions & 0 deletions .changeset/four-insects-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/cypher-builder": patch
---

Add support for square brackets syntax on varaible properties
5 changes: 5 additions & 0 deletions .changeset/moody-colts-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/introspector": patch
---

Fix injection through relationship labels on introspection
5 changes: 5 additions & 0 deletions .changeset/stupid-planes-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/graphql": minor
---

`@node` arguments `label` and `additionalLabels` deprecated in favor of a new `labels` argument
1 change: 1 addition & 0 deletions .github/workflows/docs-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
number: ${{ steps.get-pr-number.outputs.pr-number }}
header: docs
message: |
Looks like you've updated the documentation!

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/docs-teardown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- name: Comment on PR
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
header: docs
message: |
Thanks for the documentation updates.

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/performance-tests-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ jobs:
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
number: ${{ steps.get-pr-number.outputs.pr-number }}
header: performance
path: ./performanceReport
GITHUB_TOKEN: ${{ secrets.NEO4J_TEAM_GRAPHQL_PERSONAL_ACCESS_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/ui-stage-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
number: ${{ steps.get-pr-number.outputs.pr-number }}
header: toolbox
message: |
Made some changes to the GraphQL Toolbox? Or made some changes to the core library perhaps?

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ui-stage-teardown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- name: Comment on PR
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
header: toolbox
message: |
Thanks for the UI updates.

Expand Down
47 changes: 47 additions & 0 deletions docs/modules/ROOT/pages/guides/v4-migration/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,53 @@ type Tech @node(label: "TechDB") @plural(value: "Techs") {
}
----

[label-migration]
=== `label` and `additionalLabels` arguments removed from `@node` and replaced with new argument `labels`

There is no concept of a "main label" in the Neo4j database. As such, keeping these two separate arguments causes a disconnect between the database and the GraphQL library.
As a result, the `label` and `additionalLabels` arguments have been condensed into a single argument `labels` which will accept a list of string labels that used when a node of the given GraphQL type is created.

The equivalent of using just the `label` argument is now a list with a single value:

[source, graphql, indent=0]
----
type Tech @node(label: "TechDB") {
name: String
}
# becomes
type Tech @node(labels: ["TechDB"]) {
name: String
}
----

When creating the equivalent of using just the `additionalLabels` argument now requires the first value in the list to be the GraphQL type name:

[source, graphql, indent=0]
----
type Tech @node(additionalLabels: ["TechDB"]) {
name: String
}
# becomes
type Tech @node(labels: ["Tech", "TechDB"]) {
name: String
}
----

The equivalent of using both deprecated arguments is a list with all the values concatenated:

[source, graphql, indent=0]
----
type Tech @node(label: "TechDB", additionalLabels: ["AwesomeTech"]) {
name: String
}
# becomes
type Tech @node(labels: ["TechDB", "AwesomeTech"]) {
name: String
}
----

As before, providing none of these arguments results in the node label being the same as the GraphQL type name.

[full-text-migration]
=== `@fulltext` changes

Expand Down
110 changes: 28 additions & 82 deletions docs/modules/ROOT/pages/type-definitions/database-mapping.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,20 @@ The `@node` directive is used to specify the configuration of a GraphQL object t
----
"""Informs @neo4j/graphql of node metadata"""
directive @node(
"""Map the GraphQL type to a custom Neo4j node label"""
label: String
"""Map the GraphQL type to match additional Neo4j node labels"""
additionalLabels: [String]
"""Allows for the specification of the plural of the type name."""
plural: String
"""Map the GraphQL type to match Neo4j node labels"""
labels: [String]
) on OBJECT
----

=== Usage
`@node` can be used with the following optional parameters.

==== `label`
The parameter `label` defines the label to be used in Neo4j instead of the GraphQL type name:
==== `labels`
The parameter `labels` defines the list of label to be used in Neo4j instead of the GraphQL type name:

[source, graphql, indent=0]
----
type Movie @node(label: "Film") {
type Movie @node(labels: ["Film"]) {
title: String!
}
----
Expand All @@ -121,131 +117,81 @@ MATCH (this: Film)
RETURN this { .title } as this
----

===== Using `$jwt` and `$context`
In some cases, we may want to generate dynamic labels depending on the user requesting. In these cases, we can use the variable `$jwt` to define a custom label define in the JWT (similarly to how it is used in the xref::auth/index.adoc[`@auth` directive]):
If the GraphQL type name should still be used as a label, it needs to be specified as well:

[source, graphql, indent=0]
----
type User @node(label: "$jwt.username") {
type Dog @node(labels: ["Dog", "K9"]) {
name: String!
}
----

The following query will yield a different cypher query depending on the user JWT:
This way, the following query:

[source, graphql, indent=0]
----
{
users {
dogs {
name
}
}
----

Assuming an user with the value `"username": "arthur"` in JWT, the Cypher query will look like:
Generates the cypher query:

[source, cypher, indent=0]
----
MATCH (this:arthur)
MATCH (this:Dog:K9)
RETURN this { .name } as this
----

Similarly, context values can be passed directly:

[source, graphql, indent=0]
----
type User @node(label: "$context.appId") {
name: String!
}
----

When running the server with Apollo:

[source, js, indent=0]
----
neoSchema.getSchema().then((schema) => {
new ApolloServer({
schema,
context: ({ req }) => ({ req, appId: "myApp" }),
});
})
----

==== `additionalLabels`

`additionalLabels` lets you define extra Neo4j labels that need to exist on the node for that GraphQL type.
===== Using `$jwt` and `$context`
In some cases, we may want to generate dynamic labels depending on the user requesting. In these cases, we can use the variable `$jwt` to define a custom label define in the JWT (similarly to how it is used in the xref::auth/index.adoc[`@auth` directive]):

[source, graphql, indent=0]
----
type Actor @node(additionalLabels: ["Person", "User"]) {
type User @node(labels: ["$jwt.username"]) {
name: String!
}
----

The following query:
The following query will yield a different cypher query depending on the user JWT:

[source, graphql, indent=0]
----
{
Actor {
users {
name
}
}
----

Generates the following cypher query, with the labels `Actor`, `Person` and `User`:
Assuming an user with the value `"username": "arthur"` in JWT, the Cypher query will look like:

[source, cypher, indent=0]
----
MATCH (this:Actor:Person:User)
MATCH (this:arthur)
RETURN this { .name } as this
----

Note that `additionalLabels` can be used along with `label`:

[source, graphql, indent=0]
----
type Actor @node(label: "ActorDB", additionalLabels: ["Person"]) {
name: String!
}
----

In this case, the resulting Cypher query will use the labels `ActorDB` and `Person` instead of `Actor`:

----
MATCH (this:ActorDB:Person)
RETURN this { .name } as this
----
<<#_using_jwt_and_context,Context and JWT variables>> can be used with `additionalLabels` in the same fashion as in `label`:
Similarly, context values can be passed directly:

[source, graphql, indent=0]
----
type User @node(additionalLabels: ["$jwt.username"]) {
type User @node(label: ["$context.appId"]) {
name: String!
}
----

==== `plural`

The parameter `plural` redefines how to compose the plural of the type for the generated operations. This is particularly
useful for types that are not correctly pluralized or are non-English words.

[source, graphql, indent=0]
----
type Tech @node(plural: "Techs") {
name: String
}
----

This way, instead of the wrongly generated `teches`, the type is properly written as `techs`:
When running the server with Apollo:

[source, graphql, indent=0]
[source, js, indent=0]
----
{
techs {
title
}
}
neoSchema.getSchema().then((schema) => {
new ApolloServer({
schema,
context: ({ req }) => ({ req, appId: "myApp" }),
});
})
----

The same is applied to other operations such as `createTechs`. Note that database labels will not change.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"devDependencies": {
"@tsconfig/node16": "1.0.3",
"@typescript-eslint/eslint-plugin": "5.49.0",
"@typescript-eslint/parser": "5.49.0",
"@typescript-eslint/eslint-plugin": "5.50.0",
"@typescript-eslint/parser": "5.50.0",
"concurrently": "7.6.0",
"dotenv": "16.0.3",
"eslint": "8.33.0",
Expand Down
Loading