1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | const neo4jGraphqlJs = require('neo4j-graphql-js');
|
6 | const neo4j = _interopDefault(require('neo4j-driver'));
|
7 |
|
8 | class Neo4JHandler {
|
9 | constructor({ name, cache, config, pubsub }) {
|
10 | this.name = name;
|
11 | this.cache = cache;
|
12 | this.config = config;
|
13 | this.pubsub = pubsub;
|
14 | }
|
15 | getDriver() {
|
16 | if (!this.driver) {
|
17 | this.driver = neo4j.driver(this.config.url, neo4j.auth.basic(this.config.username, this.config.password));
|
18 | this.pubsub.subscribe('destroy', () => this.driver.close());
|
19 | }
|
20 | return this.driver;
|
21 | }
|
22 | async getMeshSource() {
|
23 | let typeDefs;
|
24 | const cacheKey = this.name + '_introspection';
|
25 | if (this.config.cacheIntrospection) {
|
26 | typeDefs = await this.cache.get(cacheKey);
|
27 | }
|
28 | if (!typeDefs) {
|
29 | const inferredSchema = await neo4jGraphqlJs.inferSchema(this.getDriver(), {
|
30 | alwaysIncludeRelationships: this.config.alwaysIncludeRelationships,
|
31 | });
|
32 | typeDefs = inferredSchema.typeDefs;
|
33 | if (this.config.cacheIntrospection) {
|
34 | await this.cache.set(cacheKey, typeDefs);
|
35 | }
|
36 | }
|
37 | const schema = neo4jGraphqlJs.makeAugmentedSchema({ typeDefs });
|
38 | return {
|
39 | schema,
|
40 | contextBuilder: async () => ({ driver: this.getDriver(), neo4jDatabase: this.config.database }),
|
41 | };
|
42 | }
|
43 | }
|
44 |
|
45 | module.exports = Neo4JHandler;
|
46 |
|