traxxx/src/web/postgraphile.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
/* eslint-disable arrow-body-style */
2023-06-18 23:45:45 +00:00
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
2023-06-18 23:45:45 +00:00
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,
2023-06-19 01:28:55 +00:00
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;