UNPKG

@graphql-mesh/mysql

Version:
67 lines (66 loc) 2.53 kB
import { PredefinedProxyOptions } from '@graphql-mesh/store'; import { dispose, isDisposable, loadFromModuleExportExpression } from '@graphql-mesh/utils'; import { getMySQLExecutor, loadGraphQLSchemaFromMySQL } from '@omnigraph/mysql'; export default class MySQLHandler { constructor({ name, config, baseDir, pubsub, store, importFn, }) { this.name = name; this.config = config; this.baseDir = baseDir; this.pubsub = pubsub; this.importFn = importFn; this.schemaProxy = store.proxy('schema.graphql', PredefinedProxyOptions.GraphQLSchemaWithDiffing); } async getMeshSource() { const { pool: configPool } = this.config; const schema = await this.schemaProxy.getWithSet(() => { const endpointUrl = new URL('mysql://localhost:3306'); if (this.config.port) { endpointUrl.port = this.config.port.toString(); } if (this.config.host) { endpointUrl.hostname = this.config.host; } if (this.config.user) { endpointUrl.username = this.config.user; } if (this.config.password) { endpointUrl.password = this.config.password; } if (this.config.database) { endpointUrl.pathname = this.config.database; } if (this.config.ssl) { endpointUrl.protocol = 'mysqls:'; } return loadGraphQLSchemaFromMySQL(this.name, { endpoint: endpointUrl.toString(), ssl: { rejectUnauthorized: this.config.ssl?.rejectUnauthorized, caPath: this.config.ssl?.ca, }, }); }); const pool = typeof configPool === 'string' ? await loadFromModuleExportExpression(configPool, { cwd: this.baseDir, defaultExportName: 'default', importFn: this.importFn, }) : configPool; const executor = getMySQLExecutor({ subgraph: schema, pool, }); if (isDisposable(executor)) { const id = this.pubsub.subscribe('destroy', () => { // eslint-disable-next-line @typescript-eslint/no-floating-promises dispose(executor); this.pubsub.unsubscribe(id); }); } return { schema, executor, }; } }