@@ -72,7 +72,7 @@ A delegate that resolves the DbContext.
7272``` cs
7373namespace GraphQL .EntityFramework ;
7474
75- public delegate TDbContext ResolveDbContext <out TDbContext >(object userContext )
75+ public delegate TDbContext ResolveDbContext <out TDbContext >(object userContext , IServiceProvider ? requestServices )
7676 where TDbContext : DbContext ;
7777```
7878<sup ><a href =' /src/GraphQL.EntityFramework/GraphApi/ResolveDbContext.cs#L1-L4 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-ResolveDbContext.cs ' title =' Start of snippet ' >anchor</a ></sup >
@@ -298,23 +298,6 @@ public class GraphQlController(ISchema schema, IDocumentExecuter executer) :
298298Multiple different DbContext types can be registered and used.
299299
300300
301- ### UserContext
302-
303- A user context that exposes both types.
304-
305- <!-- snippet: MultiUserContext -->
306- <a id =' snippet-MultiUserContext ' ></a >
307- ``` cs
308- public class UserContext (DbContext1 context1 , DbContext2 context2 ) : Dictionary <string , object ?>
309- {
310- public readonly DbContext1 DbContext1 = context1 ;
311- public readonly DbContext2 DbContext2 = context2 ;
312- }
313- ```
314- <sup ><a href =' /src/Tests/MultiContextTests/MultiContextTests.cs#L80-L86 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-MultiUserContext ' title =' Start of snippet ' >anchor</a ></sup >
315- <!-- endSnippet -->
316-
317-
318301### Register in container
319302
320303Register both DbContext types in the container and include how those instance can be extracted from the GraphQL context:
@@ -324,10 +307,10 @@ Register both DbContext types in the container and include how those instance ca
324307``` cs
325308EfGraphQLConventions .RegisterInContainer (
326309 services ,
327- userContext => (( UserContext ) userContext ). DbContext1 );
310+ ( _ , requestServices ) => requestServices ! . GetRequiredService < DbContext1 >() );
328311EfGraphQLConventions .RegisterInContainer (
329312 services ,
330- userContext => (( UserContext ) userContext ). DbContext2 );
313+ ( _ , requestServices ) => requestServices ! . GetRequiredService < DbContext2 >() );
331314```
332315<sup ><a href =' /src/Tests/MultiContextTests/MultiContextTests.cs#L49-L58 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-RegisterMultipleInContainer ' title =' Start of snippet ' >anchor</a ></sup >
333316<!-- endSnippet -->
@@ -345,7 +328,7 @@ var executionOptions = new ExecutionOptions
345328{
346329 Schema = schema ,
347330 Query = query ,
348- UserContext = new UserContext ( dbContext1 , dbContext2 )
331+ RequestServices = provider ,
349332};
350333```
351334<sup ><a href =' /src/Tests/MultiContextTests/MultiContextTests.cs#L64-L73 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-MultiExecutionOptions ' title =' Start of snippet ' >anchor</a ></sup >
@@ -369,39 +352,23 @@ public class MultiContextQuery :
369352 efGraphQlService1 .AddSingleField (
370353 graph : this ,
371354 name : " entity1" ,
372- resolve : context =>
373- {
374- var userContext = (UserContext ) context .UserContext ;
375- return userContext .DbContext1 .Entities ;
376- });
355+ resolve : _ => _ .DbContext .Entities );
377356 efGraphQlService1 .AddFirstField (
378357 graph : this ,
379358 name : " entity1First" ,
380- resolve : context =>
381- {
382- var userContext = (UserContext ) context .UserContext ;
383- return userContext .DbContext1 .Entities ;
384- });
359+ resolve : _ => _ .DbContext .Entities );
385360 efGraphQlService2 .AddSingleField (
386361 graph : this ,
387362 name : " entity2" ,
388- resolve : context =>
389- {
390- var userContext = (UserContext ) context .UserContext ;
391- return userContext .DbContext2 .Entities ;
392- });
363+ resolve : _ => _ .DbContext .Entities );
393364 efGraphQlService2 .AddFirstField (
394365 graph : this ,
395366 name : " entity2First" ,
396- resolve : context =>
397- {
398- var userContext = (UserContext ) context .UserContext ;
399- return userContext .DbContext2 .Entities ;
400- });
367+ resolve : _ => _ .DbContext .Entities );
401368 }
402369}
403370```
404- <sup ><a href =' /src/Tests/MultiContextTests/MultiContextQuery.cs#L1-L41 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-MultiContextQuery.cs ' title =' Start of snippet ' >anchor</a ></sup >
371+ <sup ><a href =' /src/Tests/MultiContextTests/MultiContextQuery.cs#L1-L25 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-MultiContextQuery.cs ' title =' Start of snippet ' >anchor</a ></sup >
405372<!-- endSnippet -->
406373
407374
0 commit comments