forked from DebaucheryLibrarian/traxxx
Added internal GraphQL client, using GraphQL for scenes API.
This commit is contained in:
24
src/web/graphql.js
Normal file
24
src/web/graphql.js
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
const { withPostGraphileContext } = require('postgraphile');
|
||||
const { graphql } = require('graphql');
|
||||
|
||||
const pg = require('./postgraphile');
|
||||
const logger = require('../logger')(__filename);
|
||||
|
||||
async function query(graphqlQuery, params) {
|
||||
return withPostGraphileContext(pg, async (context) => {
|
||||
const schema = await pg.getGraphQLSchema();
|
||||
const result = await graphql(schema, graphqlQuery, null, context, params);
|
||||
|
||||
if (result.errors?.length > 0) {
|
||||
logger.error(result.errors);
|
||||
|
||||
throw result.errors[0];
|
||||
}
|
||||
|
||||
return result.data;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { graphql: query };
|
||||
39
src/web/postgraphile.js
Normal file
39
src/web/postgraphile.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
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 } = require('./plugins/plugins');
|
||||
|
||||
const connectionString = `postgres://${config.database.user}:${config.database.password}@${config.database.host}:5432/${config.database.database}`;
|
||||
|
||||
module.exports = postgraphile(
|
||||
connectionString,
|
||||
'public',
|
||||
{
|
||||
// watchPg: true,
|
||||
dynamicJson: true,
|
||||
graphiql: true,
|
||||
enhanceGraphiql: true,
|
||||
allowExplain: () => true,
|
||||
// simpleCollections: 'only',
|
||||
simpleCollections: 'both',
|
||||
graphileBuildOptions: {
|
||||
pgOmitListSuffix: true,
|
||||
connectionFilterRelations: true,
|
||||
},
|
||||
appendPlugins: [
|
||||
PgSimplifyInflectorPlugin,
|
||||
PgConnectionFilterPlugin,
|
||||
PgOrderByRelatedPlugin,
|
||||
...ActorPlugins,
|
||||
...SitePlugins,
|
||||
...ReleasePlugins,
|
||||
],
|
||||
},
|
||||
);
|
||||
@@ -3,22 +3,18 @@
|
||||
const path = require('path');
|
||||
const config = require('config');
|
||||
const express = require('express');
|
||||
const { postgraphile } = require('postgraphile');
|
||||
const Router = require('express-promise-router');
|
||||
const bodyParser = require('body-parser');
|
||||
const session = require('express-session');
|
||||
const KnexSessionStore = require('connect-session-knex')(session);
|
||||
const nanoid = require('nanoid');
|
||||
|
||||
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 logger = require('../logger')(__filename);
|
||||
const knex = require('../knex');
|
||||
const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins');
|
||||
const errorHandler = require('./error');
|
||||
|
||||
const pg = require('./postgraphile');
|
||||
|
||||
const {
|
||||
fetchScene,
|
||||
fetchScenes,
|
||||
@@ -45,34 +41,7 @@ async function initServer() {
|
||||
const router = Router();
|
||||
const store = new KnexSessionStore({ knex });
|
||||
|
||||
const connectionString = `postgres://${config.database.user}:${config.database.password}@${config.database.host}:5432/${config.database.database}`;
|
||||
|
||||
app.use(postgraphile(
|
||||
connectionString,
|
||||
'public',
|
||||
{
|
||||
// watchPg: true,
|
||||
dynamicJson: true,
|
||||
graphiql: true,
|
||||
enhanceGraphiql: true,
|
||||
allowExplain: () => true,
|
||||
// simpleCollections: 'only',
|
||||
simpleCollections: 'both',
|
||||
graphileBuildOptions: {
|
||||
pgOmitListSuffix: true,
|
||||
connectionFilterRelations: true,
|
||||
},
|
||||
appendPlugins: [
|
||||
PgSimplifyInflectorPlugin,
|
||||
PgConnectionFilterPlugin,
|
||||
PgOrderByRelatedPlugin,
|
||||
...ActorPlugins,
|
||||
...SitePlugins,
|
||||
...ReleasePlugins,
|
||||
],
|
||||
},
|
||||
));
|
||||
|
||||
app.use(pg);
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
router.use('/media', express.static(config.media.path));
|
||||
|
||||
Reference in New Issue
Block a user