In my ASP NET Core 6 api, we make a query with a query (SQL) using Dapper, typing a model and with the result return in some endpoint or use in some service, for example, a service that generates a PDF report and returns in a File on endpoint
In the case of the query to retrieve the data needed to generate the PDF, how would I do it using GraphQL installed in the application (HotChocolate)?
In short, how to use GraphQL from the application to the application itself?
Question "In short, how to use GraphQL from the application to the application itself?" can be understood in two ways.
If you want to directly execute a query you may use IRequestExecutor
. If you look at HttpGetMiddleware
you can see that this is how queries are executed. To get IRequestExecutor
take IRequestExecutorResolver
from the DI.
// From DI
IRequestExecutorResolver resolver = ...;
// See next snippet
IQueryRequest request = ...;
IRequestExecutor executor = await resolver.GetRequestExecutorAsync();
IExecutionResult result = await executor.ExecuteAsync(request);
Type IQueryRequest
represents a GraphQL request. In can be created using IQueryRequestBuilder
. If your resolvers user "special" dependencies you have to explicitly specify them shen creating the request. To check what counts as "special" dependenceis check DefaultHttpRequestInterceptor
. The same goes for if you are adding extra "special" dependencies in your own interceptor.
IQueryRequest request = new QueryRequestBuilder().SetQuery("query text as string").SetVariableValues(/* if your query needs varaibles */).Create();
StrawberryShake is a .NET GraphQL client made by ChilliCream. This will can generate you .NET types for your queries and invoke queries through network. Esentially you can query from the GraphQL server on it's endpoint as any other public client even if you are doing it from the same process. I won't elaborate on this as the link I included shows you basic example (whereas directly using IRequestExecutor
is not described in docs).