Skip to content

Commit a9ed0ce

Browse files
authored
Merge pull request #218 from neo4j/apply-unify-directives-to-6.x
Apply unify directives to 6.x
2 parents da9a958 + b578a35 commit a9ed0ce

14 files changed

+274
-149
lines changed

modules/ROOT/pages/directives/autogeneration.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This enables autogeneration of IDs for the field.
1515
The format of each generated ID is a UUID generated by the link:https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-randomuuid[`randomUUID()` function].
1616
The field will not be present in input types for mutations.
1717

18-
It is recommended to use xref::/directives/indexes-and-constraints.adoc#_unique_node_property_constraints[`@unique`] in conjunction with this to add a unique node property constraint.
18+
It is recommended to use xref::/directives/indexes-and-constraints.adoc#_unique[`@unique`] in conjunction with this to add a unique node property constraint.
1919

2020
=== Definition
2121

modules/ROOT/pages/directives/custom-logic.adoc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
The `@cypher` directive binds a GraphQL field to the results of a Cypher query.
88
This directive can be used both for properties in a type or as top level queries.
99

10-
=== Global variables
10+
=== Definition
11+
12+
==== Global variables
1113

1214
Global variables are available for use within the Cypher statement, and can be applied to the `@cypher` directive.
1315

@@ -90,14 +92,14 @@ type Query {
9092
|===
9193

9294

93-
=== Return values
95+
==== Return values
9496

9597
The return value of Cypher statements must always be of the same type to which the directive is applied.
9698

9799
The variable must also be aliased with a name that is the same as the one passed to `columnName`.
98100
This can be the name of a node, relationship query, or an alias in the `RETURN` statement of the Cypher statement.
99101

100-
==== Scalar values
102+
===== Scalar values
101103

102104
Cypher statements must return a value which matches the scalar type to which the directive was applied.
103105
For example:
@@ -109,7 +111,7 @@ type Query {
109111
}
110112
----
111113

112-
==== Object types
114+
===== Object types
113115

114116
When returning an object type, all fields of the type must be available in the Cypher return value.
115117
This can be achieved by either returning the entire object from the Cypher query, or returning a map of the fields which are required for the object type.
@@ -152,7 +154,7 @@ type Query {
152154
The downside of the latter approach is that you need to adjust the return object as you change your object type definition.
153155

154156

155-
=== Input arguments
157+
==== Input arguments
156158

157159
The `@cypher` statement can access the query parameters by prepending `$` to the parameter name.
158160
For example:
@@ -174,7 +176,7 @@ query {
174176
----
175177

176178

177-
=== Usage examples
179+
=== Usage
178180

179181
The `@cypher` directive can be used in different contexts, such as the ones described in this section.
180182

@@ -383,7 +385,7 @@ directive @customResolver(
383385
) on FIELD_DEFINITION
384386
----
385387

386-
=== The `requires` argument
388+
=== Usage
387389

388390
The `requires` argument can be used:
389391

@@ -552,7 +554,7 @@ new Neo4jGraphQL({
552554
})
553555
----
554556

555-
=== Context values
557+
==== Context values
556558

557559
The GraphQL context for the request is available as the third argument in a callback.
558560
This maps to the argument pattern for GraphQL resolvers.

modules/ROOT/pages/directives/database-mapping.adoc

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ This page describes how to use directives for database mapping.
88
Each type in your GraphQL type definitions can be mapped to an entity in your Neo4j database, such as nodes, relationships, and relationship properties.
99

1010

11-
[[type-definitions-node]]
1211
== `@node`
1312

13+
=== Definition
14+
15+
[source, graphql, indent=0]
16+
----
17+
"""Informs @neo4j/graphql of node metadata"""
18+
directive @node(
19+
"""The labels to map this GraphQL type to in the Neo4j database"""
20+
labels: [String!]
21+
) on OBJECT
22+
----
23+
24+
=== Usage
25+
1426
Adding the `@node` directive to your GraphQL type specifies that it represents a Neo4j node.
1527
For example, to represent a Neo4j node with the label "Movie" and a single property "title" of type string:
1628

@@ -27,12 +39,16 @@ In version 6.x, it's not required to specify every GraphQL type representing a N
2739
In the future, types without the `@node` directive will no longer be treated as Neo4j nodes.
2840
====
2941

30-
When not differently specified, the GraphQL type name is used as a label for the represented Neo4j node. It's possible to explicitly define the Neo4j node labels by using the argument `labels`:
42+
When not differently specified, the GraphQL type name is used as a label for the represented Neo4j node. It's possible to explicitly define the Neo4j node labels by using the parameter `labels`.
3143

32-
[discrete]
33-
=== `labels`
44+
==== The `labels` parameter
3445

35-
This parameter defines the list of label to be used in Neo4j instead of the GraphQL type name:
46+
You can append the optional parameters `labels` to a GraphQL object with the `@node` directive.
47+
This parameter lists the labels to be used in Neo4j instead of the GraphQL type name.
48+
49+
.Querying a field with the `labels` parameter
50+
====
51+
Consider the following type definition:
3652
3753
[source, graphql, indent=0]
3854
----
@@ -41,7 +57,7 @@ type Dog @node(labels: ["K9"]) {
4157
}
4258
----
4359
44-
This way, the following query:
60+
Here is a query against the `Dog` node:
4561
4662
[source, graphql, indent=0]
4763
----
@@ -52,15 +68,20 @@ This way, the following query:
5268
}
5369
----
5470
55-
Generates the Cypher query:
71+
The following Cypher is generated:
5672
5773
[source, cypher, indent=0]
5874
----
5975
MATCH (this: K9)
6076
RETURN this { .name } as name
6177
----
78+
====
6279

63-
If the GraphQL type name should still be used as a label, it needs to be specified as well:
80+
If you want to use the GraphQL type name as a label, specifiy both.
81+
82+
.Querying a field with two entries for the `labels` parameter
83+
====
84+
Consider the following type definition:
6485
6586
[source, graphql, indent=0]
6687
----
@@ -69,7 +90,8 @@ type Dog @node(labels: ["Dog", "K9"]) {
6990
}
7091
----
7192
72-
This way, the following query:
93+
Here is an example for a query against the `Dog` node
94+
7395
7496
[source, graphql, indent=0]
7597
----
@@ -80,13 +102,14 @@ This way, the following query:
80102
}
81103
----
82104
83-
Generates the Cypher query:
105+
The following Cypher is generated:
84106
85107
[source, cypher, indent=0]
86108
----
87109
MATCH (this:Dog:K9)
88110
RETURN this { .name } as this
89111
----
112+
====
90113

91114
[NOTE]
92115
====
@@ -103,12 +126,18 @@ type Dog @node(labels: ["K9", "Dog"]) {
103126
}
104127
----
105128

129+
See xref::/directives/indexes-and-constraints.adoc#_unique[`@unique`] to learn more about the `@unique` directive.
130+
106131

107-
[discrete]
108-
=== Using `$jwt` and `$context`
132+
==== Using `$jwt` and `$context`
109133

110134
In some cases, you may want to generate dynamic labels depending on the user requesting.
111-
For that, you can use the variable `$jwt` to define a custom label in the JWT:
135+
You can use the variable `$jwt` to define a custom label in the JWT.
136+
137+
138+
.Querying a field with a `$jwt` variable in the `labels` parameter
139+
====
140+
Consider the following type definition:
112141
113142
[source, graphql, indent=0]
114143
----
@@ -128,13 +157,14 @@ The following query yields a different Cypher query depending on the user JWT:
128157
}
129158
----
130159
131-
Assuming there is a user with the value `"username": "arthur"` in JWT, the Cypher query looks like:
160+
Assuming there is a user with the value `"username": "arthur"` in JWT, the Cypher query looks like this:
132161
133162
[source, cypher, indent=0]
134163
----
135164
MATCH (this:arthur)
136165
RETURN this { .name } as this
137166
----
167+
====
138168

139169
Similarly, context values can be passed directly:
140170

@@ -145,7 +175,7 @@ type User @node(label: ["$context.appId"]) {
145175
}
146176
----
147177

148-
When running the server with Apollo:
178+
For example, if you are running the server with Apollo:
149179

150180
[source, js, indent=0]
151181
----
@@ -160,26 +190,59 @@ await startStandaloneServer(server, {
160190

161191
== `@relationship`
162192

193+
=== Definition
194+
195+
[source, graphql, indent=0]
196+
----
197+
"""
198+
Instructs @neo4j/graphql to treat this field as a relationship. Opens up the ability to create and connect on this field.
199+
"""
200+
directive @relationship(
201+
type: String!
202+
"""Valid and default directions for this relationship."""
203+
queryDirection: RelationshipQueryDirection = DEFAULT_DIRECTED
204+
direction: RelationshipDirection!
205+
"""
206+
The name of the interface containing the properties for this relationship.
207+
"""
208+
properties: String
209+
"""
210+
Prevent all but these operations from being generated for this relationship
211+
"""
212+
nestedOperations: [RelationshipNestedOperations!]! = [CREATE, UPDATE, DELETE, CONNECT, DISCONNECT, CONNECT_OR_CREATE]
213+
"""Prevent aggregation for this relationship"""
214+
aggregate: Boolean = true
215+
) on FIELD_DEFINITION
216+
----
217+
218+
=== Usage
219+
163220
Relationships are represented by marking particular fields with a directive -- in this case, `@relationship`.
164-
It defines the relationship type in the database, as well as its direction.
221+
It defines the relationship type in the database, as well as which direction that relationship goes in.
165222

166-
The following type definitions add a second node type, `Actor`, and connect `Movie` and `Actor` via the `ACTED_IN` relationship:
223+
To add two node types, "Movie" and "Actor", and connect the two:
167224

168225
[source, graphql, indent=0]
169226
----
170-
type Movie @node {
227+
type Movie {
171228
title: String
172229
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
173230
}
174231
175-
type Actor @node {
232+
type Actor {
176233
name: String
177234
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
178235
}
179236
----
180237

181-
Note that, in this case, there is a directive on each "end" of the relationship, but it is not essential.
238+
[NOTE]
239+
====
240+
The `@relationship` directive is used twice, once on each end of the relationship.
241+
This is the standard way of modeling a relationship with the GraphQL Library.
242+
However, it is not a requirement of the type definitions themselves as relationships can deliberately be underspecified, for example to limit access through the API layer.
243+
====
182244

245+
See also: xref::/directives/schema-configuration/field-configuration#_relationship[`@relationship` field configuration].
183246

184247
== `@relationshipProperties`
185248

@@ -201,12 +264,12 @@ For example, for the "ACTED_IN" relationship, add a property "roles":
201264

202265
[source, graphql, indent=0]
203266
----
204-
type Movie @node {
267+
type Movie {
205268
title: String
206269
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN, properties: "ActedIn")
207270
}
208271
209-
type Actor @node {
272+
type Actor {
210273
name: String
211274
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT, properties: "ActedIn")
212275
}
@@ -220,9 +283,23 @@ Note that in addition to this type, there is an added key `properties` in the ex
220283
For more information, see xref::/types/relationships.adoc[Type definitions -> Relationships].
221284

222285

223-
[[type-definitions-alias]]
224286
== `@alias`
225287

288+
=== Definition
289+
290+
[source, graphql, indent=0]
291+
----
292+
"""
293+
Instructs @neo4j/graphql to map a GraphQL field to a Neo4j node or relationship property.
294+
"""
295+
directive @alias(
296+
"""The name of the Neo4j property"""
297+
property: String!
298+
) on FIELD_DEFINITION
299+
----
300+
301+
=== Usage
302+
226303
This directive maps a GraphQL field to a Neo4j property on a node or relationship.
227304
It can be used on any fields that are not `@cypher` or `@relationship` fields.
228305

0 commit comments

Comments
 (0)