Skip to content

Commit

Permalink
Update server setup in uni-info-watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Nov 14, 2022
1 parent c4e255c commit a3e39ce
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
7 changes: 7 additions & 0 deletions packages/uni-info-watcher/environments/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
# Boolean to load GQL query nested entity relations sequentially.
loadRelationsSequential = false

# Max GQL API requests to process simultaneously (defaults to 1).
maxSimultaneousRequests = 50

[server.gqlCache]
maxAge = 15
timeTravelMaxAge = 86400
Expand All @@ -48,6 +51,10 @@
logging = false
maxQueryExecutionTime = 100

[database.extra]
# maximum number of clients the pool should contain
max = 20

[upstream]
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
Expand Down
5 changes: 5 additions & 0 deletions packages/uni-info-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"private": true,
"dependencies": {
"@apollo/client": "^3.3.19",
"@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",
Expand All @@ -23,10 +25,12 @@
"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 @@ -64,6 +68,7 @@
"devDependencies": {
"@types/chance": "^1.1.2",
"@types/express": "^4.17.11",
"@types/ws": "^7.4.4",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"@uniswap/v3-core": "1.0.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/uni-info-watcher/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ export const createResolvers = async (indexer: Indexer, customIndexer: CustomInd
assert(info.fieldNodes[0].selectionSet);

// Set cache-control maxAge
const maxAge = lodash.isEmpty(block)
? indexer.serverConfig.gqlCache.maxAge
: indexer.serverConfig.gqlCache.timeTravelMaxAge;
info.cacheControl.setCacheHint({ maxAge });
if (indexer.serverConfig.gqlCache) {
const maxAge = lodash.isEmpty(block)
? indexer.serverConfig.gqlCache.maxAge
: indexer.serverConfig.gqlCache.timeTravelMaxAge;
info.cacheControl.setCacheHint({ maxAge });
}

return customIndexer.getPool(id, block, info.fieldNodes[0].selectionSet.selections);
},
Expand Down Expand Up @@ -229,10 +231,12 @@ export const createResolvers = async (indexer: Indexer, customIndexer: CustomInd
assert(info.fieldNodes[0].selectionSet);

// Set cache-control maxAge
const maxAge = lodash.isEmpty(block)
? indexer.serverConfig.gqlCache.maxAge
: indexer.serverConfig.gqlCache.timeTravelMaxAge;
info.cacheControl.setCacheHint({ maxAge });
if (indexer.serverConfig.gqlCache) {
const maxAge = lodash.isEmpty(block)
? indexer.serverConfig.gqlCache.maxAge
: indexer.serverConfig.gqlCache.timeTravelMaxAge;
info.cacheControl.setCacheHint({ maxAge });
}

return customIndexer.getEntities(
Pool,
Expand Down
43 changes: 34 additions & 9 deletions packages/uni-info-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ 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 yargs from 'yargs';
Expand Down Expand Up @@ -100,24 +104,45 @@ 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
const app: Application = express();
app.use(queue({ activeLimit: maxSimultaneousRequests || 1, queuedLimit: maxRequestQueueLimit || -1 }));
const httpServer = createServer(app);

const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [responseCachePlugin()]
// 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);

const server = new ApolloServer({
schema,
csrfPrevention: true,
cache: 'bounded',
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 });

const httpServer = createServer(app);
// TODO: Enable subscriptions for apollo server v3
// server.installSubscriptionHandlers(httpServer);

httpServer.listen(port, host, () => {
log(`Server is listening on host ${host} port ${port}`);
log(`Server is listening on ${host}:${port}${server.graphqlPath}`);
});

await startGQLMetricsServer(config);
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface CacheControlConfig {
}

export interface ServerConfig extends BaseServerConfig {
gqlCache: CacheControlConfig;
gqlCache?: CacheControlConfig;
}

export interface GQLMetricsConfig {
Expand Down
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@
"@graphql-tools/utils" "9.1.0"
tslib "^2.4.0"

"@graphql-tools/[email protected]":
version "8.3.12"
resolved "https://registry.yarnpkg.com/@graphql-tools/merge/-/merge-8.3.12.tgz#e3f2e5d8a7b34fb689cda66799d845cbc919e464"
integrity sha512-BFL8r4+FrqecPnIW0H8UJCBRQ4Y8Ep60aujw9c/sQuFmQTiqgWgpphswMGfaosP2zUinDE3ojU5wwcS2IJnumA==
dependencies:
"@graphql-tools/utils" "9.1.1"
tslib "^2.4.0"

"@graphql-tools/mock@^8.1.2":
version "8.7.11"
resolved "https://registry.yarnpkg.com/@graphql-tools/mock/-/mock-8.7.11.tgz#6471b4221c3d8e6a29517811dac6b785f849846e"
Expand Down Expand Up @@ -771,6 +779,16 @@
tslib "^2.4.0"
value-or-promise "1.0.11"

"@graphql-tools/schema@^9.0.10":
version "9.0.10"
resolved "https://registry.yarnpkg.com/@graphql-tools/schema/-/schema-9.0.10.tgz#77ba3dfab241f0232dc0d3ba03201663b63714e2"
integrity sha512-lV0o4df9SpPiaeeDAzgdCJ2o2N9Wvsp0SMHlF2qDbh9aFCFQRsXuksgiDm2yTgT3TG5OtUes/t0D6uPjPZFUbQ==
dependencies:
"@graphql-tools/merge" "8.3.12"
"@graphql-tools/utils" "9.1.1"
tslib "^2.4.0"
value-or-promise "1.0.11"

"@graphql-tools/[email protected]":
version "8.9.0"
resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-8.9.0.tgz#c6aa5f651c9c99e1aca55510af21b56ec296cdb7"
Expand All @@ -785,6 +803,13 @@
dependencies:
tslib "^2.4.0"

"@graphql-tools/[email protected]":
version "9.1.1"
resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-9.1.1.tgz#b47ea8f0d18c038c5c1c429e72caa5c25039fbab"
integrity sha512-DXKLIEDbihK24fktR2hwp/BNIVwULIHaSTNTNhXS+19vgT50eX9wndx1bPxGwHnVBOONcwjXy0roQac49vdt/w==
dependencies:
tslib "^2.4.0"

"@graphql-typed-document-node/core@^3.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.0.tgz#0eee6373e11418bfe0b5638f654df7a4ca6a3950"
Expand Down Expand Up @@ -5645,6 +5670,11 @@ graphql-tools@^4.0.8:
iterall "^1.1.3"
uuid "^3.1.0"

graphql-ws@^5.11.2:
version "5.11.2"
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.11.2.tgz#d5e0acae8b4d4a4cf7be410a24135cfcefd7ddc0"
integrity sha512-4EiZ3/UXYcjm+xFGP544/yW1+DVI8ZpKASFbzrV5EDTFWJp0ZvLl4Dy2fSZAzz9imKp5pZMIcjB0x/H69Pv/6w==

graphql@^14.0.2:
version "14.7.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.7.0.tgz#7fa79a80a69be4a31c27dda824dc04dac2035a72"
Expand Down

0 comments on commit a3e39ce

Please sign in to comment.