(options: HttpOptions<R>)
| 352 | } |
| 353 | |
| 354 | const makeHttpWith = <R>(options: HttpOptions<R>): Effect.Effect< |
| 355 | GraphQLProtocol["Service"], |
| 356 | never, |
| 357 | HttpClient.HttpClient | Socket.WebSocketConstructor | Scope.Scope | R |
| 358 | > => { |
| 359 | const resolve = urlResolver(options.url) |
| 360 | // A resolver batch carries the URL it was grouped by. |
| 361 | const urlOf = (request: GraphQLRequest) => |
| 362 | internalGraphQL.ResolvedUrl.use((resolved) => |
| 363 | resolved?.resolve === resolve ? Effect.succeed(resolved.url) : resolve(request) |
| 364 | ) |
| 365 | const http = Clock.clockWith((clock) => |
| 366 | HttpClient.HttpClient.useSync((client) => { |
| 367 | const execute = (request: GraphQLRequest) => |
| 368 | Effect.flatMap(Effect.zip(Effect.service(internalGraphQL.ForcePost), urlOf(request)), ([forcePost, url]) => { |
| 369 | const toRequest = options.queryMethod === "GET" && request.kind === "query" && !forcePost |
| 370 | ? getRequest |
| 371 | : postRequest |
| 372 | return client.execute( |
| 373 | toRequest(url, request, `${graphqlResponseContentType}, ${jsonContentType}`) |
| 374 | ).pipe( |
| 375 | Effect.flatMap(readAnswer), |
| 376 | fromHttpClientError(clock) |
| 377 | ) |
| 378 | }) |
| 379 | const subscribe = (request: GraphQLRequest) => |
| 380 | Stream.unwrap(Effect.flatMap(urlOf(request), (url) => |
| 381 | client.execute(postRequest(url, request, eventStreamContentType)).pipe( |
| 382 | Effect.flatMap((response) => { |
| 383 | const contentType = contentTypeOf(response) |
| 384 | if (contentType.includes(eventStreamContentType) && response.status >= 200 && response.status < 300) { |
| 385 | return Effect.succeed(sseEvents(response)) |
| 386 | } |
| 387 | // A server that rejects the subscription answers with a single |
| 388 | // GraphQL response, which the client reads like a query's. |
| 389 | return Effect.map(readAnswer(response), Stream.succeed) |
| 390 | }), |
| 391 | fromHttpClientError(clock) |
| 392 | ))) |
| 393 | return GraphQLProtocol.of({ execute, subscribe, url: resolve }) |
| 394 | }) |
| 395 | ) |
| 396 | const webSocket = options.subscriptions?.webSocket |
| 397 | return webSocket === undefined |
| 398 | ? http |
| 399 | : Effect.zipWith( |
| 400 | http, |
| 401 | makeWebSocket(webSocket), |
| 402 | (http, ws) => GraphQLProtocol.of({ execute: http.execute, subscribe: ws.subscribe, url: http.url }) |
| 403 | ) |
| 404 | } |
| 405 | |
| 406 | const urlResolver = (url: Url): NonNullable<GraphQLProtocol["Service"]["url"]> => { |
| 407 | if (typeof url === "string") return constant(Effect.succeed(url)) |
no test coverage detected