()
| 156 | } |
| 157 | |
| 158 | async start() { |
| 159 | const schema = await this._setupSchema(); |
| 160 | this.schema = schema; |
| 161 | // Allow a graphql context resolver to be bound to GRAPHQL_CONTEXT_RESOLVER |
| 162 | const graphqlContextResolver: ExpressMiddlewareOptions<BaseContext>['context'] = |
| 163 | (await this.get(GraphQLBindings.GRAPHQL_CONTEXT_RESOLVER, { |
| 164 | optional: true, |
| 165 | })) ?? (async context => context); |
| 166 | // Allow a graphql context resolver to be bound to GRAPHQL_CONTEXT_RESOLVER |
| 167 | const graphqlWsContextResolver = |
| 168 | (await this.get(GraphQLBindings.GRAPHQL_WS_CONTEXT_RESOLVER, { |
| 169 | optional: true, |
| 170 | })) ?? |
| 171 | (async (context: object) => { |
| 172 | return context; |
| 173 | }); |
| 174 | |
| 175 | // Create ApolloServerExpress GraphQL server |
| 176 | const serverConfig = { |
| 177 | ...this.options.apollo, |
| 178 | schema, |
| 179 | // plugins: [ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer })], |
| 180 | } as ApolloServerOptionsWithSchema<BaseContext>; |
| 181 | const graphQLServer = new ApolloServer(serverConfig); |
| 182 | await graphQLServer.start(); |
| 183 | this.expressApp.use( |
| 184 | this.options.graphQLPath ?? '/', |
| 185 | cors<cors.CorsRequest>(), |
| 186 | json(), |
| 187 | expressMiddleware(graphQLServer, { |
| 188 | context: graphqlContextResolver, |
| 189 | ...this.options.middlewareOptions, |
| 190 | }), |
| 191 | ); |
| 192 | // Start the http server if created |
| 193 | await this.httpServer?.start(); |
| 194 | |
| 195 | let server; |
| 196 | if (this.options.asMiddlewareOnly) { |
| 197 | const rest: RestServer = await this.get(RestBindings.SERVER); |
| 198 | server = rest.httpServer?.server; |
| 199 | } else { |
| 200 | server = this.httpServer?.server; |
| 201 | } |
| 202 | const wsServer = new WebSocketServer({ |
| 203 | server: server, |
| 204 | path: this.options.path, |
| 205 | }); |
| 206 | this.wsServer = wsServer; |
| 207 | useServer( |
| 208 | { |
| 209 | schema, |
| 210 | context: graphqlWsContextResolver, |
| 211 | }, |
| 212 | wsServer, |
| 213 | ); |
| 214 | } |
| 215 |
nothing calls this directly
no test coverage detected