-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from byme8/feature/constants
Add support for C# constants
- Loading branch information
Showing
6 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/ZeroQL.SourceGenerators/Resolver/GraphQLConstantResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace ZeroQL.SourceGenerators.Resolver; | ||
|
||
public static class GraphQLConstantResolver | ||
{ | ||
public static string ToGraphQL(ITypeSymbol symbol, object value) => | ||
symbol switch | ||
{ | ||
{ SpecialType: SpecialType.System_String } => $@"""{value}""", | ||
{ TypeKind: TypeKind.Enum } => MaterializeEnum(symbol, value), | ||
_ => value.ToString(), | ||
}; | ||
|
||
private static string MaterializeEnum(ITypeSymbol symbol, object value) | ||
{ | ||
if (symbol is not INamedTypeSymbol enumType) | ||
{ | ||
return value.ToString(); | ||
} | ||
|
||
var graphQLName = enumType | ||
.GetMembers() | ||
.OfType<IFieldSymbol>() | ||
.FirstOrDefault(o => o.ConstantValue?.Equals(value) ?? false) | ||
?.GetAttributes() | ||
.FirstOrDefault(o => o.AttributeClass?.Name == "GraphQLNameAttribute") | ||
?.ConstructorArguments | ||
.FirstOrDefault() | ||
.Value? | ||
.ToString(); | ||
|
||
return graphQLName!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/ZeroQL.Tests/SourceGeneration/QueryTests.SupportForConstants.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
Query: mutation { addUser(firstName: "John", lastName: "Smith") { id } }, | ||
Data: { | ||
Value: 10 | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/ZeroQL.Tests/SourceGeneration/QueryTests.SupportForEnumConstants.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
Query: mutation { addUserKindPascal(userKindPascal: Good)}, | ||
Data: 12 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters