Add gqlPath in config for GQL server URL (#483)

This commit is contained in:
Nabarun Gogoi 2023-11-21 15:21:10 +05:30 committed by GitHub
parent 4a88ff76e0
commit 8c44fbbe6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -2,6 +2,7 @@
host = "127.0.0.1" host = "127.0.0.1"
port = {{port}} port = {{port}}
kind = "{{watcherKind}}" kind = "{{watcherKind}}"
gqlPath = "/graphql"
# Checkpointing state. # Checkpointing state.
checkpointing = true checkpointing = true

View File

@ -200,6 +200,7 @@ export interface ServerConfig {
host: string; host: string;
port: number; port: number;
mode: string; mode: string;
gqlPath: string;
kind: string; kind: string;
enableConfigValidation: boolean; enableConfigValidation: boolean;
checkpointing: boolean; checkpointing: boolean;

View File

@ -3,7 +3,10 @@ import { ApolloServer } from 'apollo-server-express';
import { createServer } from 'http'; import { createServer } from 'http';
import { WebSocketServer } from 'ws'; import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws'; import { useServer } from 'graphql-ws/lib/use/ws';
import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core'; import {
ApolloServerPluginDrainHttpServer,
ApolloServerPluginLandingPageLocalDefault
} from 'apollo-server-core';
import debug from 'debug'; import debug from 'debug';
import responseCachePlugin from 'apollo-server-plugin-response-cache'; import responseCachePlugin from 'apollo-server-plugin-response-cache';
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'; import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
@ -18,6 +21,8 @@ import { PaymentsManager, paymentsPlugin } from './payments';
const log = debug('vulcanize:server'); const log = debug('vulcanize:server');
const DEFAULT_GQL_PATH = '/graphql';
export const createAndStartServer = async ( export const createAndStartServer = async (
app: Application, app: Application,
typeDefs: TypeSource, typeDefs: TypeSource,
@ -25,7 +30,14 @@ export const createAndStartServer = async (
serverConfig: ServerConfig, serverConfig: ServerConfig,
paymentsManager?: PaymentsManager paymentsManager?: PaymentsManager
): Promise<ApolloServer> => { ): Promise<ApolloServer> => {
const { host, port, gqlCache: gqlCacheConfig, maxSimultaneousRequests, maxRequestQueueLimit } = serverConfig; const {
host,
port,
gqlCache: gqlCacheConfig,
maxSimultaneousRequests,
maxRequestQueueLimit,
gqlPath = DEFAULT_GQL_PATH
} = serverConfig;
app.use(queue({ activeLimit: maxSimultaneousRequests || 1, queuedLimit: maxRequestQueueLimit || -1 })); app.use(queue({ activeLimit: maxSimultaneousRequests || 1, queuedLimit: maxRequestQueueLimit || -1 }));
@ -38,7 +50,7 @@ export const createAndStartServer = async (
// Create our WebSocket server using the HTTP server we just set up. // Create our WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocketServer({ const wsServer = new WebSocketServer({
server: httpServer, server: httpServer,
path: '/graphql' path: gqlPath
}); });
const serverCleanup = useServer({ schema }, wsServer); const serverCleanup = useServer({ schema }, wsServer);
@ -73,8 +85,13 @@ export const createAndStartServer = async (
ApolloServerPluginLandingPageLocalDefault({ embed: true }) ApolloServerPluginLandingPageLocalDefault({ embed: true })
] ]
}); });
await server.start(); await server.start();
server.applyMiddleware({ app });
server.applyMiddleware({
app,
path: gqlPath
});
httpServer.listen(port, host, () => { httpServer.listen(port, host, () => {
log(`Server is listening on ${host}:${port}${server.graphqlPath}`); log(`Server is listening on ${host}:${port}${server.graphqlPath}`);