Skip to content

Commit

Permalink
feat(Core): adds scoped data context (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored and michaelstaib committed Jan 25, 2019
1 parent 00b2e5a commit 7b106aa
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 5 deletions.
46 changes: 46 additions & 0 deletions src/Core/Core.Tests/Execution/Middleware/ScopedContextDataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ChilliCream.Testing;
using Xunit;

namespace HotChocolate.Execution
{
public class ScopedContextDataTests
{
[Fact]
public async Task ScopedContextDataIsPassedAllongCorrectly()
{
// arrange
bool allDataIsPassedAlong = false;

ISchema schema = Schema.Create(
"type Query { nested: Nested } type Nested { foo: String }",
c => c.Use(next => context =>
{
if (context.ScopedContextData.ContainsKey("field"))
{
allDataIsPassedAlong = true;
context.Result = "123";
}
else
{
context.ScopedContextData = context.ScopedContextData.Add("field", "abc");
context.Result = new { foo = "123"};
}

return Task.CompletedTask;
}));

IQueryExecutor executor = schema.MakeExecutable(
b => b.UseDefaultPipeline()
);

// act
IExecutionResult result = await executor.ExecuteAsync(
new QueryRequest("{ nested { foo } }"));

// assert
Assert.True(allDataIsPassedAlong);
}
}
}
4 changes: 3 additions & 1 deletion src/Core/Core/Execution/ExecutionStrategyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ protected IEnumerable<ResolverTask> CreateRootResolverTasks(
fieldSelection,
Path.New(fieldSelection.ResponseName),
source,
result);
result,
ImmutableDictionary<string, object>.Empty
);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/DirectiveContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public object Result
public IDictionary<string, object> ContextData =>
_middlewareContext.ContextData;

public IImmutableDictionary<string, object> ScopedContextData
{
get => _middlewareContext.ScopedContextData;
set => _middlewareContext.ScopedContextData = value;
}

public T Argument<T>(NameString name) =>
_middlewareContext.Argument<T>(name);

Expand Down
6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/MiddlewareContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public object Result
public IDictionary<string, object> ContextData =>
_resolverContext.ContextData;

public IImmutableDictionary<string, object> ScopedContextData
{
get => _resolverContext.ScopedContextData;
set => _resolverContext.ScopedContextData = value;
}

public T Argument<T>(NameString name) =>
_resolverContext.Argument<T>(name);

Expand Down
6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/ResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public ResolverContext(
public IDictionary<string, object> ContextData =>
_executionContext.ContextData;

public IImmutableDictionary<string, object> ScopedContextData
{
get => _resolverTask.ScopedContextData;
set => _resolverTask.ScopedContextData = value;
}

public T Argument<T>(NameString name)
{
if (string.IsNullOrEmpty(name))
Expand Down
12 changes: 10 additions & 2 deletions src/Core/Core/Execution/Utilities/ResolverTask.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.Collections.Immutable;
using System.Threading.Tasks;
Expand All @@ -18,7 +18,8 @@ public ResolverTask(
FieldSelection fieldSelection,
Path path,
IImmutableStack<object> source,
IDictionary<string, object> result)
IDictionary<string, object> result,
IImmutableDictionary<string, object> scopedContextData)
{
_executionContext = executionContext;
Source = source;
Expand All @@ -27,6 +28,7 @@ public ResolverTask(
FieldType = fieldSelection.Field.Type;
Path = path;
_result = result;
ScopedContextData = scopedContextData;

ResolverContext = new ResolverContext(
executionContext, this,
Expand All @@ -53,6 +55,12 @@ public ResolverTask(
public object ResolverResult { get; set; }

public FieldDelegate FieldDelegate { get; }

public IImmutableDictionary<string, object> ScopedContextData
{
get;
set;
}

public void IntegrateResult(object value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ public void EnqueueForProcessing(
_enqueueResolverTask(new ResolverTask(
ExecutionContext, objectType, field,
Path.Append(field.ResponseName),
Source.Push(Value), objectResult));
Source.Push(Value), objectResult,
_resolverTask.ScopedContextData)
);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/Core/Types/Resolvers/IResolverContext.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.Collections.Immutable;
using System.Threading;
Expand Down Expand Up @@ -61,6 +61,13 @@ public interface IResolverContext
/// </summary>
IDictionary<string, object> ContextData { get; }

/// <summary>
/// The scoped context data dictionary can be used by middlewares and
/// resolvers to store and retrieve data during execution scoped to the
/// hierarchy
/// </summary>
IImmutableDictionary<string, object> ScopedContextData { get; set; }

/// <summary>
/// Notifies when the connection underlying this request is aborted
/// and thus request operations should be cancelled.
Expand Down

0 comments on commit 7b106aa

Please sign in to comment.