Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Core): adds scoped data context #538

Merged
merged 4 commits into from
Jan 25, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -163,7 +163,9 @@ protected IEnumerable<ResolverTask> CreateRootResolverTasks(
fieldSelection,
Path.New(fieldSelection.ResponseName),
source,
result);
result,
ImmutableDictionary.CreateBuilder<string, object>()
.ToImmutable());
PascalSenn marked this conversation as resolved.
Show resolved Hide resolved
}
}

6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/DirectiveContext.cs
Original file line number Diff line number Diff line change
@@ -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);

6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/MiddlewareContext.cs
Original file line number Diff line number Diff line change
@@ -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);

6 changes: 6 additions & 0 deletions src/Core/Core/Execution/Resolvers/ResolverContext.cs
Original file line number Diff line number Diff line change
@@ -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))
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;
@@ -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;
@@ -27,6 +28,7 @@ public ResolverTask(
FieldType = fieldSelection.Field.Type;
Path = path;
_result = result;
ScopedContextData = scopedContextData;

ResolverContext = new ResolverContext(
executionContext, this,
@@ -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)
{
Original file line number Diff line number Diff line change
@@ -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)
);
}
}

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;
@@ -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.