traxxx/src/web/postgraphile.js

57 lines
1.6 KiB
JavaScript
Executable File

'use strict';
/* eslint-disable arrow-body-style */
const config = require('config');
const { postgraphile } = require('postgraphile');
const PgConnectionFilterPlugin = require('postgraphile-plugin-connection-filter');
const PgSimplifyInflectorPlugin = require('@graphile-contrib/pg-simplify-inflector');
const PgOrderByRelatedPlugin = require('@graphile-contrib/pg-order-by-related');
const { ActorPlugins, SitePlugins, ReleasePlugins, MediaPlugins } = require('./plugins/plugins');
async function pgSettings(req) {
return {
'user.id': req.session.user?.id || null, // undefined is passed as an empty string, avoid
statement_timeout: config.database.timeout,
};
}
function initPostgraphile(credentials) {
const connectionString = `postgres://${credentials.user}:${credentials.password}@${credentials.host}:5432/${credentials.database}`;
return postgraphile(
connectionString,
'public',
{
// watchPg: true,
disableDefaultMutations: true,
dynamicJson: true,
graphiql: config.database.graphiql,
enhanceGraphiql: true,
allowExplain: () => true,
// simpleCollections: 'only',
simpleCollections: 'both',
graphileBuildOptions: {
pgOmitListSuffix: true,
// connectionFilterUseListInflectors: true,
connectionFilterRelations: true,
connectionFilterAllowNullInput: true,
},
appendPlugins: [
PgSimplifyInflectorPlugin,
PgConnectionFilterPlugin,
PgOrderByRelatedPlugin,
...ActorPlugins,
...SitePlugins,
...ReleasePlugins,
...MediaPlugins,
],
pgSettings,
},
pgSettings,
);
}
module.exports = initPostgraphile;