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

Allow enum casting in GraphQL query. #101

Merged
merged 1 commit into from
Apr 14, 2024
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
8 changes: 8 additions & 0 deletions src/TestApp/ZeroQL.TestApp/QueryFragments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public static UserModel AsUserWithRoleNameInitializers(this User user)
Role = user.Role(o => o.Name)
};
}

[GraphQLFragment]
public static UserRead AsUserRead(this User user)
=> new UserRead()
{
Id = user.Id,
Kind2 = (UserKind2)user.UserKind
};

[GraphQLFragment]
public static UserModel AsUserWithRoleNameExpression(this User user)
Expand Down
21 changes: 4 additions & 17 deletions src/TestApp/ZeroQL.TestApp/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,18 @@ public record UserModel
{
public UserModel()
{

}

public UserModel(string firstName, string lastName, string? role)
{
FirstName = firstName;
LastName = lastName;
Role = role;
}

public string FirstName
{
get;
init;
}
public string FirstName { get; init; }

public string LastName
{
get;
init;
}
public string LastName { get; init; }

public string? Role
{
get;
init;
}
public string? Role { get; init; }
}
14 changes: 14 additions & 0 deletions src/TestApp/ZeroQL.TestApp/UserRead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ZeroQL.TestApp;

public class UserRead
{
public ID Id { get; set; }

public UserKind2 Kind2 { get; set; }
}

public enum UserKind2
{
User,
Admin
}
26 changes: 24 additions & 2 deletions src/ZeroQL.SourceGenerators/Resolver/GraphQLQueryResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ private static Result<string> ResolveQuery(GraphQLResolveContext context, CSharp
{
return ResolveQuery(context.WithParent(arrowExpression), arrowExpression.Expression);
}
case CastExpressionSyntax cast:
{
return HandleEnumCast(context, cast);
}
}

return Failed(node);
Expand Down Expand Up @@ -370,8 +374,9 @@ private static Result<string> HandleArgumentAsVariable(GraphQLResolveContext con
{
return variable.GraphQLValue;
}

var possibleMethodSymbol = context.SemanticModel.GetSymbolInfo(argument.Parent!.Parent!, context.CancellationToken);

var possibleMethodSymbol =
context.SemanticModel.GetSymbolInfo(argument.Parent!.Parent!, context.CancellationToken);
if (possibleMethodSymbol.Symbol is not IMethodSymbol methodSymbol)
{
return Failed(argument);
Expand Down Expand Up @@ -917,6 +922,23 @@ private static Result<string> HandleFieldSelector(
return stringBuilder.ToString();
}

private static Result<string> HandleEnumCast(GraphQLResolveContext context, CastExpressionSyntax cast)
{
var castedTo = context.SemanticModel.GetTypeInfo(cast.Type);
if (castedTo.Type is null)
{
return Failed(cast);
}

var type = castedTo.Type;
if (type.TypeKind != TypeKind.Enum)
{
return Failed(cast);
}

return ResolveQuery(context.WithParent(cast), cast.Expression);
}

private static bool IsUnionType(GraphQLResolveContext context, INamedTypeSymbol namedTypeSymbol)
{
var selectorType = namedTypeSymbol.TypeArguments.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
Query: query { me { id userKind } },
Data: {
Id: {
Value: 1
}
}
}
9 changes: 9 additions & 0 deletions src/ZeroQL.Tests/SourceGeneration/FragmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public async Task CanApplyFragmentWithInitializers()
await Validate(project, graphqlQuery);
}

[Fact]
public async Task CanApplyFragmentWithEnumCast()
{
var csharpQuery = "q => q.Me(o => o.AsUserRead())";
var result = await TestProject.Project.Execute(csharpQuery);

await Verify(result);
}

[Fact]
public async Task CanApplyFragmentWithArgument()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
Query: query { me { id userKind } },
Data: {
Id: {
Value: 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
Query: query { me { id userKind } },
Data: {
Id: {
Value: 1
}
}
}
20 changes: 13 additions & 7 deletions src/ZeroQL.Tests/SourceGeneration/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,19 @@ public async Task SupportsAnonymousTypeWithConstantArgumentQuery()
[Fact]
public async Task SupportForEnums()
{
var csharpQuery = "static q => q.Me(o => o.UserKind)";
var graphqlQuery = @"query { me { userKind } }";

var project = await TestProject.Project
.ReplacePartOfDocumentAsync("Program.cs", (TestProject.MeQuery, csharpQuery));

await project.Validate(graphqlQuery);
var query = "q => q.Me(o => new { o.Id, o.UserKind })";
var response = await TestProject.Project.Execute(query);

await Verify(response);
}

[Fact]
public async Task SupportForEnumsWithCast()
{
var query = "q => q.Me(o => new { Id = o.Id, UserKind = (UserKind)o.UserKind })";
var response = await TestProject.Project.Execute(query);

await Verify(response);
}

[Fact]
Expand Down