UNPKG

2.32 kBJavaScriptView Raw
1import { inferSchema, makeAugmentedSchema } from 'neo4j-graphql-js';
2import neo4j from 'neo4j-driver';
3import { loadTypedefs } from '@graphql-tools/load';
4import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
5import { CodeFileLoader } from '@graphql-tools/code-file-loader';
6import { mergeTypeDefs } from '@graphql-tools/merge';
7
8class 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.typeDefs) {
26 const typeDefsArr = await loadTypedefs(this.config.typeDefs, {
27 loaders: [new CodeFileLoader(), new GraphQLFileLoader()],
28 assumeValid: true,
29 assumeValidSDL: true,
30 });
31 typeDefs = mergeTypeDefs(typeDefsArr.map(source => source.document));
32 }
33 else {
34 if (this.config.cacheIntrospection) {
35 typeDefs = await this.cache.get(cacheKey);
36 }
37 if (!typeDefs) {
38 const inferredSchema = await inferSchema(this.getDriver(), {
39 alwaysIncludeRelationships: this.config.alwaysIncludeRelationships,
40 });
41 typeDefs = inferredSchema.typeDefs;
42 if (this.config.cacheIntrospection) {
43 await this.cache.set(cacheKey, typeDefs, {
44 ttl: typeof this.config.cacheIntrospection === 'object' && this.config.cacheIntrospection.ttl,
45 });
46 }
47 }
48 }
49 const schema = makeAugmentedSchema({ typeDefs, config: { experimental: true } });
50 return {
51 schema,
52 contextBuilder: async () => ({ driver: this.getDriver(), neo4jDatabase: this.config.database }),
53 };
54 }
55}
56
57export default Neo4JHandler;
58//# sourceMappingURL=index.esm.js.map