You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here are some examples of how you might take advantage of the OGM.
21
+
Here are some examples of how you can use the OGM.
28
22
29
23
[[ogm-examples-custom-resolvers]]
30
24
=== Custom Resolvers
31
25
32
-
A common case for using the OGM will be within custom resolvers inside a Neo4j GraphQL instance (very meta!), due to the fact that it has access to some fields which the Neo4j GraphQL Library may not. A common use case might be to have a `password` field marked with directive `@private`, and a custom resolver for creating users with passwords.
26
+
The OGM has access to some fields which the Neo4j GraphQL Library doesn't.
27
+
It is common practice to use the OGM to create custom resolvers when dealing with such fields.
28
+
For example, you can have a `password` field marked with the `@private` directive and a custom resolver for creating users with passwords.
33
29
34
-
To get started with this example, create your example application directory, create a new project and also the file which will contain your application code:
30
+
Execute the following to create an example application directory and create a new project:
Assuming a running Neo4j database at "bolt://localhost:7687" with username "neo4j" and password "password", in your empty `index.js` file, add the following code:
47
+
Assuming a running Neo4j database at "neo4j://localhost:7687" with username "username" and password "password", in your empty `index.js` file, add the following code:
52
48
53
49
[source, javascript, indent=0]
54
50
----
55
51
import { ApolloServer } from "@apollo/server";
56
52
import { startStandaloneServer } from "@apollo/server/standalone";
57
-
import { Neo4jGraphQLAuthJWTPlugin } from "@neo4j/graphql-plugin-auth";
58
53
import { Neo4jGraphQL } from "@neo4j/graphql";
59
54
import { OGM } from "@neo4j/graphql-ogm";
60
55
import neo4j from "neo4j-driver";
61
56
62
-
import { createJWT, comparePassword } from "./utils"; // example util function, more information below
57
+
import { createJWT, comparePassword } from "./utils.js"; // example util function, more information below
It's important to note the JWT secret being passed into the `Neo4jGraphQL` constructor in this example.
161
+
Create the file `utils.js` in the same directory.
162
+
Install additional dependencies:
163
+
164
+
[source, bash, indent=0]
165
+
----
166
+
npm install bcrypt jsonwebtoken
167
+
----
168
+
169
+
Add the following code to `utils.js`:
156
170
157
-
Additionally, an example implementation for the util functions `createJWT` and `comparePassword` is provided in this code snippet:
158
171
[source, javascript, indent=0]
159
172
----
160
173
import bcrypt from "bcrypt";
@@ -167,7 +180,7 @@ export function createJWT(data) {
167
180
return reject(err);
168
181
}
169
182
170
-
return resolve(token as string);
183
+
return resolve(token);
171
184
});
172
185
});
173
186
}
@@ -187,11 +200,11 @@ export function comparePassword(plainText, hash) {
187
200
188
201
[NOTE]
189
202
====
190
-
This code for the util functions `createJWT` and `comparePassword` is an example.
191
-
You will likely need to adjust it to suit your use case.
203
+
The code for the util functions `createJWT` and `comparePassword` is an example.
204
+
Adjust it to suit your use case.
192
205
====
193
206
194
-
Back in the command line, run the following command to start your server:
207
+
Back on the command line, run the following command to start your server:
195
208
196
209
[source, bash, indent=0]
197
210
----
@@ -205,31 +218,32 @@ You should see the following output:
205
218
🚀 Server ready at http://localhost:4000/
206
219
----
207
220
208
-
You can execute the `signUp` Mutation against this GraphQL API to sign up, but when you go to query the user through the same API, the password field will not be available.
221
+
You can execute the `signUp` mutation against the GraphQL API to sign up, but if you try querying the user through the same API, the password field will not be available.
209
222
210
223
[[ogm-examples-rest-api]]
211
224
=== REST API
212
225
213
-
This example demonstrates how you might use the OGM without exposing a Neo4j GraphQL API endpoint. The example starts an https://expressjs.com/[Express] server and uses the OGM to interact with the Neo4j GraphQL Library, exposed over a REST endpoint.
226
+
This example demonstrates how you can use the OGM without exposing a Neo4j GraphQL API endpoint.
227
+
It starts an https://expressjs.com/[Express] server and uses the OGM to interact with the Neo4j GraphQL Library, exposed via a REST endpoint.
214
228
215
-
First, create your example application directory, create a new project and also the file which will contain yur application code:
229
+
Execute the following to create an example application directory and a new project:
Assuming a running Neo4j database at "bolt://localhost:7687" with username "neo4j" and password "password", in your empty `index.js` file, add the following code:
246
+
Assuming a running Neo4j database at "neo4j://localhost:7687" with username "username" and password "password", in your empty `index.js` file, add the following code:
233
247
234
248
[source, javascript, indent=0]
235
249
----
@@ -238,8 +252,8 @@ import { OGM } from "@neo4j/graphql-ogm";
0 commit comments