Skip to content

Commit

Permalink
Detect Pure Resolver Properly with new Resolver Compiler (#7181)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jun 20, 2024
1 parent a4fc933 commit 7b5dd10
Show file tree
Hide file tree
Showing 35 changed files with 817 additions and 504 deletions.
13 changes: 12 additions & 1 deletion src/HotChocolate/Core/src/Abstractions/ErrorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public IErrorBuilder SetMessage(string message)
AbstractionResources.Error_Message_Must_Not_Be_Null,
nameof(message));
}

_message = message;
return this;
}
Expand Down Expand Up @@ -110,7 +111,17 @@ public IErrorBuilder AddLocation(Location location)
public IErrorBuilder AddLocation(int line, int column) =>
AddLocation(new Location(line, column));

public IErrorBuilder AddLocation<T>(IReadOnlyList<T>? syntaxNodes) where T : ISyntaxNode
public IErrorBuilder AddLocation(ISyntaxNode syntaxNode)
{
if (syntaxNode.Location is { } location)
{
AddLocation(location.Line, location.Column);
}

return this;
}

public IErrorBuilder SetLocations<T>(IReadOnlyList<T>? syntaxNodes) where T : ISyntaxNode
{
if (syntaxNodes is null)
{
Expand Down
4 changes: 3 additions & 1 deletion src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public interface IErrorBuilder

IErrorBuilder AddLocation(int line, int column);

IErrorBuilder AddLocation<T>(IReadOnlyList<T>? syntaxNodes) where T : ISyntaxNode;
IErrorBuilder AddLocation(ISyntaxNode syntaxNode);

IErrorBuilder SetLocations<T>(IReadOnlyList<T>? syntaxNodes) where T : ISyntaxNode;

IErrorBuilder ClearLocations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void SetError(
.SetMessage(AuthorizeMiddleware_NoDefaultPolicy)
.SetCode(ErrorCodes.Authentication.NoDefaultPolicy)
.SetPath(context.Path)
.AddLocation([context.Selection.SyntaxNode])
.SetLocations([context.Selection.SyntaxNode])
.Build(),
AuthorizeResult.PolicyNotFound
=> ErrorBuilder.New()
Expand All @@ -90,7 +90,7 @@ private void SetError(
_directive.Policy!)
.SetCode(ErrorCodes.Authentication.PolicyNotFound)
.SetPath(context.Path)
.AddLocation([context.Selection.SyntaxNode])
.SetLocations([context.Selection.SyntaxNode])
.Build(),
_
=> ErrorBuilder.New()
Expand All @@ -100,7 +100,7 @@ private void SetError(
? ErrorCodes.Authentication.NotAuthorized
: ErrorCodes.Authentication.NotAuthenticated)
.SetPath(context.Path)
.AddLocation([context.Selection.SyntaxNode])
.SetLocations([context.Selection.SyntaxNode])
.Build(),
};
}
22 changes: 11 additions & 11 deletions src/HotChocolate/Core/src/Execution/ErrorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IError ArgumentNonNullError(
.SetMessage(
ErrorHelper_ArgumentNonNullError_Message,
argument.Name.Value)
.AddLocation([argument])
.SetLocations([argument])
.SetExtension("responseName", responseName)
.SetExtension("errorPath", validationResult.Path)
.Build();
Expand All @@ -30,7 +30,7 @@ public static IError ArgumentValueIsInvalid(
GraphQLException exception)
{
return ErrorBuilder.FromError(exception.Errors[0])
.AddLocation([argument])
.SetLocations([argument])
.SetExtension("responseName", responseName)
.Build();
}
Expand All @@ -50,7 +50,7 @@ public static IError InvalidLeafValue(
Path path)
{
return ErrorBuilder.FromError(exception.Errors[0])
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.CannotSerializeLeafValue)
.Build();
Expand All @@ -64,7 +64,7 @@ public static IError UnexpectedLeafValueSerializationError(
{
return errorHandler
.CreateUnexpectedError(exception)
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.CannotSerializeLeafValue)
.Build();
Expand All @@ -77,7 +77,7 @@ public static IError UnableToResolveTheAbstractType(
{
return ErrorBuilder.New()
.SetMessage(ErrorHelper_UnableToResolveTheAbstractType_Message, typeName)
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.CannotResolveAbstractType)
.Build();
Expand All @@ -91,7 +91,7 @@ public static IError UnexpectedErrorWhileResolvingAbstractType(
{
return ErrorBuilder.New()
.SetMessage(ErrorHelper_UnableToResolveTheAbstractType_Message, typeName)
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.CannotResolveAbstractType)
.SetException(exception)
Expand All @@ -105,7 +105,7 @@ public static IError ListValueIsNotSupported(
{
return ErrorBuilder.New()
.SetMessage(ErrorHelper_ListValueIsNotSupported_Message, listType.FullName!)
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.ListTypeNotSupported)
.Build();
Expand All @@ -117,7 +117,7 @@ public static IError UnexpectedValueCompletionError(
{
return ErrorBuilder.New()
.SetMessage(ErrorHelper_UnexpectedValueCompletionError_Message)
.AddLocation([field])
.SetLocations([field])
.SetPath(path)
.SetCode(ErrorCodes.Execution.ListTypeNotSupported)
.Build();
Expand Down Expand Up @@ -161,7 +161,7 @@ public static IError ValueCompletion_CouldNotResolveAbstractType(
result.GetType().FullName ?? result.GetType().Name,
field.Name)
.SetPath(path)
.AddLocation([field])
.SetLocations([field])
.Build();

public static IOperationResult StateInvalidForDocumentValidation() =>
Expand Down Expand Up @@ -237,7 +237,7 @@ public static IError NonNullOutputFieldViolation(Path? path, FieldNode selection
.SetMessage("Cannot return null for non-nullable field.")
.SetCode(ErrorCodes.Execution.NonNullViolation)
.SetPath(path)
.AddLocation([selection])
.SetLocations([selection])
.Build();

public static IError PersistedQueryNotFound(OperationDocumentId requestedKey)
Expand All @@ -264,7 +264,7 @@ public static IError NoNullBubbling_ArgumentValue_NotAllowed(
ArgumentNode argument)
{
var errorBuilder = ErrorBuilder.New();
errorBuilder.AddLocation([argument.Value]);
errorBuilder.SetLocations([argument.Value]);
errorBuilder.SetMessage(ErrorHelper_NoNullBubbling_ArgumentValue_NotAllowed);

return errorBuilder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static OperationContext ReportError(
var error = operationContext.ErrorHandler
.CreateUnexpectedError(exception)
.SetPath(path)
.AddLocation([selection.SyntaxNode])
.SetLocations([selection.SyntaxNode])
.Build();

ReportError(operationContext, error, resolverContext, selection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void ReportError(string errorMessage)
ErrorBuilder.New()
.SetMessage(errorMessage)
.SetPath(Path)
.AddLocation([_selection.SyntaxNode])
.SetLocations([_selection.SyntaxNode])
.Build());
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public void ReportError(Exception exception, Action<IErrorBuilder>? configure =
var errorBuilder = _operationContext.ErrorHandler
.CreateUnexpectedError(exception)
.SetPath(Path)
.AddLocation([_selection.SyntaxNode]);
.SetLocations([_selection.SyntaxNode]);

configure?.Invoke(errorBuilder);

Expand Down
28 changes: 14 additions & 14 deletions src/HotChocolate/Core/src/Execution/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static GraphQLException VariableIsNotAnInputType(
.SetCode(ErrorCodes.Execution.MustBeInputType)
.SetExtension("variable", variableDefinition.Variable.Name.Value)
.SetExtension("type", variableDefinition.Type.ToString())
.AddLocation([variableDefinition])
.SetLocations([variableDefinition])
.Build());
}

Expand All @@ -33,7 +33,7 @@ public static GraphQLException NonNullVariableIsNull(
variableDefinition.Variable.Name.Value)
.SetCode(ErrorCodes.Execution.NonNullViolation)
.SetExtension("variable", variableDefinition.Variable.Name.Value)
.AddLocation([variableDefinition])
.SetLocations([variableDefinition])
.Build());
}

Expand All @@ -51,7 +51,7 @@ public static GraphQLException VariableValueInvalidType(
variableDefinition.Variable.Name.Value)
.SetCode(ErrorCodes.Execution.InvalidType)
.SetExtension("variable", variableDefinition.Variable.Name.Value)
.AddLocation([variableDefinition]);
.SetLocations([variableDefinition]);

if (exception is not null)
{
Expand All @@ -75,7 +75,7 @@ public static GraphQLException FieldDoesNotExistOnType(
ThrowHelper_FieldDoesNotExistOnType,
selection.Name.Value,
typeName)
.AddLocation([selection])
.SetLocations([selection])
.Build());
}

Expand Down Expand Up @@ -126,7 +126,7 @@ public static GraphQLException ResolverContext_LiteralsNotSupported(
new(ErrorBuilder.New()
.SetMessage(ThrowHelper_ResolverContext_LiteralsNotSupported_Message)
.SetPath(path)
.AddLocation([field])
.SetLocations([field])
.SetExtension("fieldName", field.Name)
.SetExtension("argumentName", argumentName)
.SetExtension("requestedType", requestedType.FullName)
Expand All @@ -140,7 +140,7 @@ public static GraphQLException ResolverContext_CannotConvertArgument(
argumentName,
requestedType.FullName ?? requestedType.Name)
.SetPath(path)
.AddLocation([field])
.SetLocations([field])
.SetExtension("fieldName", field.Name)
.SetExtension("argumentName", argumentName)
.SetExtension("requestedType", requestedType.FullName)
Expand All @@ -155,7 +155,7 @@ public static GraphQLException ResolverContext_LiteralNotCompatible(
actualType.FullName ?? actualType.Name,
requestedType.FullName ?? actualType.Name)
.SetPath(path)
.AddLocation([field])
.SetLocations([field])
.SetExtension("fieldName", field.Name)
.SetExtension("argumentName", argumentName)
.SetExtension("requestedType", requestedType.FullName)
Expand All @@ -170,7 +170,7 @@ public static GraphQLException ResolverContext_ArgumentDoesNotExist(
argumentName,
field.Name.Value)
.SetPath(path)
.AddLocation([field])
.SetLocations([field])
.SetExtension("fieldName", field.Name)
.SetExtension("argumentName", argumentName)
.Build());
Expand All @@ -183,15 +183,15 @@ public static GraphQLException OperationResolverHelper_NoOperationFound(
DocumentNode documentNode) =>
new(ErrorBuilder.New()
.SetMessage(ThrowHelper_OperationResolverHelper_NoOperationFound_Message)
.AddLocation([documentNode])
.SetLocations([documentNode])
.Build());

public static GraphQLException OperationResolverHelper_MultipleOperation(
OperationDefinitionNode firstOperation,
OperationDefinitionNode secondOperation) =>
new(ErrorBuilder.New()
.SetMessage(ThrowHelper_OperationResolverHelper_MultipleOperation_Message)
.AddLocation([firstOperation, secondOperation])
.SetLocations([firstOperation, secondOperation])
.Build());

public static GraphQLException OperationResolverHelper_InvalidOperationName(
Expand All @@ -200,7 +200,7 @@ public static GraphQLException OperationResolverHelper_InvalidOperationName(
.SetMessage(
ThrowHelper_OperationResolverHelper_InvalidOperationName_Message,
operationName)
.AddLocation([documentNode])
.SetLocations([documentNode])
.SetExtension("operationName", operationName)
.Build());

Expand All @@ -220,20 +220,20 @@ public static GraphQLException CollectVariablesVisitor_NoCompatibleType(
.SetMessage(ThrowHelper_CollectVariablesVisitor_NoCompatibleType_Message)
.SetCode(ErrorCodes.Execution.AutoMapVarError)
.SetPath(path)
.AddLocation([node])
.SetLocations([node])
.Build());

public static GraphQLException FieldVisibility_ValueNotSupported(IValueNode value) =>
new(ErrorBuilder.New()
.SetMessage(ThrowHelper_FieldVisibility_ValueNotSupported_Message)
.AddLocation([value])
.SetLocations([value])
.Build());

public static GraphQLException QueryCompiler_CompositeTypeSelectionSet(
FieldNode selection) =>
new(ErrorBuilder.New()
.SetMessage(ThrowHelper_QueryCompiler_CompositeTypeSelectionSet_Message)
.AddLocation([selection])
.SetLocations([selection])
.Build());

public static GraphQLException OperationExecutionMiddleware_NoBatchDispatcher() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using HotChocolate.Types.Analyzers.Helpers;
using HotChocolate.Types.Analyzers.Models;
using Microsoft.CodeAnalysis;

namespace HotChocolate.Types.Analyzers.FileBuilders;

Expand Down Expand Up @@ -53,7 +52,7 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)

using (_writer.IncreaseIndent())
{
if (objectTypeExtension.Members.Length > 0)
if (objectTypeExtension.Resolvers.Length > 0)
{
_writer.WriteIndentedLine("const global::{0} bindingFlags =", WellKnownTypes.BindingFlags);
using (_writer.IncreaseIndent())
Expand Down Expand Up @@ -85,9 +84,9 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
}
}

if (objectTypeExtension.Members.Length > 0)
if (objectTypeExtension.Resolvers.Length > 0)
{
foreach (var member in objectTypeExtension.Members)
foreach (var resolver in objectTypeExtension.Resolvers)
{
_writer.WriteLine();
_writer.WriteIndentedLine("descriptor");
Expand All @@ -96,24 +95,23 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
{
_writer.WriteIndentedLine(
".Field(thisType.GetMember(\"{0}\", bindingFlags)[0])",
member.Name);
resolver.Member.Name);

if (member is IMethodSymbol method &&
method.GetResultKind() is not ResolverResultKind.Pure)
if (resolver.IsPure)
{
_writer.WriteIndentedLine(
".Extend().Definition.Resolver = {0}Resolvers.{1}_{2};",
".Extend().Definition.PureResolver = {0}Resolvers.{1}_{2};",
objectTypeExtension.Type.ToDisplayString(),
objectTypeExtension.Type.Name,
member.Name);
resolver.Member.Name);
}
else
{
_writer.WriteIndentedLine(
".Extend().Definition.PureResolver = {0}Resolvers.{1}_{2};",
".Extend().Definition.Resolver = {0}Resolvers.{1}_{2};",
objectTypeExtension.Type.ToDisplayString(),
objectTypeExtension.Type.Name,
member.Name);
resolver.Member.Name);
}
}
}
Expand All @@ -122,7 +120,7 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
_writer.WriteLine();
_writer.WriteIndentedLine("Configure(descriptor);");

if (objectTypeExtension.Members.Length > 0)
if (objectTypeExtension.Resolvers.Length > 0)
{
_writer.WriteLine();
_writer.WriteIndentedLine("descriptor.Extend().Context.OnSchemaCreated(");
Expand Down
Loading

0 comments on commit 7b5dd10

Please sign in to comment.