Skip to content

Commit 38875ad

Browse files
authored
Merge pull request #211 from neo4j/unify-directives
unify directives presentation
2 parents bb73761 + a7b6670 commit 38875ad

14 files changed

+267
-146
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: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,37 @@ Each type in your GraphQL type definitions can be mapped to an entity in your Ne
1010

1111
== `@relationship`
1212

13+
=== Definition
14+
15+
[source, graphql, indent=0]
16+
----
17+
"""
18+
Instructs @neo4j/graphql to treat this field as a relationship. Opens up the ability to create and connect on this field.
19+
"""
20+
directive @relationship(
21+
type: String!
22+
"""Valid and default directions for this relationship."""
23+
queryDirection: RelationshipQueryDirection = DEFAULT_DIRECTED
24+
direction: RelationshipDirection!
25+
"""
26+
The name of the interface containing the properties for this relationship.
27+
"""
28+
properties: String
29+
"""
30+
Prevent all but these operations from being generated for this relationship
31+
"""
32+
nestedOperations: [RelationshipNestedOperations!]! = [CREATE, UPDATE, DELETE, CONNECT, DISCONNECT, CONNECT_OR_CREATE]
33+
"""Prevent aggregation for this relationship"""
34+
aggregate: Boolean = true
35+
) on FIELD_DEFINITION
36+
----
37+
38+
=== Usage
39+
1340
Relationships are represented by marking particular fields with a directive -- in this case, `@relationship`.
1441
It defines the relationship type in the database, as well as which direction that relationship goes in.
1542

16-
To add a second node type, "Actor", and connect the two together, you should do the following:
43+
To add two node types, "Movie" and "Actor", and connect the two:
1744

1845
[source, graphql, indent=0]
1946
----
@@ -28,8 +55,14 @@ type Actor {
2855
}
2956
----
3057

31-
Note that, in this case, there is a directive on each "end" of the relationship, but it is not essential.
58+
[NOTE]
59+
====
60+
The `@relationship` directive is used twice, once on each end of the relationship.
61+
This is the standard way of modeling a relationship with the GraphQL Library.
62+
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.
63+
====
3264

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

3467
== `@relationshipProperties`
3568

@@ -70,9 +103,21 @@ Note that in addition to this type, there is an added key `properties` in the ex
70103
For more information, see xref::/types/relationships.adoc[Type definitions -> Relationships].
71104

72105

73-
[[type-definitions-node]]
74106
== `@node`
75107

108+
=== Definition
109+
110+
[source, graphql, indent=0]
111+
----
112+
"""Informs @neo4j/graphql of node metadata"""
113+
directive @node(
114+
"""The labels to map this GraphQL type to in the Neo4j database"""
115+
labels: [String!]
116+
) on OBJECT
117+
----
118+
119+
=== Usage
120+
76121
The most basic mapping uses GraphQL type names to map to a Neo4j node label.
77122
For example, to represent a node with the label "Movie" and a single property "title" of type string:
78123

@@ -84,13 +129,15 @@ type Movie {
84129
----
85130

86131
With the `@node` directive, you have more control over this mapping, and you can specify the configuration of a GraphQL object type which represents a Neo4j node.
87-
It can be appended with the following optional parameters:
88132

133+
==== The `labels` parameter
89134

90-
[discrete]
91-
=== `labels`
135+
You can append the optional parameters `labels` to a GraphQL object with the `@node` directive.
136+
This parameter lists the labels to be used in Neo4j instead of the GraphQL type name.
92137

93-
This parameter defines the list of label to be used in Neo4j instead of the GraphQL type name:
138+
.Querying a field with the `labels` parameter
139+
====
140+
Consider the following type definition:
94141
95142
[source, graphql, indent=0]
96143
----
@@ -99,7 +146,7 @@ type Dog @node(labels: ["K9"]) {
99146
}
100147
----
101148
102-
This way, the following query:
149+
Here is a query against the `Dog` node:
103150
104151
[source, graphql, indent=0]
105152
----
@@ -110,15 +157,20 @@ This way, the following query:
110157
}
111158
----
112159
113-
Generates the Cypher query:
160+
The following Cypher is generated:
114161
115162
[source, cypher, indent=0]
116163
----
117164
MATCH (this: K9)
118165
RETURN this { .name } as name
119166
----
167+
====
120168

121-
If the GraphQL type name should still be used as a label, it needs to be specified as well:
169+
If you want to use the GraphQL type name as a label, specifiy both.
170+
171+
.Querying a field with two entries for the `labels` parameter
172+
====
173+
Consider the following type definition:
122174
123175
[source, graphql, indent=0]
124176
----
@@ -127,7 +179,8 @@ type Dog @node(labels: ["Dog", "K9"]) {
127179
}
128180
----
129181
130-
This way, the following query:
182+
Here is an example for a query against the `Dog` node
183+
131184
132185
[source, graphql, indent=0]
133186
----
@@ -138,13 +191,14 @@ This way, the following query:
138191
}
139192
----
140193
141-
Generates the Cypher query:
194+
The following Cypher is generated:
142195
143196
[source, cypher, indent=0]
144197
----
145198
MATCH (this:Dog:K9)
146199
RETURN this { .name } as this
147200
----
201+
====
148202

149203
[NOTE]
150204
====
@@ -161,12 +215,18 @@ type Dog @node(labels: ["K9", "Dog"]) {
161215
}
162216
----
163217

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

165-
[discrete]
166-
=== Using `$jwt` and `$context`
221+
==== Using `$jwt` and `$context`
167222

168223
In some cases, you may want to generate dynamic labels depending on the user requesting.
169-
For that, you can use the variable `$jwt` to define a custom label in the JWT:
224+
You can use the variable `$jwt` to define a custom label in the JWT.
225+
226+
227+
.Querying a field with a `$jwt` variable in the `labels` parameter
228+
====
229+
Consider the following type definition:
170230
171231
[source, graphql, indent=0]
172232
----
@@ -186,13 +246,14 @@ The following query yields a different Cypher query depending on the user JWT:
186246
}
187247
----
188248
189-
Assuming there is a user with the value `"username": "arthur"` in JWT, the Cypher query looks like:
249+
Assuming there is a user with the value `"username": "arthur"` in JWT, the Cypher query looks like this:
190250
191251
[source, cypher, indent=0]
192252
----
193253
MATCH (this:arthur)
194254
RETURN this { .name } as this
195255
----
256+
====
196257

197258
Similarly, context values can be passed directly:
198259

@@ -203,7 +264,7 @@ type User @node(label: ["$context.appId"]) {
203264
}
204265
----
205266

206-
When running the server with Apollo:
267+
For example, if you are running the server with Apollo:
207268

208269
[source, js, indent=0]
209270
----
@@ -217,9 +278,23 @@ await startStandaloneServer(server, {
217278
----
218279

219280

220-
[[type-definitions-alias]]
221281
== `@alias`
222282

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

0 commit comments

Comments
 (0)