MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeHttpWith

Function makeHttpWith

packages/effect/src/graphql/GraphQLProtocol.ts:354–404  ·  view source on GitHub ↗
(options: HttpOptions<R>)

Source from the content-addressed store, hash-verified

352}
353
354const 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
406const urlResolver = (url: Url): NonNullable<GraphQLProtocol["Service"]["url"]> => {
407 if (typeof url === "string") return constant(Effect.succeed(url))

Callers 1

layerHttpFunction · 0.85

Calls 4

urlResolverFunction · 0.85
useSyncMethod · 0.80
makeWebSocketFunction · 0.70
ofMethod · 0.65

Tested by

no test coverage detected