Merge Complete

This commit is contained in:
Luis Fernando Molina committed 2020-11-20 20:28:04 +01:00
1 parent 482ef7a81a
commit dabe23d505
6483 files changed

No files matched your search

@@ -0,0 +1,43 @@
import 'dotenv/config';
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import schema from './schema';
import wrongPreshared from './errors/notAllowed.json';
const app = express();
app.post('/graphql', (req, res, next) => {
if (req.headers.preshared === process.env.PRESHARED_GATEWAY_KEY) {
next();
} else {
res.send(wrongPreshared);
}
});
// Starting the graphql server
const server = new ApolloServer({
schema,
playground: {
settings: {
'editor.theme': 'dark',
'editor.cursorShape': 'line',
},
},
context: ({ req }) => ({
userId: req.headers.userid,
authorization: req.headers.authorization,
}),
formatError: error => ({
code: error.extensions.exception.code,
message: error.message,
path: error.path,
}),
introspection: true,
});
server.applyMiddleware({
app,
cors: true,
});
export default app;
@@ -0,0 +1,11 @@
import 'dotenv/config';
import mongoose from 'mongoose';
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
export default db;
@@ -0,0 +1,23 @@
export class AuthentificationError extends Error {
code = 401
message = 'Autentification not found, you have to be authentificated to perform this action'
}
export class DatabaseError extends Error {
code = 404
message = `Ressource not found : ${this.message}`
}
export class WrongArgumentsError extends Error {
code = 400
message = `Wrong arguments : ${this.message}`
}
export class ServiceUnavailableError extends Error {
code = 503
message = `At least one service is unresponding ${this.message}`
}
@@ -0,0 +1,8 @@
{
"errors": [
{
"code": 401,
"message": "Wait, you're not supposed to be here, go through the gate as everyone else"
}
]
}
@@ -0,0 +1,14 @@
/* eslint-disable no-console */
import db from './db';
import app from './app';
db.on('error', (err) => {
console.log(err);
});
db.once('open', () => {
console.log('Connected to the DB.');
});
app.listen(process.env.PORT, () => {
console.log(`Listening on port ${process.env.PORT}`);
});
@@ -0,0 +1,24 @@
import mongoose, { Schema } from 'mongoose';
const tradingSystemSchema = new Schema(
{
fbSlug: {
type: String,
required: true,
unique: true,
dropDups: true,
},
data: Object,
history: [{
updatedAt: Date,
data: Object,
}],
},
{
timestamps: true,
},
);
const TradingSystem = mongoose.model('TradingSystem', tradingSystemSchema);
export default TradingSystem;
@@ -0,0 +1,6 @@
/* eslint-disable import/prefer-default-export */
import TradingSystem from './TradingSystem';
export {
TradingSystem,
};
@@ -0,0 +1,13 @@
import {
GraphQLSchema,
} from 'graphql';
import Mutation from './mutations';
import RootQuery from './queries';
const Schema = new GraphQLSchema({
query: RootQuery,
mutation: Mutation,
});
export default Schema;
@@ -0,0 +1,42 @@
import {
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { TradingSystemType } from '../types';
import { TradingSystem } from '../../models';
import { TradingSystemInputType } from '../types/input';
import defaultTradingSystem from '../../templates/tradingSystemTemplate';
export const args = {
fbSlug: { type: new GraphQLNonNull(GraphQLString) },
tradingSystem: { type: TradingSystemInputType },
};
const resolve = (parent, { fbSlug, tradingSystem }) => {
if (tradingSystem === undefined) {
tradingSystem = defaultTradingSystem;
}
tradingSystem.fbSlug = fbSlug;
tradingSystem.history = [];
const newTradingSystem = new TradingSystem(tradingSystem);
return new Promise((res, rej) => {
newTradingSystem.save((err) => {
if (err) {
rej(err);
return;
}
res(newTradingSystem);
});
});
};
const mutation = {
createTradingSystem: {
type: TradingSystemType,
args,
resolve,
},
};
export default mutation;
@@ -0,0 +1,47 @@
import {
GraphQLNonNull,
GraphQLID,
} from 'graphql';
import { TradingSystemType } from '../types';
import { TradingSystem } from '../../models';
import { TradingSystemInputType } from '../types/input';
const args = {
id: { type: new GraphQLNonNull(GraphQLID) },
tradingSystem: { type: new GraphQLNonNull(TradingSystemInputType) },
};
const resolve = (parent, { id: _id, tradingSystem: editedTradingSystem }) => new Promise((res, rej) => {
TradingSystem.findOne({ _id }).exec((err, tradingSystem) => {
if (err) {
rej(err);
return;
}
if (!tradingSystem) {
rej(err);
return;
}
tradingSystem.history.push({
updatedAt: tradingSystem.updatedAt,
data: tradingSystem.data,
});
tradingSystem.data = editedTradingSystem.data;
tradingSystem.save((error) => {
if (error) {
rej(error);
return;
}
res(tradingSystem);
});
});
});
const mutation = {
editTradingSystem: {
type: TradingSystemType,
args,
resolve,
},
};
export default mutation;
@@ -0,0 +1,16 @@
import {
GraphQLObjectType,
} from 'graphql';
import CreateTradingSystemMutation from './CreateTradingSystem';
import EditTradingSystemMutation from './EditTradingSystem';
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: Object.assign(
CreateTradingSystemMutation,
EditTradingSystemMutation,
),
});
export default Mutation;
@@ -0,0 +1,35 @@
import {
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { DatabaseError } from '../../errors';
import { TradingSystemType } from '../types';
import { TradingSystem } from '../../models';
const args = {
fbSlug: { type: new GraphQLNonNull(GraphQLString) },
};
const resolve = (parent, { fbSlug }) => new Promise((res, rej) => {
TradingSystem.findOne({ fbSlug }).exec((err, tradingSystem) => {
if (err) {
rej(err);
return;
}
if (!tradingSystem) {
rej(new DatabaseError('None of the tradingSystem are linked to that fbSlug'));
return;
}
res(tradingSystem);
});
});
const query = {
tradingSystemByFb: {
type: TradingSystemType,
args,
resolve,
},
};
export default query;
@@ -0,0 +1,35 @@
import {
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { DatabaseError } from '../../errors';
import { TradingSystemType } from '../types';
import { TradingSystem } from '../../models';
const args = {
id: { type: new GraphQLNonNull(GraphQLString) },
};
const resolve = (parent, { id }) => new Promise((res, rej) => {
TradingSystem.findOne({ _id: id }).exec((err, tradingSystem) => {
if (err) {
rej(err);
return;
}
if (!tradingSystem) {
rej(new DatabaseError('None of the tradingSystem are linked to that id'));
return;
}
res(tradingSystem);
});
});
const query = {
tradingSystemById: {
type: TradingSystemType,
args,
resolve,
},
};
export default query;
@@ -0,0 +1,16 @@
import {
GraphQLObjectType,
} from 'graphql';
import TradingSystemByFbQuery from './TradingSystemByFB';
import TradingSystemByIdQuery from './TradingSystemById';
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: Object.assign(
TradingSystemByFbQuery,
TradingSystemByIdQuery,
),
});
export default RootQuery;
@@ -0,0 +1,83 @@
/* eslint-disable no-void */
exports.__esModule = true;
exports.GraphQLJSONObject = void 0;
exports.default = void 0;
exports.GraphQLJSON = void 0;
const _graphql = require('graphql');
const _language = require('graphql/language');
function identity(value) {
return value;
}
function ensureObject(value) {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
throw new TypeError(`JSONObject cannot represent non-object value: ${value}`);
}
return value;
}
function parseObject(ast, variables) {
const value = Object.create(null);
ast.fields.forEach((field) => {
// eslint-disable-next-line no-use-before-define
value[field.name.value] = parseLiteral(field.value, variables);
});
return value;
}
function parseLiteral(ast, variables) {
switch (ast.kind) {
case _language.Kind.STRING:
case _language.Kind.BOOLEAN:
return ast.value;
case _language.Kind.INT:
case _language.Kind.FLOAT:
return parseFloat(ast.value);
case _language.Kind.OBJECT:
return parseObject(ast, variables);
case _language.Kind.LIST:
return ast.values.map(n => parseLiteral(n, variables));
case _language.Kind.NULL:
return null;
case _language.Kind.VARIABLE:
{
const name = ast.name.value;
return variables ? variables[name] : undefined;
}
default:
return undefined;
}
} // This named export is intended for users of CommonJS. Users of ES modules
// should instead use the default export.
const GraphQLJSON = new _graphql.GraphQLScalarType({
name: 'JSON',
description: 'The `JSON` scalar type represents JSON values as specified by'
+ ' [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).',
serialize: identity,
parseValue: identity,
parseLiteral,
});
exports.GraphQLJSON = GraphQLJSON;
const _default = GraphQLJSON;
exports.default = _default;
const GraphQLJSONObject = new _graphql.GraphQLScalarType({
name: 'JSONObject',
description: 'The `JSONObject` scalar type represents JSON objects as specified by'
+ ' [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).',
serialize: ensureObject,
parseValue: ensureObject,
parseLiteral: parseObject,
});
exports.GraphQLJSONObject = GraphQLJSONObject;
@@ -0,0 +1,18 @@
import {
GraphQLObjectType,
GraphQLString,
GraphQLID,
} from 'graphql';
import GraphQLJSON from './GraphQLJSONObject';
const Type = new GraphQLObjectType({
name: 'TradingSystem',
description: 'Everything you need to know about a trading system',
fields: () => ({
id: { type: GraphQLID },
fbSlug: { type: GraphQLString },
data: { type: GraphQLJSON },
}),
});
export default Type;
Whitespace-only changes.
@@ -0,0 +1,6 @@
import TradingSystemType from './TradingSystem';
export {
// eslint-disable-next-line import/prefer-default-export
TradingSystemType,
};
@@ -0,0 +1,14 @@
import {
GraphQLInputObjectType,
} from 'graphql';
import GraphQLJSON from '../GraphQLJSONObject';
const Type = new GraphQLInputObjectType({
name: 'TradingSystemInput',
description: 'Payload for trading system input',
fields: () => ({
data: { type: GraphQLJSON },
}),
});
export default Type;
@@ -0,0 +1,6 @@
import TradingSystemInputType from './TradingSystem';
export {
// eslint-disable-next-line import/prefer-default-export
TradingSystemInputType,
};
@@ -0,0 +1,6 @@
/* eslint-disable max-len */
const tradingSystem = {
data: {},
};
export default tradingSystem;
Whitespace-only changes.