Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IHttpConnectionFactory for non-HttpClient support #6688

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

zaneclaes
Copy link

@zaneclaes zaneclaes commented Nov 10, 2023

  • Create IHttpConnectionFactory that returns IHttpConnection
  • Implement DefaultHttpConnectionFactory
  • Change code generation to use new factory

Closes #6647

I started by trying to modify IHttpClientFactory, but quickly realized it is deeply entangled with the Microsoft implementation thereof. Instead, I opted to implement a new factory for the higher-order IHttpConnection. However, this necessitated changing some dependencies, especially notably I had to add Microsoft.Extensions.Http to the StrawberryShake Tools package (assuming we want to colocate DefaultHttpConnectionFactory with DefaultHttpClientFactory, that is).

Finally, I'm not sure if I've correctly injected the DefaultHttpConnectionFactory into the DI. I would expect that StrawberryShake should automatically provide this feature for clients so that it does not break any existing clients when new code is generated. However, I'm not sure where that's currently being done for DefaultHttpClientFactory (I only see it referenced in CommandTools.cs, so I'm not sure how it's being injected by default for clients) @michaelstaib

@CLAassistant
Copy link

CLAassistant commented Nov 10, 2023

CLA assistant check
All committers have signed the CLA.

Copy link

codecov bot commented Nov 10, 2023

Codecov Report

Attention: 12 lines in your changes are missing coverage. Please review.

Comparison is base (6965960) 78.98% compared to head (5f291ca) 78.16%.
Report is 72 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6688      +/-   ##
==========================================
- Coverage   78.98%   78.16%   -0.82%     
==========================================
  Files        2903     2541     -362     
  Lines      139771   127135   -12636     
==========================================
- Hits       110397    99379   -11018     
+ Misses      29374    27756    -1618     
Flag Coverage Δ
unittests 78.16% <85.18%> (-0.82%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...e/src/CookieCrumble/Extensions/WriterExtensions.cs 100.00% <ø> (ø)
...CookieCrumble/src/CookieCrumble/TestEnvironment.cs 0.00% <ø> (ø)
...polloFederationRequestExecutorBuilderExtensions.cs 66.66% <100.00%> (ø)
...ensions/ApolloFederationSchemaBuilderExtensions.cs 86.66% <ø> (ø)
.../src/ApolloFederation/FederationTypeInterceptor.cs 100.00% <100.00%> (ø)
...ion/src/ApolloFederation/Helpers/ArgumentParser.cs 68.65% <100.00%> (+0.47%) ⬆️
...rc/AspNetCore.CommandLine/Command/ExportCommand.cs 100.00% <100.00%> (ø)
...tChocolateAspNetCoreServiceCollectionExtensions.cs 93.33% <ø> (+26.66%) ⬆️
...rc/AspNetCore/Extensions/HttpResponseExtensions.cs 26.31% <ø> (+4.57%) ⬆️
...pNetCore/src/AspNetCore/GraphQLRequestException.cs 25.00% <ø> (+5.00%) ⬆️
... and 14 more

... and 878 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zaneclaes
Copy link
Author

In the last push, I was able to successfully test code generation. Unfortunately, I am struggling to run the tests locally. The codegen tests always fail, even on a fresh checkout (maybe I missed a step?) I believe because the DEBUG flag is not set in the project run configuration, meaning that the comments in the code are not generated and the output does not match the stubbed output:
Screenshot 2023-11-10 at 9 42 01 AM

@zaneclaes
Copy link
Author

Last thought: if preferred, we could omit the new classes and simply change the code generator to add the 2 lines at the top of the client generation function:

            global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.IHttpConnection>(services, sp => {
                global::StrawberryShake.IHttpConnection? conn = parentServices.GetService<global::StrawberryShake.IHttpConnection>();
                if (conn != null) return conn;
                var clientFactory = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::System.Net.Http.IHttpClientFactory>(parentServices);
                return new global::StrawberryShake.Transport.Http.HttpConnection(() => clientFactory.CreateClient("TuneQuery"));
            });

@michaelstaib michaelstaib marked this pull request as draft December 13, 2023 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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