@@ -53,8 +53,11 @@ as defined in the [apollographql/subscriptions-transport-ws](https://github.com/
5353and [ enisdenjo/graphql-ws] ( https://github.com/enisdenjo/graphql-ws ) repositories, respectively.
5454
5555The middleware can be configured through the ` IApplicationBuilder ` or ` IEndpointRouteBuilder `
56- builder interfaces. In addition, an ` ExecutionResultActionResult ` class is added for returning
57- ` ExecutionResult ` instances directly from a controller action.
56+ builder interfaces. Alternatively, route handlers (such as ` MapGet ` and ` MapPost ` ) can return
57+ a ` GraphQLExecutionHttpResult ` for direct GraphQL execution, or ` ExecutionResultHttpResult ` for
58+ returning pre-executed GraphQL responses. Similarly, ` GraphQLExecutionActionResult `
59+ and ` ExecutionResultActionResult ` classes can be used to return GraphQL responses from
60+ controller actions.
5861
5962Authorization is also supported with the included ` AuthorizationValidationRule ` . It will
6063scan GraphQL documents and validate that the schema and all referenced output graph types, fields of
@@ -211,6 +214,50 @@ app.UseEndpoints(endpoints =>
211214await app .RunAsync ();
212215```
213216
217+ ### Configuration with route handlers (.NET 6+)
218+
219+ Although not recommended, you may set up [ route handlers] ( https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/route-handlers )
220+ to execute GraphQL requests using ` MapGet ` and ` MapPost ` that return an ` IResult ` .
221+ You will not need ` UseGraphQL ` or ` MapGraphQL ` in the application startup. Note that GET must be
222+ mapped to support WebSocket connections, as WebSocket connections upgrade from HTTP GET requests.
223+
224+ #### Using ` GraphQLExecutionHttpResult `
225+
226+ ``` csharp
227+ var app = builder .Build ();
228+ app .UseDeveloperExceptionPage ();
229+ app .UseWebSockets ();
230+
231+ // configure the graphql endpoint at "/graphql", using GraphQLExecutionHttpResult
232+ // map GET in order to support both GET and WebSocket requests
233+ app .MapGet (" /graphql" , () => new GraphQLExecutionHttpResult ());
234+ // map POST to handle standard GraphQL POST requests
235+ app .MapPost (" /graphql" , () => new GraphQLExecutionHttpResult ());
236+
237+ await app .RunAsync ();
238+ ```
239+
240+ #### Using ` ExecutionResultHttpResult `
241+
242+ ``` csharp
243+ app .MapPost (" /graphql" , async (HttpContext context , IDocumentExecuter <ISchema > documentExecuter , IGraphQLSerializer serializer ) =>
244+ {
245+ var request = await serializer .ReadAsync <GraphQLRequest >(context .Request .Body , context .RequestAborted );
246+ var opts = new ExecutionOptions
247+ {
248+ Query = request ? .Query ,
249+ DocumentId = request ? .DocumentId ,
250+ Variables = request ? .Variables ,
251+ Extensions = request ? .Extensions ,
252+ CancellationToken = context .RequestAborted ,
253+ RequestServices = context .RequestServices ,
254+ User = context .User ,
255+ };
256+
257+ return new ExecutionResultHttpResult (await documentExecuter .ExecuteAsync (opts ));
258+ });
259+ ```
260+
214261### Configuration with a MVC controller
215262
216263Although not recommended, you may set up a controller action to execute GraphQL
@@ -1136,6 +1183,7 @@ typical ASP.NET Core scenarios.
11361183| Controller | .NET 8 Minimal | MVC implementation ; does not include WebSocket support |
11371184| Cors | .NET 8 Minimal | Demonstrates configuring a GraphQL endpoint to use a specified CORS policy |
11381185| EndpointRouting | .NET 8 Minimal | Demonstrates configuring GraphQL through endpoint routing |
1186+ | HttpResult | .NET 8 Minimal | Demonstrates using `MapGet ` and / or `MapPost ` to return a GraphQL response |
11391187| Jwt | .NET 8 Minimal | Demonstrates authenticating GraphQL requests with a JWT bearer token over HTTP POST and WebSocket connections |
11401188| MultipleSchemas | .NET 8 Minimal | Demonstrates configuring multiple schemas within a single server |
11411189| NativeAot | .NET 8 Slim | Demonstrates configuring GraphQL for Native AOT publishing |
0 commit comments