Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions packages/hydra-cli/src/templates/graphql-server/src/index.ts.mst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { buildServerSchema, getServer } from './server';
import { queryTemplates } from './queryTemplates';
import { createProcessorStateApp } from './processorStateApp';

import { EnvelopArmor } from "@escape.tech/graphql-armor";

class CustomNamingStrategy extends SnakeNamingStrategy {
constructor() {
super();
Expand All @@ -20,9 +22,44 @@ class CustomNamingStrategy extends SnakeNamingStrategy {
}
}

async function bootstrap() {
function getArmorConfig() {
if (process.env.GRAPHQL_ENABLE_PROTECTION === 'true') {
const armor = new EnvelopArmor({
blockFieldSuggestion: {
enabled: false,
},
maxDepth: {
enabled: true,
n: 3,
},
maxAliases: {
enabled: true,
n: 5,
},
//characterLimit: {
// enabled: true,
// maxLength: 1000,
//},
maxDirectives: {
enabled: true,
n: 0,
},
});

const config = armor.protect();
const { plugins } = config;
Logger.info(`Enabling GraphQL Armor with ${plugins.length} plugins.`);
return config
} else {
return {};
}
}

function prepareServer() {
loadConfig();

const armor = getArmorConfig();

const server = getServer(
{
playgroundConfig: {
Expand All @@ -32,28 +69,37 @@ async function bootstrap() {
subscriptionEndpoint: process.env.GRAPHQL_PLAYGROUND_SUBSCRIPTION_ENDPOINT || undefined,
queryTemplates,
},
...armor,
},
{
namingStrategy: new CustomNamingStrategy(),
maxQueryExecutionTime: 1000,
logging: [process.env.WARTHOG_DB_LOGGING || 'error'],
extra: {
poolSize: 10,
//cache: {
//
//}
},
}
);

// Create database tables. Warthog migrate command does not support CustomNamingStrategy thats why
// we have this code
return server;
}

async function bootstrap() {
const server = prepareServer();

const syncDatabase: string | undefined = process.env.SYNC;
if (syncDatabase === 'true') {
await server.establishDBConnection();
process.exit(0);
}

await buildServerSchema(server);
await server.start();
const processorStateApp = createProcessorStateApp(process.env.PROCESSOR_HOST || 'localhost');
await processorStateApp.listen(
parseInt(process.env.PROCESSOR_STATE_APP_PORT || '8082'),
'0.0.0.0'
);
processorStateApp.listen(parseInt(process.env.PROCESSOR_STATE_APP_PORT || '8082'), '0.0.0.0');
}

bootstrap().catch((error: Error) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/hydra-cli/src/templates/scaffold/package.json.mst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"dependencies": {
"tslib": "^2.0.0",
"@types/bn.js": "^5.1.0",
"bn.js": "^5.1.2"
"bn.js": "^5.1.2",
"@escape.tech/graphql-armor": "^2.3.1"
},
"devDependencies": {
"@joystream/hydra-cli": "{{{hydraVersion}}}",
Expand Down