Skip to content

Commit

Permalink
Reuse code to create server from watcher-ts (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 authored Nov 17, 2022
1 parent ef806d9 commit 5adc7ff
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 483 deletions.
5 changes: 2 additions & 3 deletions packages/erc20-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@
"@vulcanize/cache": "^0.1.0",
"@vulcanize/ipld-eth-client": "^0.1.0",
"@vulcanize/util": "^0.1.0",
"apollo-server-express": "^2.25.0",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",
"ethers": "^5.2.0",
"express": "^4.17.1",
"express": "^4.18.2",
"graphql-import-node": "^0.0.4",
"graphql-request": "^3.4.0",
"graphql-subscriptions": "^2.0.0",
Expand All @@ -65,7 +64,7 @@
"@ethersproject/abi": "^5.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/express": "^4.17.11",
"@types/express": "^4.17.14",
"@types/json-bigint": "^1.0.0",
"@types/yargs": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.25.0",
Expand Down
23 changes: 5 additions & 18 deletions packages/erc20-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import assert from 'assert';
import 'reflect-metadata';
import express, { Application } from 'express';
import { ApolloServer } from 'apollo-server-express';
import { PubSub } from 'graphql-subscriptions';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import debug from 'debug';
import 'graphql-import-node';
import { createServer } from 'http';

import { getCache } from '@vulcanize/cache';
import {
KIND_ACTIVE,
DEFAULT_CONFIG_PATH
DEFAULT_CONFIG_PATH,
createAndStartServer
} from '@cerc-io/util';
import { getConfig, getCustomProvider, JobQueue, startGQLMetricsServer } from '@vulcanize/util';
import { EthClient } from '@cerc-io/ipld-eth-client';
Expand Down Expand Up @@ -46,7 +45,7 @@ export const main = async (): Promise<any> => {

assert(config.server, 'Missing server config');

const { host, port, kind: watcherKind } = config.server;
const { kind: watcherKind } = config.server;

const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config;

Expand Down Expand Up @@ -91,21 +90,9 @@ export const main = async (): Promise<any> => {

const resolvers = process.env.MOCK ? await createMockResolvers() : await createResolvers(indexer, eventWatcher);

// Create an Express app and server
const app: Application = express();
const server = new ApolloServer({
typeDefs,
resolvers
});

await server.start();
server.applyMiddleware({ app });

const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(port, host, () => {
log(`Server is listening on host ${host} port ${port}`);
});
const server = createAndStartServer(app, typeDefs, resolvers, config.server);

await startGQLMetricsServer(config);

Expand Down
13 changes: 2 additions & 11 deletions packages/uni-info-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@
"private": true,
"dependencies": {
"@apollo/client": "^3.3.19",
"@apollo/utils.keyvaluecache": "^1.0.1",
"@graphql-tools/schema": "^9.0.10",
"@ipld/dag-cbor": "^6.0.12",
"@types/lodash": "^4.14.168",
"@vulcanize/cache": "^0.1.0",
"@vulcanize/erc20-watcher": "^0.1.0",
"@vulcanize/ipld-eth-client": "^0.1.0",
"@vulcanize/uni-watcher": "^0.1.0",
"@vulcanize/util": "^0.1.0",
"apollo-server-core": "^3.11.1",
"apollo-server-express": "^3.11.1",
"apollo-server-plugin-response-cache": "^3.8.1",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",
"express": "^4.17.1",
"express-queue": "^0.0.13",
"express": "^4.18.2",
"graphql": "^15.5.0",
"graphql-import-node": "^0.0.4",
"graphql-request": "^3.4.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.11.2",
"json-bigint": "^1.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.32",
"ws": "^7.4.6",
"yargs": "^17.0.1"
},
"scripts": {
Expand Down Expand Up @@ -68,8 +60,7 @@
},
"devDependencies": {
"@types/chance": "^1.1.2",
"@types/express": "^4.17.11",
"@types/ws": "^7.4.4",
"@types/express": "^4.17.14",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"@uniswap/v3-core": "1.0.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/uni-info-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import assert from 'assert';
import debug from 'debug';
import { DeepPartial, FindConditions, FindManyOptions, FindOneOptions, LessThan, LessThanOrEqual, MoreThan, QueryRunner } from 'typeorm';
import { DeepPartial, FindConditions, FindManyOptions, FindOneOptions, LessThan, MoreThan, QueryRunner } from 'typeorm';
import JSONbig from 'json-bigint';
import { providers, utils, BigNumber } from 'ethers';
import { SelectionNode } from 'graphql';
Expand Down Expand Up @@ -514,8 +514,7 @@ export class Indexer implements IndexerInterface {
async pruneFrothyEntities (blockNumber: number): Promise<void> {
const dbTx = await this._db.createTransactionRunner();
try {
// Remove frothy entity entries at | below the prune block height
await this._db.removeEntities(dbTx, FrothyEntity, { where: { blockNumber: LessThanOrEqual(blockNumber) } });
await this._db.graphDatabase.pruneFrothyEntities(dbTx, FrothyEntity, blockNumber);

dbTx.commitTransaction();
} catch (error) {
Expand Down
60 changes: 3 additions & 57 deletions packages/uni-info-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@
import assert from 'assert';
import 'reflect-metadata';
import express, { Application } from 'express';
import { ApolloServer } from 'apollo-server-express';
import WebSocket from 'ws';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { useServer } from 'graphql-ws/lib/use/ws';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import { PubSub } from 'graphql-subscriptions';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import debug from 'debug';
import 'graphql-import-node';
import { createServer } from 'http';
import queue from 'express-queue';

import { Client as ERC20Client } from '@vulcanize/erc20-watcher';
import { Client as UniClient } from '@vulcanize/uni-watcher';
import { DEFAULT_CONFIG_PATH, DEFAULT_MAX_GQL_CACHE_SIZE } from '@cerc-io/util';
import { createAndStartServer, DEFAULT_CONFIG_PATH } from '@cerc-io/util';

import { getConfig, getCustomProvider, JobQueue, startGQLMetricsServer } from '@vulcanize/util';
import { getCache } from '@vulcanize/cache';
Expand Down Expand Up @@ -54,8 +45,6 @@ export const main = async (): Promise<any> => {

assert(config.server, 'Missing server config');

const { host, port, maxSimultaneousRequests, maxRequestQueueLimit, gqlCache: gqlCacheConfig } = config.server;

const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config;

assert(dbConfig, 'Missing database config');
Expand Down Expand Up @@ -105,52 +94,9 @@ export const main = async (): Promise<any> => {
const customIndexer = new CustomIndexer(config, db, indexer);
const resolvers = process.env.MOCK ? await createMockResolvers() : await createResolvers(indexer, customIndexer, eventWatcher);

// Create an Express app and HTTP server
// Create an Express app and server
const app: Application = express();
app.use(queue({ activeLimit: maxSimultaneousRequests || 1, queuedLimit: maxRequestQueueLimit || -1 }));
const httpServer = createServer(app);

// Create the schema
const schema = makeExecutableSchema({ typeDefs, resolvers });

// Create our WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocket.Server({
server: httpServer,
path: '/graphql'
});
const serverCleanup = useServer({ schema }, wsServer);

let gqlCache;
if (gqlCacheConfig && gqlCacheConfig.enabled) {
const maxSize = gqlCacheConfig.maxCacheSize ? gqlCacheConfig.maxCacheSize : DEFAULT_MAX_GQL_CACHE_SIZE;
gqlCache = new InMemoryLRUCache({ maxSize });
}

const server = new ApolloServer({
schema,
csrfPrevention: true,
cache: gqlCache,
plugins: [
// Proper shutdown for the HTTP server
ApolloServerPluginDrainHttpServer({ httpServer }),
// Proper shutdown for the WebSocket server
{
async serverWillStart () {
return {
async drainServer () {
await serverCleanup.dispose();
}
};
}
},
responseCachePlugin()]
});
await server.start();
server.applyMiddleware({ app });

httpServer.listen(port, host, () => {
log(`Server is listening on ${host}:${port}${server.graphqlPath}`);
});
const server = createAndStartServer(app, typeDefs, resolvers, config.server);

await startGQLMetricsServer(config);

Expand Down
2 changes: 0 additions & 2 deletions packages/uni-info-watcher/src/types/common/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@

// https://medium.com/@steveruiz/using-a-javascript-library-without-type-declarations-in-a-typescript-project-3643490015f3
declare module 'canonical-json'

declare module 'express-queue';
5 changes: 2 additions & 3 deletions packages/uni-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@
"@vulcanize/cache": "^0.1.0",
"@vulcanize/ipld-eth-client": "^0.1.0",
"@vulcanize/util": "^0.1.0",
"apollo-server-express": "^2.25.0",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",
"ethers": "^5.2.0",
"express": "^4.17.1",
"express": "^4.18.2",
"graphql-import-node": "^0.0.4",
"graphql-request": "^3.4.0",
"graphql-subscriptions": "^2.0.0",
Expand All @@ -58,7 +57,7 @@
},
"devDependencies": {
"@types/chai": "^4.2.18",
"@types/express": "^4.17.11",
"@types/express": "^4.17.14",
"@types/json-bigint": "^1.0.0",
"@types/mocha": "^8.2.2",
"@types/yargs": "^17.0.0",
Expand Down
22 changes: 3 additions & 19 deletions packages/uni-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import assert from 'assert';
import 'reflect-metadata';
import express, { Application } from 'express';
import { ApolloServer } from 'apollo-server-express';
import { PubSub } from 'graphql-subscriptions';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import debug from 'debug';
import 'graphql-import-node';
import { createServer } from 'http';

import { EthClient } from '@cerc-io/ipld-eth-client';
import { getCache } from '@vulcanize/cache';
import { DEFAULT_CONFIG_PATH } from '@cerc-io/util';
import { createAndStartServer, DEFAULT_CONFIG_PATH } from '@cerc-io/util';
import { getConfig, getCustomProvider, JobQueue, startGQLMetricsServer } from '@vulcanize/util';

import typeDefs from './schema';
Expand Down Expand Up @@ -43,8 +41,6 @@ export const main = async (): Promise<any> => {

assert(config.server, 'Missing server config');

const { host, port } = config.server;

const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config;

assert(dbConfig, 'Missing database config');
Expand Down Expand Up @@ -86,21 +82,9 @@ export const main = async (): Promise<any> => {

const resolvers = process.env.MOCK ? await createMockResolvers() : await createResolvers(indexer, eventWatcher);

// Create an Express app and server
const app: Application = express();
const server = new ApolloServer({
typeDefs,
resolvers
});

await server.start();
server.applyMiddleware({ app });

const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(port, host, () => {
log(`Server is listening on host ${host} port ${port}`);
});
const server = createAndStartServer(app, typeDefs, resolvers, config.server);

await startGQLMetricsServer(config);

Expand Down
Loading

0 comments on commit 5adc7ff

Please sign in to comment.