ChilliCream / ChilliCream/graphql-platform

IHttpClientFactory returns the wrong type, making it impossible to implement a custom IHttpConnection

Open
#6,647 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🌶️ strawberry shake
Dominant language
C#
Stars
5.8k
Forks
810
Avg merge
15h 39m
Merged PRs (30d)
98

Description

Is there an existing issue for this?
  • I have searched the existing issues
Product

Strawberry Shake

Describe the bug

Presumably, the goal of exposing a DI version of IHttpConnection is to allow the end-user to plug the connection with a different implementation. In my case, System.Net.HttpClient is totally incompatible with my deployment. This should be the perfect use case for IHttpConnection, so I can plug it with a compatible implementation.

Unfortunately, even if a different connection is provided, Strawberry Shake still attempts to create its own HTTP client:

AddTransient<StrawberryShake.Transport.Http.IHttpConnection, MyHttpConnection>()

Regardless of if I use Singleton, Scoped, or Transient... the MyHttpConnection is never used. Strawberry Shake appears to load up the IHttpClientFactory instance and create a new client for each request instead.

This should be easy to fix on my side, except the IHttpClientFactory is incompatible with IHttpConnection. The Factory is obligated to return a StrawberryShake.Transport.Http.HttpConnection connection, instead of a IHttpConnection:

HttpClient CreateClient(string name);

Thus, the StrawberryShake.Transport.Http library is strongly coupled to the system's implementation of System.Net.Http.HttpClient... which is in turn coupled to System.Threading, meaning that the library cannot be used at all in environments where this is unavailable (i.e., Unity WebGL).

All it would take to fix this is to change the return type to IHttpConnection. This is already what the code generators do, coercing the HttpClient into the interface type, so it appears the current implementation is unnecessary.

Steps to reproduce

Try to use a IHttpConnection that is not derived from `

Relevant log output

No response

Additional Context?

I was able to create a very hacky solution by rewriting the generated code from StrawberryShake, but it's quite annoying since I'll need to do it every time the code is generated:

global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Transport.Http.IHttpConnection>(services, sp =>
            {
                return new MyHttpConnection();
            });
Version

13.6.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Strawberry Shake's IHttpClientFactory and IHttpConnection integration, including the generated service-registration code shown in the issue. Verify why a registered custom IHttpConnection is ignored and where the factory's concrete HttpConnection type is required. Done means a custom IHttpConnection can be used without creating the system HttpClient implementation, including in the described Unity WebGL environment.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.