@graphql-mesh/postgraphile
Version:
134 lines (128 loc) • 5.49 kB
JavaScript
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}
const postgraphile = require('postgraphile');
const postgraphileCore = require('postgraphile-core');
const pg = _interopDefault(require('pg'));
const crossHelpers = require('@graphql-mesh/cross-helpers');
const os = require('os');
const stringInterpolation = require('@graphql-mesh/string-interpolation');
const utils = require('@graphql-mesh/utils');
const store = require('@graphql-mesh/store');
const delegate = require('@graphql-tools/delegate');
class PostGraphileHandler {
constructor({ name, config, baseDir, pubsub, store: store$1, logger, importFn, }) {
this.name = name;
this.config = config;
this.baseDir = baseDir;
this.pubsub = pubsub;
this.pgCache = store$1.proxy('pgCache.json', store.PredefinedProxyOptions.JsonWithoutValidation);
this.logger = logger;
this.importFn = importFn;
}
async getMeshSource() {
var _a, _b;
let pgPool;
if (typeof ((_a = this.config) === null || _a === void 0 ? void 0 : _a.pool) === 'string') {
pgPool = await utils.loadFromModuleExportExpression(this.config.pool, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
});
}
if (!pgPool || !('connect' in pgPool)) {
const pgLogger = this.logger.child('PostgreSQL');
pgPool = new pg.Pool({
connectionString: stringInterpolation.stringInterpolator.parse(this.config.connectionString, { env: crossHelpers.process.env }),
log: messages => pgLogger.debug(messages),
...(_b = this.config) === null || _b === void 0 ? void 0 : _b.pool,
});
}
const id = this.pubsub.subscribe('destroy', () => {
this.pubsub.unsubscribe(id);
this.logger.debug('Destroying PostgreSQL pool');
pgPool.end();
});
const cacheKey = this.name + '_introspection.json';
const dummyCacheFilePath = crossHelpers.path.join(os.tmpdir(), cacheKey);
let cachedIntrospection = await this.pgCache.get();
let writeCache;
const appendPlugins = await Promise.all((this.config.appendPlugins || []).map(pluginName => utils.loadFromModuleExportExpression(pluginName, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
})));
const skipPlugins = await Promise.all((this.config.skipPlugins || []).map(pluginName => utils.loadFromModuleExportExpression(pluginName, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
})));
const options = await utils.loadFromModuleExportExpression(this.config.options, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
});
const builder = await postgraphileCore.getPostGraphileBuilder(pgPool, this.config.schemaName || 'public', {
dynamicJson: true,
subscriptions: 'subscriptions' in this.config ? this.config.subscriptions : true,
live: 'live' in this.config ? this.config.live : true,
readCache: cachedIntrospection,
writeCache: !cachedIntrospection && dummyCacheFilePath,
setWriteCacheCallback: fn => {
writeCache = fn;
},
appendPlugins,
skipPlugins,
simpleCollections: 'both',
...options,
});
const schema = builder.buildSchema();
const defaultExecutor = delegate.createDefaultExecutor(schema);
if (!cachedIntrospection) {
await writeCache();
cachedIntrospection = await new Promise(function (resolve) { resolve(_interopNamespace(require(dummyCacheFilePath))); });
await this.pgCache.set(cachedIntrospection);
}
return {
schema,
executor({ document, variables, context: meshContext, rootValue, operationName, extensions }) {
return postgraphile.withPostGraphileContext({
pgPool,
queryDocumentAst: document,
operationName,
variables,
}, function withPgContextCallback(pgContext) {
return defaultExecutor({
document,
variables,
context: {
...meshContext,
...pgContext,
},
rootValue,
operationName,
extensions,
});
});
},
};
}
}
module.exports = PostGraphileHandler;