ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.26k stars 745 forks source link

Stitching having issues when using the new .NET 8 IKeyedServiceProvider #6536

Closed repne closed 1 year ago

repne commented 1 year ago

Is there an existing issue for this?

Product

Hot Chocolate

Describe the bug

When using the new .NET 8 IKeyedServiceProvider (.AddKeyedSingleton, .AddKeyedScoped, etc...) AddLocalSchema and AddRemoteSchema will throw an exception.

Steps to reproduce

This

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGraphQLServer("local");

builder.Services.AddKeyedSingleton("key", new { foo = 42 });

builder.Services
    .AddGraphQLServer()
    .AddLocalSchema("local");

await using var app = builder.Build();

will throw

Unhandled exception. System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor()
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationType()
   at Microsoft.Extensions.DependencyInjection.HotChocolateStitchingRequestExecutorExtensions.<>c.<AddLocalSchema>b__6_2(ServiceDescriptor t)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.Extensions.DependencyInjection.HotChocolateStitchingRequestExecutorExtensions.AddLocalSchema(IRequestExecutorBuilder builder, String schemaName, Boolean ignoreRootTypes)
   at Program.<Main>$(String[] args) in FOOOOO/Program.cs:line 7
   at Program.<Main>(String[] args)

but moving AddKeyedSingleton after the AddLocalSchema call

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGraphQLServer("local");

builder.Services
    .AddGraphQLServer()
    .AddLocalSchema("local");

builder.Services.AddKeyedSingleton("key", new { foo = 42 });

await using var app = builder.Build();

will work just fine.

Relevant log output

No response

Additional Context?

No response

Version

13.5.1

michaelstaib commented 1 year ago

We no longer support stitching. Its time to move to fusion :) https://chillicream.com/blog/2023/08/15/graphql-fusion

cambirch commented 1 year ago

Note... this issue isn't really due to stitching. v13 fails when resolving any service from the service provider that has a keyed version. This includes if you register a single keyed and resolve via IEnumerable<>. Keyed services in .NET 8 require an extra interface to be implemented on the scope otherwise they crash.

see: https://github.com/dotnet/runtime/issues/89447 for the same issue occurring in asp.net before release.