UNPKG

@graphql-mesh/transport-neo4j

Version:
105 lines (104 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExecutableSchemaFromTypeDefsAndDriver = exports.getNeo4JExecutor = void 0; const graphql_1 = require("graphql"); const graphql_scalars_1 = require("graphql-scalars"); const delegate_1 = require("@graphql-tools/delegate"); const utils_1 = require("@graphql-tools/utils"); const graphql_2 = require("@neo4j/graphql"); const driver_js_1 = require("./driver.js"); const eventEmitterForPubSub_js_1 = require("./eventEmitterForPubSub.js"); function filterIntrospectionDefinitions(node) { if (!node.directives?.some(directive => directive.name.value === 'introspection')) { return null; } return node; } async function getNeo4JExecutor(opts) { const transportDirectives = (0, utils_1.getDirective)(opts.schema, opts.schema, 'transport'); if (!transportDirectives?.length) { throw new Error('No transport directive found on the schema!'); } const { location: endpoint, options: { database, auth }, } = transportDirectives[0]; let driver = opts.driver; if (!driver) { driver = (0, driver_js_1.getDriverFromOpts)({ endpoint, auth, logger: opts.logger, }); } let typeDefs = (0, utils_1.getDocumentNodeFromSchema)(opts.schema); const astVisitor = { enter: filterIntrospectionDefinitions, }; typeDefs = (0, graphql_1.visit)(typeDefs, { EnumTypeDefinition: astVisitor, ObjectTypeDefinition: astVisitor, InterfaceTypeDefinition: astVisitor, UnionTypeDefinition: astVisitor, InputObjectTypeDefinition: astVisitor, FieldDefinition: astVisitor, // DirectiveDefinition: astVisitor, ScalarTypeDefinition: astVisitor, EnumValueDefinition: astVisitor, InputValueDefinition: astVisitor, }); typeDefs.definitions.push(...(0, graphql_1.parse)(/* GraphQL */ ` directive @source( subgraph: String name: String type: String ) on ENUM | OBJECT | INTERFACE | UNION | INPUT_OBJECT | FIELD_DEFINITION | SCALAR | ENUM_VALUE | INPUT_FIELD_DEFINITION `).definitions); const executableSchema = await getExecutableSchemaFromTypeDefsAndDriver({ driver, logger: opts.logger, pubsub: opts.pubsub, typeDefs, }); const defaultExecutor = (0, delegate_1.createDefaultExecutor)(executableSchema); const sessionConfig = { database, }; return function neo4JExecutor(args) { return defaultExecutor({ ...args, context: { ...args.context, sessionConfig, }, }); }; } exports.getNeo4JExecutor = getNeo4JExecutor; function getExecutableSchemaFromTypeDefsAndDriver({ driver, logger, pubsub, typeDefs, }) { let features; if (pubsub) { features = { subscriptions: { events: (0, eventEmitterForPubSub_js_1.getEventEmitterFromPubSub)(pubsub), publish: eventMeta => pubsub.publish(eventMeta.event, eventMeta), close: () => { }, }, }; const id = pubsub.subscribe('destroy', async () => { pubsub.unsubscribe(id); logger?.debug('Closing Neo4j'); await driver.close(); logger?.debug('Neo4j closed'); }); } const neo4jGraphQL = new graphql_2.Neo4jGraphQL({ typeDefs, driver, validate: false, debug: !!process.env.DEBUG, resolvers: { BigInt: graphql_scalars_1.GraphQLBigInt, }, features, }); return neo4jGraphQL.getSchema(); } exports.getExecutableSchemaFromTypeDefsAndDriver = getExecutableSchemaFromTypeDefsAndDriver;