UNPKG

2.52 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5const neo4jGraphqlJs = require('neo4j-graphql-js');
6const neo4j = _interopDefault(require('neo4j-driver'));
7const load = require('@graphql-tools/load');
8const graphqlFileLoader = require('@graphql-tools/graphql-file-loader');
9const codeFileLoader = require('@graphql-tools/code-file-loader');
10const merge = require('@graphql-tools/merge');
11
12class Neo4JHandler {
13 constructor({ name, cache, config, pubsub }) {
14 this.name = name;
15 this.cache = cache;
16 this.config = config;
17 this.pubsub = pubsub;
18 }
19 getDriver() {
20 if (!this.driver) {
21 this.driver = neo4j.driver(this.config.url, neo4j.auth.basic(this.config.username, this.config.password));
22 this.pubsub.subscribe('destroy', () => this.driver.close());
23 }
24 return this.driver;
25 }
26 async getMeshSource() {
27 let typeDefs;
28 const cacheKey = this.name + '_introspection';
29 if (this.config.typeDefs) {
30 const typeDefsArr = await load.loadTypedefs(this.config.typeDefs, {
31 loaders: [new codeFileLoader.CodeFileLoader(), new graphqlFileLoader.GraphQLFileLoader()],
32 assumeValid: true,
33 assumeValidSDL: true,
34 });
35 typeDefs = merge.mergeTypeDefs(typeDefsArr.map(source => source.document));
36 }
37 else {
38 if (this.config.cacheIntrospection) {
39 typeDefs = await this.cache.get(cacheKey);
40 }
41 if (!typeDefs) {
42 const inferredSchema = await neo4jGraphqlJs.inferSchema(this.getDriver(), {
43 alwaysIncludeRelationships: this.config.alwaysIncludeRelationships,
44 });
45 typeDefs = inferredSchema.typeDefs;
46 if (this.config.cacheIntrospection) {
47 await this.cache.set(cacheKey, typeDefs, {
48 ttl: typeof this.config.cacheIntrospection === 'object' && this.config.cacheIntrospection.ttl,
49 });
50 }
51 }
52 }
53 const schema = neo4jGraphqlJs.makeAugmentedSchema({ typeDefs, config: { experimental: true } });
54 return {
55 schema,
56 contextBuilder: async () => ({ driver: this.getDriver(), neo4jDatabase: this.config.database }),
57 };
58 }
59}
60
61module.exports = Neo4JHandler;
62//# sourceMappingURL=index.cjs.js.map