Skip to content

Commit bc918a9

Browse files
committed
adjustments after discussions
1 parent 5dc67a8 commit bc918a9

File tree

1 file changed

+66
-50
lines changed

1 file changed

+66
-50
lines changed

modules/ROOT/pages/ogm/installation.adoc

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
= Installation
44
:page-aliases: ogm/examples/index.adoc
55

6-
The OGM can be installed into a new or existing Node.js project in a similar way to the installation of the Neo4j GraphQL Library.
6+
You can install the OGM into a new or existing Node.js project in a similar way to how you install the Neo4j GraphQL Library.
77
It has the following dependencies:
88

99
* `@neo4j/graphql-ogm`: the OGM package.
@@ -15,12 +15,6 @@ It has the following dependencies:
1515
npm install @neo4j/graphql-ogm graphql neo4j-driver
1616
----
1717

18-
To use the OGM, you must always import it:
19-
20-
[source, javascript, indent=0]
21-
----
22-
import { OGM } from "@neo4j/graphql-ogm";
23-
----
2418

2519
== Usage examples
2620

@@ -55,12 +49,11 @@ Assuming a running Neo4j database at "neo4j://localhost:7687" with username "use
5549
----
5650
import { ApolloServer } from "@apollo/server";
5751
import { startStandaloneServer } from "@apollo/server/standalone";
58-
import { Neo4jGraphQLAuthJWTPlugin } from "@neo4j/graphql-plugin-auth";
5952
import { Neo4jGraphQL } from "@neo4j/graphql";
60-
import { OGM } from "@neo4j/graphql-ogm";
53+
import OGM from "@neo4j/graphql-ogm";
6154
import neo4j from "neo4j-driver";
6255
63-
import { createJWT, comparePassword } from "./utils"; // example util function, more information below
56+
import { createJWT, comparePassword } from "./utils.js"; // example util function, more information below
6457
6558
const driver = neo4j.driver(
6659
"neo4j://localhost:7687",
@@ -80,7 +73,7 @@ const typeDefs = `#graphql
8073
}
8174
`;
8275
83-
const ogm = new OGM({ typeDefs, driver });
76+
const ogm = new OGM.OGM({ typeDefs, driver });
8477
const User = ogm.model("User");
8578
8679
const resolvers = {
@@ -107,55 +100,65 @@ const resolvers = {
107100
108101
return createJWT({ sub: users[0].id });
109102
},
103+
110104
signIn: async (_source, { username, password }) => {
111105
const [user] = await User.find({
112106
where: {
113107
username,
114108
},
115109
});
116-
110+
117111
if (!user) {
118112
throw new Error(`User with username ${username} not found!`);
119113
}
120114
121115
const correctPassword = await comparePassword(password, user.password);
122116
123117
if (!correctPassword) {
124-
throw new Error(`Incorrect password for user with username ${username}!`);
118+
throw new Error(
119+
`Incorrect password for user with username ${username}!`
120+
);
125121
}
126122
127123
return createJWT({ sub: user.id });
128124
},
129125
},
130126
};
131127
128+
132129
const neoSchema = new Neo4jGraphQL({
133130
typeDefs,
134131
driver,
135132
resolvers,
136-
plugins: {
137-
auth: new Neo4jGraphQLAuthJWTPlugin({
138-
secret: "secret"
139-
})
140-
}
133+
features: {
134+
authorization: {
135+
key: "secret",
136+
},
137+
},
141138
});
142139
143-
Promise.all([neoSchema.getSchema(), ogm.init()]).then(([schema]) => {
140+
async function main() {
141+
await ogm.init();
142+
144143
const server = new ApolloServer({
145-
schema,
144+
schema: await neoSchema.getSchema(),
146145
});
147146
148-
startStandaloneServer(server, {
149-
context: async ({ req }) => ({ req }),
150-
}).then(({ url }) => {
151-
console.log(`🚀 Server ready at ${url}`);
147+
const { url } = await startStandaloneServer(server, {
148+
listen: { port: 4000 },
149+
context: async ({ req }) => ({
150+
token: req.headers.authorization,
151+
}),
152152
});
153-
});
153+
154+
console.log(`🚀 Server ready at ${url}`);
155+
}
156+
157+
main();
154158
----
155159

156-
It's important to note the JWT secret being passed into the `Neo4jGraphQL` constructor in this example.
160+
Additionally, create a file `utils.js` in the same directory and add the following code to it:
157161

158-
Additionally, the following code snippet provides an example implementation for the util functions `createJWT` and `comparePassword`:
159162
[source, javascript, indent=0]
160163
----
161164
import bcrypt from "bcrypt";
@@ -188,10 +191,17 @@ export function comparePassword(plainText, hash) {
188191

189192
[NOTE]
190193
====
191-
This code for the util functions `createJWT` and `comparePassword` is an example.
194+
The code for the util functions `createJWT` and `comparePassword` is an example.
192195
Adjust it to suit your use case.
193196
====
194197

198+
Install the additional dependencies:
199+
200+
[source, bash, indent=0]
201+
----
202+
npm install bcrypt jsonwebtoken
203+
----
204+
195205
Back on the command line, run the following command to start your server:
196206

197207
[source, bash, indent=0]
@@ -211,15 +221,16 @@ You can execute the `signUp` mutation against the GraphQL API to sign up, but if
211221
[[ogm-examples-rest-api]]
212222
=== REST API
213223

214-
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 via a REST endpoint.
224+
This example demonstrates how you can use the OGM without exposing a Neo4j GraphQL API endpoint.
225+
It starts an https://expressjs.com/[Express] server and uses the OGM to interact with the Neo4j GraphQL Library, exposed via a REST endpoint.
215226

216227
Execute the following to create an example application directory and create a new project:
217228

218229
[source, bash, indent=0]
219230
----
220231
mkdir ogm-rest-example
221232
cd ogm-rest-example
222-
npm init --yes
233+
npm init es6 --yes
223234
touch index.js
224235
----
225236

@@ -235,12 +246,12 @@ Assuming a running Neo4j database at "bolt://localhost:7687" with username "user
235246
[source, javascript, indent=0]
236247
----
237248
import express from "express";
238-
import { OGM } from "@neo4j/graphql-ogm";
249+
import OGM from "@neo4j/graphql-ogm";
239250
import neo4j from "neo4j-driver";
240251
241252
const driver = neo4j.driver(
242-
"bolt://localhost:7687",
243-
neo4j.auth.basic("username", "password")
253+
"bolt://localhost:7687",
254+
neo4j.auth.basic("username", "password")
244255
);
245256
246257
const typeDefs = `
@@ -250,34 +261,39 @@ const typeDefs = `
250261
}
251262
`;
252263
253-
const ogm = new OGM({ typeDefs, driver });
264+
const ogm = new OGM.OGM({
265+
typeDefs,
266+
driver,
267+
features: { filters: { String: { MATCHES: true } } },
268+
});
269+
254270
const User = ogm.model("User");
255271
256272
const app = express();
257273
258274
app.get("/users", async (req, res) => {
259-
const { search, offset, limit, sort } = req.query;
275+
const { search, offset, limit, sort } = req.query;
260276
261-
const regex = search ? `(?i).*${search}.*` : null;
277+
const regex = search ? `(?i).*${search}.*` : null;
262278
263-
const users = await User.find({
264-
where: { name_REGEX: regex },
265-
options: {
266-
offset,
267-
limit,
268-
sort
269-
}
270-
});
279+
const users = await User.find({
280+
where: { name_MATCHES: regex },
281+
options: {
282+
offset,
283+
limit,
284+
sort,
285+
},
286+
});
271287
272-
return res.json(users).end();
288+
return res.json(users).end();
273289
});
274290
275291
const port = 4000;
276292
277293
ogm.init().then(() => {
278-
app.listen(port, () => {
279-
console.log("Example app listening at http://localhost:${port}")
280-
});
294+
app.listen(port, () => {
295+
console.log(`Example app listening at http://localhost:${port}/users`);
296+
});
281297
});
282298
----
283299

@@ -288,11 +304,11 @@ In your application directory, you can run this application:
288304
node index.js
289305
----
290306

291-
Which should output:
307+
You should see the following output:
292308

293309
[source, bash, indent=0]
294310
----
295-
Example app listening at http://localhost:4000
311+
Example app listening at http://localhost:4000/users
296312
----
297313

298314
The REST API should now be ready to accept requests at that URL.

0 commit comments

Comments
 (0)