Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit fd55712

Browse files
committed
Add support for multidatabase
Use the value of context.neo4jDatabase as the database name
1 parent 7e55017 commit fd55712

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { makeAugmentedSchema } from '../../src/index';
2+
import { ApolloServer } from 'apollo-server';
3+
import neo4j from 'neo4j-driver';
4+
5+
const typeDefs = `
6+
7+
type User {
8+
name: String!
9+
wrote: [Review] @relation(name: "WROTE", direction: "OUT")
10+
}
11+
type Review {
12+
date: Date!
13+
reviewId: String!
14+
stars: Float!
15+
text: String
16+
reviews: [Business] @relation(name: "REVIEWS", direction: "OUT")
17+
users: [User] @relation(name: "WROTE", direction: "IN")
18+
}
19+
type Category {
20+
name: String!
21+
business: [Business] @relation(name: "IN_CATEGORY", direction: "IN")
22+
}
23+
type Business {
24+
address: String!
25+
city: String!
26+
location: Point!
27+
name: String!
28+
state: String!
29+
in_category: [Category] @relation(name: "IN_CATEGORY", direction: "OUT")
30+
reviews: [Review] @relation(name: "REVIEWS", direction: "IN")
31+
}
32+
`;
33+
34+
const schema = makeAugmentedSchema({ typeDefs });
35+
36+
const driver = neo4j.driver(
37+
'neo4j://localhost:7687',
38+
neo4j.auth.basic('neo4j', 'letmein'),
39+
{ encrypted: false }
40+
);
41+
42+
// Create two separate servers by hardcoding value for context.neo4jDatabase
43+
// const sanmateoServer = new ApolloServer({
44+
// schema,
45+
// context: { driver, neo4jDatabase: 'sanmateo' }
46+
// });
47+
48+
// sanmateoServer.listen(3003, '0.0.0.0').then(({ url }) => {
49+
// console.log(`San Mateo GraphQL API ready at ${url}`);
50+
// });
51+
52+
// const missoulaServer = new ApolloServer({
53+
// schema,
54+
// context: { driver, neo4jDatabase: 'missoula' }
55+
// });
56+
57+
// missoulaServer.listen(3004, '0.0.0.0').then(({ url }) => {
58+
// console.log(`Missoula GraphQL API ready at ${url}`);
59+
// });
60+
61+
// Or we can add a header to the request
62+
const server = new ApolloServer({
63+
schema,
64+
context: ({ req }) => {
65+
return { driver, neo4jDatabase: req.headers['x-database'] };
66+
}
67+
});
68+
69+
server.listen(3003, '0.0.0.0').then(({ url }) => {
70+
console.log(`GraphQL API ready at ${url}`);
71+
});

src/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,26 @@ instead: \`DEBUG=neo4j-graphql-js\`.
6767

6868
context.driver._userAgent = `neo4j-graphql-js/${neo4jGraphQLVersion}`;
6969

70-
// TODO: Is this a 4.0 driver instance? Check bolt path for default database name and use that when creating the session
71-
const session = context.driver.session();
70+
let session;
71+
72+
if (context.neo4jDatabase) {
73+
// database is specified in context object
74+
try {
75+
// connect to the specified database
76+
// must be using 4.x version of driver
77+
session = context.driver.session({
78+
database: context.neo4jDatabase
79+
});
80+
} catch (e) {
81+
// error - not using a 4.x version of driver!
82+
// fall back to default database
83+
session = context.driver.session();
84+
}
85+
} else {
86+
// no database specified
87+
session = context.driver.session();
88+
}
89+
7290
let result;
7391

7492
try {

0 commit comments

Comments
 (0)