@@ -21,7 +21,7 @@ namespace GraphQL.Client.LocalExecution
2121 public static class GraphQLLocalExecutionClient
2222 {
2323 public static GraphQLLocalExecutionClient < TSchema > New < TSchema > ( TSchema schema , IGraphQLJsonSerializer serializer ) where TSchema : ISchema
24- => new GraphQLLocalExecutionClient < TSchema > ( schema , serializer ) ;
24+ => new GraphQLLocalExecutionClient < TSchema > ( schema , serializer , new SubscriptionDocumentExecuter ( ) , new DocumentWriter ( ) ) ;
2525 }
2626
2727 public class GraphQLLocalExecutionClient < TSchema > : IGraphQLClient where TSchema : ISchema
@@ -41,18 +41,18 @@ public class GraphQLLocalExecutionClient<TSchema> : IGraphQLClient where TSchema
4141
4242 public IGraphQLJsonSerializer Serializer { get ; }
4343
44- private readonly DocumentExecuter _documentExecuter ;
45- private readonly DocumentWriter _documentWriter ;
44+ private readonly IDocumentExecuter _documentExecuter ;
45+ private readonly IDocumentWriter _documentWriter ;
4646
47- public GraphQLLocalExecutionClient ( TSchema schema , IGraphQLJsonSerializer serializer )
47+ public GraphQLLocalExecutionClient ( TSchema schema , IGraphQLJsonSerializer serializer , IDocumentExecuter documentExecuter , IDocumentWriter documentWriter )
4848 {
4949 Schema = schema ?? throw new ArgumentNullException ( nameof ( schema ) , "no schema configured" ) ;
5050 Serializer = serializer ?? throw new ArgumentNullException ( nameof ( serializer ) , "please configure the JSON serializer you want to use" ) ;
5151
5252 if ( ! Schema . Initialized )
5353 Schema . Initialize ( ) ;
54- _documentExecuter = new DocumentExecuter ( ) ;
55- _documentWriter = new DocumentWriter ( ) ;
54+ _documentExecuter = documentExecuter ;
55+ _documentWriter = documentWriter ;
5656 }
5757
5858 public void Dispose ( ) { }
@@ -78,7 +78,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
7878 private async Task < GraphQLResponse < TResponse > > ExecuteQueryAsync < TResponse > ( GraphQLRequest request , CancellationToken cancellationToken )
7979 {
8080 var executionResult = await ExecuteAsync ( request , cancellationToken ) ;
81- return await ExecutionResultToGraphQLResponse < TResponse > ( executionResult , cancellationToken ) ;
81+ return await ExecutionResultToGraphQLResponseAsync < TResponse > ( executionResult , cancellationToken ) ;
8282 }
8383 private async Task < IObservable < GraphQLResponse < TResponse > > > ExecuteSubscriptionAsync < TResponse > ( GraphQLRequest request , CancellationToken cancellationToken = default )
8484 {
@@ -87,12 +87,12 @@ private async Task<IObservable<GraphQLResponse<TResponse>>> ExecuteSubscriptionA
8787
8888 return stream == null
8989 ? Observable . Throw < GraphQLResponse < TResponse > > ( new InvalidOperationException ( "the GraphQL execution did not return an observable" ) )
90- : stream . SelectMany ( executionResult => Observable . FromAsync ( token => ExecutionResultToGraphQLResponse < TResponse > ( executionResult , token ) ) ) ;
90+ : stream . SelectMany ( executionResult => Observable . FromAsync ( token => ExecutionResultToGraphQLResponseAsync < TResponse > ( executionResult , token ) ) ) ;
9191 }
9292
9393 private async Task < ExecutionResult > ExecuteAsync ( GraphQLRequest request , CancellationToken cancellationToken = default )
9494 {
95- var serializedRequest = Serializer . SerializeToString ( request ) ;
95+ string serializedRequest = Serializer . SerializeToString ( request ) ;
9696
9797 var deserializedRequest = JsonConvert . DeserializeObject < GraphQLRequest > ( serializedRequest ) ;
9898 var inputs = deserializedRequest . Variables != null
@@ -103,22 +103,21 @@ private async Task<ExecutionResult> ExecuteAsync(GraphQLRequest request, Cancell
103103 var result = await _documentExecuter . ExecuteAsync ( options =>
104104 {
105105 options . Schema = Schema ;
106- options . OperationName = request . OperationName ;
107- options . Query = request . Query ;
106+ options . OperationName = deserializedRequest ? . OperationName ;
107+ options . Query = deserializedRequest ? . Query ;
108108 options . Inputs = inputs ;
109109 options . CancellationToken = cancellationToken ;
110110 } ) ;
111111
112112 return result ;
113113 }
114114
115- private async Task < GraphQLResponse < TResponse > > ExecutionResultToGraphQLResponse < TResponse > ( ExecutionResult executionResult , CancellationToken cancellationToken = default )
115+ private async Task < GraphQLResponse < TResponse > > ExecutionResultToGraphQLResponseAsync < TResponse > ( ExecutionResult executionResult , CancellationToken cancellationToken = default )
116116 {
117- string json = await _documentWriter . WriteToStringAsync ( executionResult ) ;
118- // serialize result into utf8 byte stream
119- var resultStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( json ) ) ;
120- // deserialize using the provided serializer
121- return await Serializer . DeserializeFromUtf8StreamAsync < TResponse > ( resultStream , cancellationToken ) ;
117+ using var stream = new MemoryStream ( ) ;
118+ await _documentWriter . WriteAsync ( stream , executionResult , cancellationToken ) ;
119+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
120+ return await Serializer . DeserializeFromUtf8StreamAsync < TResponse > ( stream , cancellationToken ) ;
122121 }
123122
124123 #endregion
0 commit comments