Skip to content

Commit

Permalink
feat: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudlabat committed Jul 28, 2022
1 parent 9493c08 commit 7c3633d
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
],
"rules": {
"object-curly-spacing": "off",
"max-len":"off"
},
"ignorePatterns": [
"**/node_modules/**",
Expand Down
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/graphql-armor.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const Configuration = {
helpUrl:
'https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines',
rules: {
"type-enum": [2, 'always' ,["ci", "docs", "feat", "fix", "refactor", "test", "chore"]],
'type-enum': [2, 'always', ['ci', 'docs', 'feat', 'fix', 'refactor', 'test', 'chore']],
},
};

module.exports = Configuration;
module.exports = Configuration;
80 changes: 80 additions & 0 deletions examples/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core';
const express = require('express');
const http = require('http');
import {gql} from 'apollo-server';
import {Armor} from '../src';

const typeDefs = gql`
type Book {
title: String
author: String
}
type Nested {
child : Nested
text : String
}
type Query {
books: [Book]
}
type Mutation {
addBook(title: String, author: String): Book
}
`;


const books = [
{
title: 'The Awakening',
author: 'Kate Chopin',
},
{
title: 'City of Glass',
author: 'Paul Auster',
},
];


const resolvers = {
Query: {
books: () => books,
},
Mutation: {
addBook: (title: String, author: String) => {
return {title: 'title_test', author: 'author_test'};
},
},
};

const app = express();
const httpServer = http.createServer(app);


const armor = new Armor({

});

const server = armor.apolloServer({
typeDefs,
resolvers,
cache: 'bounded',
// eslint-disable-next-line new-cap
plugins: [ApolloServerPluginDrainHttpServer({httpServer})],
});

(async () => {
await server.start();
server.applyMiddleware({
app,
path: '/',
});

await new Promise<void>((resolve) => {
const x = httpServer
.listen({port: 4000}, resolve);
});

console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
})();
44 changes: 25 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -p tsconfig.json",
"watch": "tsc -p tsconfig.json -w",
"start": "NODE_PATH=dist node dist/index.js",
"test": "echo 'NotImplementedError'",
"lint": "npx eslint src/**/*.ts",
"lint-fix": "npx eslint --fix src/**/*.ts"
"build": "tsc -p tsconfig.json",
"watch": "tsc -p tsconfig.json -w",
"start": "NODE_PATH=dist node dist/index.js",
"test": "echo 'NotImplementedError'",
"lint": "npx eslint src/**/*.ts",
"lint-fix": "npx eslint --fix src/**/*.ts",
"example": "nodemon examples/server.ts"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]:Escape-Technologies/graphql-armor.git"
"type": "git",
"url": "git+ssh://[email protected]:Escape-Technologies/graphql-armor.git"
},
"author": "Escape Technologies SAS",
"license": "MIT",
"bugs": {
"url": "https://github.com/Escape-Technologies/graphql-armor/issues"
"url": "https://github.com/Escape-Technologies/graphql-armor/issues"
},
"homepage": "https://github.com/Escape-Technologies/graphql-armor",
"dependencies": {
"apollo-server": "^3.10.0",
"apollo-server-express": "^3.10.0",
"express": "^4.18.1",
"http": "^0.0.1-security"
},
"devDependencies": {
"@escape.tech/mookme": "^2.1.1",
"@commitlint/cli": "^17.0.0",
"@commitlint/config-angular": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"eslint": ">=8.15.0",
"eslint-config-google": "^0.14.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
"@commitlint/cli": "^17.0.0",
"@commitlint/config-angular": "^17.0.0",
"@escape.tech/mookme": "^2.1.1",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"eslint": ">=8.15.0",
"eslint-config-google": "^0.14.0",
"nodemon": "^2.0.19",
"ts-node": "^10.9.1",
"typescript": "^4.6.4"
}
}
}
22 changes: 22 additions & 0 deletions src/ArmorPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {PluginDefinition, ValidationRule} from './types';
import {Armor} from './index';

export class ArmorPlugin {
private armor: Armor;

constructor(armor: Armor) {
this.armor = armor;
}

getApolloPlugins(): PluginDefinition[] {
return [];
}

getValidationRules(): ValidationRule[] {
return [];
}

apolloPatchConfig(config) {
return config;
}
}
43 changes: 42 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
console.log('Hello World');
import {ApolloServer} from 'apollo-server-express';
import {Config} from 'apollo-server-core/src/types';
import {ExpressContext} from 'apollo-server-express/src/ApolloServer';
import {CharacterLimitPlugin, DisableIntrospectionPlugin} from './plugins';
import {ArmorPlugin} from './ArmorPlugin';
import {PluginDefinition, ValidationRule} from './types';

export type ArmorConfig = any;

export class Armor {
private armorPlugins: ArmorPlugin[] = [];

constructor(config: ArmorConfig) {
// here we add new plugins (to be moved somewhere else)
this.armorPlugins.push(new DisableIntrospectionPlugin(this));
this.armorPlugins.push(new CharacterLimitPlugin(this));
}


public apolloServer<ContextFunctionParams = ExpressContext>(config: Config<ContextFunctionParams>) {
config.plugins ||= [];
config.validationRules ||= [];

let apolloPlugins: PluginDefinition[] = [];
let validationRules: ValidationRule[] = [];

for (const plugin of this.armorPlugins) {
config = plugin.apolloPatchConfig(config);
apolloPlugins=apolloPlugins.concat(plugin.getApolloPlugins());
validationRules=validationRules.concat(plugin.getValidationRules());
}

// We prepend our plugins/rules
// So that we can protect the following user-defined plugins from attacks
config.plugins = apolloPlugins.concat(config.plugins as PluginDefinition[]);
config.validationRules = validationRules.concat(config.validationRules as ValidationRule[]);


return new ApolloServer<ContextFunctionParams>(config);
};
}

22 changes: 22 additions & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {ArmorPlugin} from './ArmorPlugin';
import {PluginDefinition} from './types';

export class DisableIntrospectionPlugin extends ArmorPlugin {
apolloPatchConfig(config) {
return {...config, introspection: false};
}
}

export class CharacterLimitPlugin extends ArmorPlugin {
getApolloPlugins(): PluginDefinition[] {
const characterLimitPlugin = {
async requestDidStart(context) {
if (context.request.query.length > 3000) {
throw new Error('Query too large.');
}
},
};

return [characterLimitPlugin];
}
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {ApolloServerPlugin} from 'apollo-server-plugin-base';

export type PluginDefinition = ApolloServerPlugin | (() => ApolloServerPlugin); // apollo-server-core/src/types.ts
export type ValidationRule = any;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "src",
"target": "es6",
"target": "es2022",
"lib": [
"es6",
"dom"
Expand Down

0 comments on commit 7c3633d

Please sign in to comment.