UNPKG

1.46 kBJavaScriptView Raw
1import { inferSchema, makeAugmentedSchema } from 'neo4j-graphql-js';
2import neo4j from 'neo4j-driver';
3
4class Neo4JHandler {
5 constructor({ name, cache, config, pubsub }) {
6 this.name = name;
7 this.cache = cache;
8 this.config = config;
9 this.pubsub = pubsub;
10 }
11 getDriver() {
12 if (!this.driver) {
13 this.driver = neo4j.driver(this.config.url, neo4j.auth.basic(this.config.username, this.config.password));
14 this.pubsub.subscribe('destroy', () => this.driver.close());
15 }
16 return this.driver;
17 }
18 async getMeshSource() {
19 let typeDefs;
20 const cacheKey = this.name + '_introspection';
21 if (this.config.cacheIntrospection) {
22 typeDefs = await this.cache.get(cacheKey);
23 }
24 if (!typeDefs) {
25 const inferredSchema = await inferSchema(this.getDriver(), {
26 alwaysIncludeRelationships: this.config.alwaysIncludeRelationships,
27 });
28 typeDefs = inferredSchema.typeDefs;
29 if (this.config.cacheIntrospection) {
30 await this.cache.set(cacheKey, typeDefs);
31 }
32 }
33 const schema = makeAugmentedSchema({ typeDefs });
34 return {
35 schema,
36 contextBuilder: async () => ({ driver: this.getDriver(), neo4jDatabase: this.config.database }),
37 };
38 }
39}
40
41export default Neo4JHandler;
42//# sourceMappingURL=index.esm.js.map