Skip to content

Commit

Permalink
Fixed code smells #352
Browse files Browse the repository at this point in the history
  • Loading branch information
rstaib committed Jan 9, 2019
1 parent 0482054 commit 0ba21ae
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/Core/Core/Execution/JsonQueryResultSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public async Task SerializeAsync(

using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
await writer.WriteAsync(JsonConvert.SerializeObject(formatted));
await writer.WriteAsync(JsonConvert.SerializeObject(formatted))
.ConfigureAwait(false);
}
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/Core/Core/Execution/Middleware/ExecuteOperationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,31 @@ public async Task InvokeAsync(IQueryContext context)
{
if (IsContextValid(context))
{
IExecutionStrategy strategy =
_strategyResolver.Resolve(
context.Operation.Type);
IExecutionContext executionContext =
CreateExecutionContext(context);
IExecutionStrategy strategy = _strategyResolver
.Resolve(context.Operation.Type);
IExecutionContext executionContext = CreateExecutionContext(
context);

context.Result = await strategy.ExecuteAsync(
executionContext, executionContext.RequestAborted);
executionContext,
executionContext.RequestAborted)
.ConfigureAwait(false);
}

await _next(context);
await _next(context).ConfigureAwait(false);
}

private IExecutionContext CreateExecutionContext(IQueryContext context)
{
DirectiveLookup directives = GetOrCreateDirectiveLookup(context);

return new ExecutionContext(
context.Schema, context.ServiceScope, context.Operation,
context.Variables, directives, context.ContextData,
context.Schema,
context.ServiceScope,
context.Operation,
context.Variables,
directives,
context.ContextData,
context.RequestAborted);
}

Expand All @@ -57,9 +62,11 @@ private DirectiveLookup GetOrCreateDirectiveLookup(
context.Request.Query,
() =>
{
var directiveCollector =
new DirectiveCollector(context.Schema);
var directiveCollector = new DirectiveCollector(
context.Schema);

directiveCollector.VisitDocument(context.Document);

return directiveCollector.CreateLookup();
});
}
Expand Down
33 changes: 13 additions & 20 deletions src/Core/Core/Execution/MutationExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ private async Task<IExecutionResult> ExecuteMutationAsync(
try
{
IEnumerable<ResolverTask> rootResolverTasks =
CreateRootResolverTasks(executionContext,
CreateRootResolverTasks(
executionContext,
executionContext.Result.Data);

await ExecuteResolverBatchSeriallyAsync(
executionContext,
rootResolverTasks,
batchOperationHandler,
cancellationToken)
.ConfigureAwait(false);
.ConfigureAwait(false);

return executionContext.Result;
}
Expand All @@ -64,18 +65,15 @@ await ExecuteResolverSeriallyAsync(
nextBatch.Add,
batchOperationHandler,
cancellationToken)
.ConfigureAwait(false);

.ConfigureAwait(false);
// execute child fields with the default parallel flow logic
await ExecuteResolversAsync(
executionContext,
nextBatch,
batchOperationHandler,
cancellationToken)
.ConfigureAwait(false);

.ConfigureAwait(false);
nextBatch.Clear();

cancellationToken.ThrowIfCancellationRequested();
}
}
Expand All @@ -91,25 +89,20 @@ private async Task ExecuteResolverSeriallyAsync(
resolverTask,
executionContext.ErrorHandler,
cancellationToken);

await CompleteBatchOperationsAsync(
new[] { resolverTask.Task },
batchOperationHandler,
cancellationToken);

if (resolverTask.Task.IsCompleted)
{
resolverTask.ResolverResult = resolverTask.Task.Result;
}
else
{
resolverTask.ResolverResult = await resolverTask.Task;
}
cancellationToken)
.ConfigureAwait(false);
resolverTask.ResolverResult = await resolverTask.Task
.ConfigureAwait(false);

// serialize and integrate result into final query result
var completionContext = new FieldValueCompletionContext(
executionContext, resolverTask.ResolverContext,
resolverTask, enqueueTask);
executionContext,
resolverTask.ResolverContext,
resolverTask,
enqueueTask);

CompleteValue(completionContext);
}
Expand Down
28 changes: 16 additions & 12 deletions src/Core/Core/Execution/SubscriptionExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ internal sealed class SubscriptionExecutionStrategy
public SubscriptionExecutionStrategy(
IRequestTimeoutOptionsAccessor options)
{
if (options == null)
{
_options = options ??
throw new ArgumentNullException(nameof(options));
}
_options = options;
;
}

public override Task<IExecutionResult> ExecuteAsync(
Expand All @@ -45,14 +43,18 @@ private async Task<IExecutionResult> ExecuteInternalAsync(
EventDescription eventDescription = CreateEvent(executionContext);

IEventStream eventStream = await SubscribeAsync(
executionContext.Services, eventDescription);
executionContext.Services,
eventDescription)
.ConfigureAwait(false);

return new SubscriptionResult(
eventStream,
msg =>
{
IExecutionContext cloned = executionContext.Clone();

cloned.ContextData[typeof(IEventMessage).FullName] = msg;

return cloned;
},
ExecuteSubscriptionQueryAsync,
Expand All @@ -70,9 +72,8 @@ private EventDescription CreateEvent(
if (selections.Count == 1)
{
FieldSelection selection = selections.Single();
Dictionary<string, ArgumentValue> argumentValues =
selection.CoerceArgumentValues(
executionContext.Variables);
Dictionary<string, ArgumentValue> argumentValues = selection
.CoerceArgumentValues(executionContext.Variables);
var arguments = new List<ArgumentNode>();

foreach (KeyValuePair<string, ArgumentValue> argumentValue in
Expand All @@ -99,8 +100,8 @@ private Task<IEventStream> SubscribeAsync(
IServiceProvider services,
EventDescription @event)
{
IEventRegistry eventRegistry =
(IEventRegistry)services.GetService(typeof(IEventRegistry));
var eventRegistry = (IEventRegistry)services
.GetService(typeof(IEventRegistry));

if (eventRegistry == null)
{
Expand All @@ -124,13 +125,16 @@ private async Task<IReadOnlyQueryResult> ExecuteSubscriptionQueryAsync(
try
{
using (var combinedCts = CancellationTokenSource
.CreateLinkedTokenSource(requestTimeoutCts.Token,
.CreateLinkedTokenSource(
requestTimeoutCts.Token,
cancellationToken))
{
IQueryResult result = await ExecuteQueryAsync(
executionContext,
batchOperationHandler,
cancellationToken);
cancellationToken)
.ConfigureAwait(false);

return result.AsReadOnly();
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/Core/Core/Execution/SubscriptionResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -62,8 +62,11 @@ public async Task<IReadOnlyQueryResult> ReadAsync(
using (var ct = CancellationTokenSource.CreateLinkedTokenSource(
_cancellationTokenSource.Token, cancellationToken))
{
IEventMessage message = await _eventStream.ReadAsync(ct.Token);
return await ExecuteQueryAsync(message, ct.Token);
IEventMessage message = await _eventStream.ReadAsync(ct.Token)
.ConfigureAwait(false);

return await ExecuteQueryAsync(message, ct.Token)
.ConfigureAwait(false);
}
}

Expand All @@ -72,7 +75,9 @@ private async Task<IReadOnlyQueryResult> ExecuteQueryAsync(
CancellationToken cancellationToken)
{
IExecutionContext context = _contextFactory(message);
return await _executeQuery(context, cancellationToken);

return await _executeQuery(context, cancellationToken)
.ConfigureAwait(false);
}

public void Dispose()
Expand All @@ -91,6 +96,7 @@ protected virtual void Dispose(bool disposing)
_eventStream.Dispose();
_cancellationTokenSource.Dispose();
}

_isCompleted = true;
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/Core/Stitching/HttpQueryExecuter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
Expand All @@ -18,15 +18,13 @@ public class HttpQueryExecuter

public HttpQueryExecuter(HttpClient client)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_client = client ??
throw new ArgumentNullException(nameof(client));
}

public ISchema Schema => throw new NotImplementedException();

public void Dispose()
{

}
public void Dispose() { }

public async Task<IExecutionResult> ExecuteAsync(
QueryRequest request,
Expand All @@ -38,26 +36,25 @@ public async Task<IExecutionResult> ExecuteAsync(
OperationName = request.OperationName,
Variables = request.VariableValues
};

var content = new StringContent(
JsonConvert.SerializeObject(remoteRquest,
JsonConvert.SerializeObject(
remoteRquest,
new JsonSerializerSettings
{
ContractResolver =
new CamelCasePropertyNamesContractResolver()
}),
Encoding.UTF8,
"application/json");

HttpResponseMessage response = await _client.PostAsync("", content);

string json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<ClientQueryResult>(json);

HttpResponseMessage response = await _client
.PostAsync("", content).ConfigureAwait(false);
string json = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
ClientQueryResult result = JsonConvert
.DeserializeObject<ClientQueryResult>(json);
OrderedDictionary data = result.Data == null
? null
: result.Data.ToDictionary();

var queryResult = new QueryResult();

if (data != null)
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Types/DataLoader/DataLoaderBatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public async Task InvokeAsync(CancellationToken cancellationToken)
{
if (dataLoader.BufferedRequests > 0)
{
await dataLoader.DispatchAsync(cancellationToken);
await dataLoader.DispatchAsync(cancellationToken)
.ConfigureAwait(false);
}
}
}
Expand Down

0 comments on commit 0ba21ae

Please sign in to comment.