Skip to content

Commit

Permalink
server working
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranhydary committed Aug 31, 2024
1 parent 8e62387 commit 2ec0479
Show file tree
Hide file tree
Showing 21 changed files with 1,331 additions and 24 deletions.
4 changes: 4 additions & 0 deletions operator/@types/minipass-fetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'minipass-fetch' {
const miniPassfetch: typeof fetch
export default miniPassfetch
}
1 change: 1 addition & 0 deletions operator/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.log('Entering api.ts')
import { ApolloServer } from '@apollo/server'
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import { getSchema, GraphqlContext } from './core/graphql/schema'
Expand Down
5 changes: 4 additions & 1 deletion operator/core/graphql/extensions/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gql from 'graphql-tag'

export const typeDefs = gql``
export const typeDefs = gql`
type Query
type Mutation
`
43 changes: 40 additions & 3 deletions operator/core/graphql/extensions/orders/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
import { default as gql } from 'graphql-tag'

export const typeDefs = gql``
export const typeDefs = gql`
extend type Query {
orders: [Order]
}
interface OrdersResolvers {}
extend type Mutation {
createOrder(input: CreateOrderInput): Order
}
`

export const resolvers: OrdersResolvers = {}
interface OrdersResolvers {
Query: {
orders: () => Promise<any[]>
}
Mutation: {
createOrder: (_: any, { input }: { input: any }) => Promise<any>
}
}

export const resolvers: OrdersResolvers = {
Query: {
orders: async () => {
return [
{
id: '1',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
status: 'CREATED',
amount: 100,
},
]
},
},
Mutation: {
createOrder: async (_: any, { input }: { input: any }) => {
return {
id: '1',
...input,
}
},
},
}
3 changes: 2 additions & 1 deletion operator/core/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
typeDefs as ordersTypeDefs,
} from './extensions/orders'

import { merge } from 'lodash'
import pkg from 'lodash'
const { merge } = pkg

const resolvers = merge(ordersResolvers)

Expand Down
40 changes: 40 additions & 0 deletions operator/dist/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ApolloServer } from '@apollo/server';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { getSchema } from './core/graphql/schema';
import { app, httpServer } from './server';
import { serverConfig } from './config/server';
import bodyParser from 'body-parser';
import compression from 'compression';
import cookieParser from 'cookie-parser';
const schema = getSchema();
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({
schema,
introspection: true,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async serverWillStart() {
return {
async drainServer() {
console.log('Draining websocket server before shutdown');
try {
// TODO: Handle any subscriptions here
}
catch (e) {
console.error('Error draining websocket server / cancelling any subscriptions', e);
}
},
};
},
},
],
});
app.use(bodyParser.json());
app.use(compression());
app.use(cookieParser());
server.start().then(() => { });
httpServer.listen(serverConfig.port, () => {
console.log(`Milady Pool AVS API running on port: ${serverConfig.port}\n`);
});
Empty file added operator/dist/config/cors.js
Empty file.
3 changes: 3 additions & 0 deletions operator/dist/config/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const serverConfig = {
port: 8081,
};
2 changes: 2 additions & 0 deletions operator/dist/core/graphql/extensions/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import gql from 'graphql-tag';
export const typeDefs = gql ``;
3 changes: 3 additions & 0 deletions operator/dist/core/graphql/extensions/orders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { default as gql } from 'graphql-tag';
export const typeDefs = gql ``;
export const resolvers = {};
11 changes: 11 additions & 0 deletions operator/dist/core/graphql/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { stitchSchemas } from '@graphql-tools/stitch';
import { typeDefs as commonTypeDefs } from './extensions/common';
import { resolvers as ordersResolvers, typeDefs as ordersTypeDefs, } from './extensions/orders';
import { merge } from 'lodash';
const resolvers = merge(ordersResolvers);
export const getSchema = () => {
return stitchSchemas({
resolvers,
typeDefs: [commonTypeDefs, ordersTypeDefs],
});
};
4 changes: 4 additions & 0 deletions operator/dist/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import express from 'express';
import { createServer } from 'http';
export const app = express();
export const httpServer = createServer(app);
38 changes: 38 additions & 0 deletions operator/lib/axios/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios from 'axios'

import Agent from 'agentkeepalive'

export * from 'axios'

export const globalHttpsKeepAliveAgent = new Agent.HttpsAgent({
keepAlive: true,
maxSockets: 15,
maxFreeSockets: 15,
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
})
export const globalHttpKeepAliveAgent = new Agent({
keepAlive: true,
maxSockets: 15,
maxFreeSockets: 15,
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
})

export const axiosInstance = axios.create({
//keepAlive pools and reuses TCP connections, so it's faster
httpAgent: globalHttpKeepAliveAgent,
httpsAgent: globalHttpsKeepAliveAgent,
//follow up to 10 HTTP 3xx redirects
maxRedirects: 10,

//cap the maximum content length we'll accept to 50MBs, just in case
maxContentLength: 50 * 1000 * 1000,
})

axiosInstance.interceptors.request.use((request) => {
console.log(`[${request.method?.toUpperCase()}]: ${request.url}`)
return request
})

export default axiosInstance
6 changes: 6 additions & 0 deletions operator/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignore": ["node_modules"],
"watch": [".", ".env"],
"exec": "ts-node scripts/runSingleton.ts --project tsconfig.json",
"ext": "ts"
}
13 changes: 11 additions & 2 deletions operator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,37 @@
"license": "VPL-1.0",
"type": "module",
"scripts": {
"compile": "tsc",
"start": "yarn run compile && node .build/api.js"
"build": "rm -rf .build && tsc && yarn tsc-alias -p tsconfig.json && copyfiles -u 1 public/* .build/public && copyfiles **/*.graphql .build",
"dev": "nodemon"
},
"dependencies": {
"@apollo/server": "^4.11.0",
"@graphql-tools/stitch": "^9.0.1",
"agentkeepalive": "^4.5.0",
"axios": "^1.7.6",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"copyfiles": "^2.4.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"graphql": "^16.9.0",
"graphql-tag": "^2.12.6",
"http": "^0.0.1-security",
"lodash": "^4.17.21",
"minipass-fetch": "^3.0.5",
"nodemon": "^3.1.4",
"tsc-alias": "^1.8.10",
"tsconfig-paths": "^4.2.0",
"uuid": "^10.0.0"
},
"devDependencies": {
"@types/compression": "^1.7.5",
"@types/cookie-parser": "^1.4.7",
"@types/lodash": "^4.17.7",
"@types/node": "^22.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
}
2 changes: 2 additions & 0 deletions operator/scripts/runSingleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './setupEnv'
import '../api'
30 changes: 30 additions & 0 deletions operator/scripts/setupEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dotenv/config'
import '../utils/import-graphql'
import axios from 'axios'

import {
globalHttpsKeepAliveAgent,
globalHttpKeepAliveAgent,
} from '../lib/axios'

import fetch from 'minipass-fetch'
import http from 'http'
import https from 'https'

axios.defaults.httpAgent = globalHttpKeepAliveAgent
axios.defaults.httpsAgent = globalHttpsKeepAliveAgent
http.globalAgent = globalHttpKeepAliveAgent
https.globalAgent = globalHttpsKeepAliveAgent

// @ts-ignore
global.fetch = fetch

Object.assign(
BigInt.prototype,
'toJSON',
function (this: typeof BigInt.prototype) {
return this.toString()
}
)

// TODO: Need to add ws...
7 changes: 5 additions & 2 deletions operator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"ts-node": {
"esm": true,
"module": "CommonJS",
"experimentalSpecifierResolution": "node",
"require": ["tsconfig-paths/register"]
},
"compilerOptions": {
"target": "ES2020",
"lib": ["esnext"],
"module": "commonjs",
"module": "ESNext",
"rootDir": ".",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@/workers/*": ["workers/*"],
"@/test/*": ["test/*"],
"@uniswap/Permit2-sdk": ["permit2-sdk/src/index"],
"@/*": ["src/*"]
},
"typeRoots": ["./node_modules/@types", "./@types", "./@types/*"],
Expand Down
21 changes: 21 additions & 0 deletions operator/utils/import-graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parse } from 'graphql'
import { readFileSync } from 'fs'
import { createRequire } from 'module'

const require = createRequire(import.meta.url)

export const supportImportGraphQL = () => {
const VALID_EXTENSIONS = ['graphql', 'graphqls', 'gql', 'gqls']

function handleModule(m: typeof module, filename: string) {
const content = readFileSync(filename, 'utf-8')

m.exports = parse(content)
}

VALID_EXTENSIONS.forEach((ext) => {
require.extensions[`.${ext}`] = handleModule
})
}

supportImportGraphQL()
Loading

0 comments on commit 2ec0479

Please sign in to comment.