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

Resolve scalar stitching in delegated fields #1221

Merged
merged 16 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/Core/Abstractions/ErrorBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System;
using HotChocolate.Language;

Expand Down Expand Up @@ -27,5 +28,14 @@ public static IErrorBuilder AddLocation(
}
return builder;
}

public static IErrorBuilder SetMessage(
this IErrorBuilder builder,
string format,
params object[] args) =>
builder.SetMessage(string.Format(
CultureInfo.InvariantCulture,
format,
args));
}
}
4 changes: 2 additions & 2 deletions src/Core/Abstractions/Execution/IVariableValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface IVariableValueCollection
{
/// <summary>
/// Gets a coreced variable value from the collection.
/// Gets a coerced variable value from the collection.
/// </summary>
/// <param name="name">The variable name.</param>
/// <returns></returns>
Expand All @@ -22,7 +22,7 @@ public interface IVariableValueCollection
T GetVariable<T>(NameString name);

/// <summary>
/// Tries to get a coreced variable value from the collection.
/// Tries to get a coerced variable value from the collection.
/// </summary>
/// <param name="name">The variable name.</param>
/// <param name="value">The coerced variable value.</param>
Expand Down
12 changes: 6 additions & 6 deletions src/Core/Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Filters.Tests", "Type
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Tests.Documentation", "Types.Tests.Documentation\Types.Tests.Documentation.csproj", "{38537BB2-BDD9-4842-BD2D-0B84B371F1D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersistedQueries.FileSystem", "PersistedQueries.FileSystem\PersistedQueries.FileSystem.csproj", "{BC4DAE10-B145-4F9F-9504-BF5EA4C42A60}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersistedQueries.FileSystem", "PersistedQueries.FileSystem\PersistedQueries.FileSystem.csproj", "{BC4DAE10-B145-4F9F-9504-BF5EA4C42A60}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Subscriptions.Redis", "Subscriptions.Redis\Subscriptions.Redis.csproj", "{85660981-4992-49A8-A786-D6FB82E01463}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Subscriptions.Redis", "Subscriptions.Redis\Subscriptions.Redis.csproj", "{85660981-4992-49A8-A786-D6FB82E01463}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarWars", "StarWars\StarWars.csproj", "{6D66B162-D2F6-411E-8F9C-F0B3AB5A9289}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StarWars", "StarWars\StarWars.csproj", "{6D66B162-D2F6-411E-8F9C-F0B3AB5A9289}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersistedQueries.Redis", "PersistedQueries.Redis\PersistedQueries.Redis.csproj", "{B4CC72E4-C93E-4AFB-8381-BFF6F4B06CC9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersistedQueries.Redis", "PersistedQueries.Redis\PersistedQueries.Redis.csproj", "{B4CC72E4-C93E-4AFB-8381-BFF6F4B06CC9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersistedQueries.FileSystem.Tests", "PersistedQueries.FileSystem.Tests\PersistedQueries.FileSystem.Tests.csproj", "{F3657CF0-6B18-4021-8CCF-A7C131E4F745}"
EndProject
Expand All @@ -63,9 +63,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreenDonut", "..\DataLoader
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting", "Types.Sorting\Types.Sorting.csproj", "{F3B8AE0B-C6D9-4B45-8131-40E93CFE6BB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Types.Sorting.Tests", "Types.Sorting.Tests\Types.Sorting.Tests.csproj", "{A23C8758-6173-46F8-B132-28E5B75846B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Tests", "Types.Sorting.Tests\Types.Sorting.Tests.csproj", "{A23C8758-6173-46F8-B132-28E5B75846B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Types.Sorting.Mongo.Tests", "Types.Sorting.Mongo.Tests\Types.Sorting.Mongo.Tests.csproj", "{E8843255-77EB-471F-B682-528BB4B5D1D5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Mongo.Tests", "Types.Sorting.Mongo.Tests\Types.Sorting.Mongo.Tests.csproj", "{E8843255-77EB-471F-B682-528BB4B5D1D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static IQueryExecutionBuilder UseDefaultPipeline(
throw new ArgumentNullException(nameof(builder));
}

return builder
.UseDefaultPipeline(new QueryExecutionOptions());
return builder.UseDefaultPipeline(new QueryExecutionOptions());
}

public static IQueryExecutionBuilder UseDefaultPipeline(
Expand Down Expand Up @@ -119,7 +118,7 @@ public static IQueryExecutionBuilder UseActivePersistedQueryPipeline(
.UseWritePersistedQuery();
}

private static IQueryExecutionBuilder AddDefaultServices(
public static IQueryExecutionBuilder AddDefaultServices(
this IQueryExecutionBuilder builder,
IQueryExecutionOptionsAccessor options)
{
Expand All @@ -134,7 +133,7 @@ private static IQueryExecutionBuilder AddDefaultServices(
.AddDefaultDocumentHashProvider();
}

private static IQueryExecutionBuilder UseDefaultDiagnostics(
public static IQueryExecutionBuilder UseDefaultDiagnostics(
this IQueryExecutionBuilder builder,
IInstrumentationOptionsAccessor options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ internal class DictionaryToObjectValueConverter
{
public IValueNode Convert(object from, IInputType type, VariableDefinitionNode variable)
{
if (from == null)
{
throw new ArgumentNullException(nameof(from));
}

if (type is null)
{
throw new ArgumentNullException(nameof(type));
Expand All @@ -35,7 +30,28 @@ public IValueNode Convert(object from, IInputType type, VariableDefinitionNode v
Name = "$" + variable.Variable.Name.Value
};
Visit(from, context);
return (IValueNode)context.Object;
return from is null ? NullValueNode.Default : (IValueNode)context.Object;
}

public IValueNode Convert(object from, IInputType type, string variableName)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}

if (variableName is null)
{
throw new ArgumentNullException(nameof(variableName));
}

var context = new ConverterContext
{
InputType = type,
Name = "$" + variableName
};
Visit(from, context);
return from is null ? NullValueNode.Default : (IValueNode)context.Object;
}

protected override void VisitObject(
Expand Down
17 changes: 5 additions & 12 deletions src/Core/Core/Execution/Utilities/VariableValueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ private Variable CoerceVariableValue(
IReadOnlyDictionary<string, object> variableValues,
Variable variable)
{
var value = variableValues.TryGetValue(
variable.Name, out var rawValue)
var value = variableValues.TryGetValue(variable.Name, out var rawValue)
? Normalize(variableDefinition, variable, rawValue)
: variable.DefaultValue;

Expand Down Expand Up @@ -197,14 +196,9 @@ private static void CheckForInvalidValueType(

if (variable.Value != null)
{
if (variable.Value is IValueNode literal)
{
invalid = !variable.Type.IsInstanceOfType(literal);
}
else
{
invalid = !variable.Type.IsInstanceOfType(variable.Value);
}
invalid = variable.Value is IValueNode literal
? !variable.Type.IsInstanceOfType(literal)
: !variable.Type.IsInstanceOfType(variable.Value);
}

if (invalid)
Expand Down Expand Up @@ -255,8 +249,7 @@ private IType GetType(ITypeNode typeNode)
return _schema.GetType<INamedType>(namedType.Name.Value);
}

throw new NotSupportedException(
TypeResources.VariableValueBuilder_NodeKind);
throw new NotSupportedException(TypeResources.VariableValueBuilder_NodeKind);
}

private ref struct Variable
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Core/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
[assembly: InternalsVisibleTo("HotChocolate.Core.Tests")]
[assembly: InternalsVisibleTo("HotChocolate.Validation.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

// this is temporary until we reworked the variable coercion #1274
[assembly: InternalsVisibleTo("HotChocolate.Stitching")]
2 changes: 1 addition & 1 deletion src/Core/Language/Parser/GraphQLConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate.Language
/// <summary>
/// This class provides internal char utilities
/// that are used to tokenize a GraphQL source text.
/// These utilities are used by the lexer dfault implementation.
/// These utilities are used by the lexer default implementation.
/// </summary>
internal static partial class GraphQLConstants
{
Expand Down
3 changes: 1 addition & 2 deletions src/Core/Utilities/BufferHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public static async Task<T> ReadAsync<T>(

if (bytesRemaining == 0)
{
var next = ArrayPool<byte>.Shared.Rent(
buffer.Length * 2);
var next = ArrayPool<byte>.Shared.Rent(buffer.Length * 2);
Buffer.BlockCopy(buffer, 0, next, 0, buffer.Length);
ArrayPool<byte>.Shared.Return(buffer);
buffer = next;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Server.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Abstractions", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Tests.Utilities", "AspNetCore.Tests.Utilities\AspNetCore.Tests.Utilities.csproj", "{AF440449-DBE1-40AB-B155-87C3B31E7378}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarWars", "..\Core\StarWars\StarWars.csproj", "{9CA71125-2837-4F92-9822-CB41B64E2E43}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StarWars", "..\Core\StarWars\StarWars.csproj", "{9CA71125-2837-4F92-9822-CB41B64E2E43}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetClassic.Abstractions", "AspNetClassic.Abstractions\AspNetClassic.Abstractions.csproj", "{B930D413-E013-4DE6-B1AB-C88BE946E615}"
EndProject
Expand Down
24 changes: 24 additions & 0 deletions src/Stitching/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
},
"QueryName": null,
"QueryHash": null,
"OperationName": null,
"OperationName": "exec_batch",
"VariableValues": {
"__0__a": "foo"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using ChilliCream.Testing;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;
Expand All @@ -24,10 +23,9 @@ public void CreateVariableValue()
});

var context = new Mock<IResolverContext>(MockBehavior.Strict);
context.SetupGet(t => t.Field).Returns(
schema.GetType<ObjectType>("Query").Fields["foo"]);
context.Setup(t => t.Argument<object>("a"))
.Returns("baz");
ObjectField field = schema.GetType<ObjectType>("Query").Fields["foo"];
context.SetupGet(t => t.Field).Returns(field);
context.Setup(t => t.Argument<IValueNode>("a")).Returns(new StringValueNode("baz"));

var scopedVariable = new ScopedVariableNode(
null,
Expand All @@ -37,15 +35,15 @@ public void CreateVariableValue()
// act
var resolver = new ArgumentScopedVariableResolver();
VariableValue value = resolver.Resolve(
context.Object, scopedVariable,
new NamedTypeNode(new NameNode("abc")));
context.Object,
scopedVariable,
schema.GetType<StringType>("String"));

// assert
Assert.Equal("bar",
Assert.IsType<StringValueNode>(value.DefaultValue).Value);
Assert.Equal("bar", Assert.IsType<StringValueNode>(value.DefaultValue).Value);
Assert.Equal("arguments_a", value.Name);
Assert.IsType<NamedTypeNode>(value.Type);
Assert.Equal("baz", value.Value);
Assert.Equal("String", Assert.IsType<NamedTypeNode>(value.Type).Name.Value);
Assert.Equal("baz", value.Value.Value);
}

[Fact]
Expand Down Expand Up @@ -83,8 +81,9 @@ public void ArgumentDoesNotExist()
// act
var resolver = new ArgumentScopedVariableResolver();
Action a = () => resolver.Resolve(
context.Object, scopedVariable,
new NamedTypeNode(new NameNode("abc")));
context.Object,
scopedVariable,
schema.GetType<StringType>("String"));

// assert
Assert.Collection(
Expand All @@ -111,8 +110,10 @@ public void ContextIsNull()

// act
var resolver = new ArgumentScopedVariableResolver();
Action a = () => resolver.Resolve(null, scopedVariable,
new NamedTypeNode(new NameNode("abc")));
Action a = () => resolver.Resolve(
null,
scopedVariable,
schema.GetType<StringType>("String"));

// assert
Assert.Equal("context",
Expand All @@ -139,8 +140,10 @@ public void ScopedVariableIsNull()

// act
var resolver = new ArgumentScopedVariableResolver();
Action a = () => resolver.Resolve(context.Object, null,
new NamedTypeNode(new NameNode("abc")));
Action a = () => resolver.Resolve(
context.Object,
null,
schema.GetType<StringType>("String"));

// assert
Assert.Equal("variable",
Expand All @@ -160,10 +163,9 @@ public void InvalidScope()
});

var context = new Mock<IMiddlewareContext>();
context.SetupGet(t => t.Field).Returns(
schema.GetType<ObjectType>("Query").Fields["foo"]);
context.Setup(t => t.Argument<object>(It.IsAny<string>()))
.Returns("Baz");
ObjectField field = schema.GetType<ObjectType>("Query").Fields["foo"];
context.SetupGet(t => t.Field).Returns(field);
context.Setup(t => t.Argument<object>(It.IsAny<string>())).Returns("Baz");

var scopedVariable = new ScopedVariableNode(
null,
Expand All @@ -172,8 +174,10 @@ public void InvalidScope()

// act
var resolver = new ArgumentScopedVariableResolver();
Action a = () => resolver.Resolve(context.Object, scopedVariable,
new NamedTypeNode(new NameNode("abc")));
Action a = () => resolver.Resolve(
context.Object,
scopedVariable,
schema.GetType<StringType>("String"));

// assert
Assert.Equal("variable",
Expand Down
Loading