Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Text;
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using HotChocolate.Validation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Formatting;
Expand Down Expand Up @@ -92,7 +94,6 @@ public static CSharpGeneratorResult Generate(
// If we cannot create a schema, we will return the schema validation errors.
if (!TryCreateSchema(
typeSystemFiles,
fileLookup,
errors,
settings.StrictSchemaValidation,
settings.NoStore,
Expand Down Expand Up @@ -404,31 +405,27 @@ private static bool TryParseDocuments(

private static bool TryCreateSchema(
IReadOnlyList<GraphQLFile> files,
Dictionary<ISyntaxNode, string> fileLookup,
ICollection<IError> errors,
bool strictValidation,
bool noStore,
[NotNullWhen(true)] out Schema? schema)
[NotNullWhen(true)] out MutableSchemaDefinition? schema)
{
try
{
schema = SchemaHelper.Load(files, strictValidation, noStore);
return true;
}
catch (SchemaException ex)
catch (SchemaInitializationException ex)
{
foreach (var error in ex.Errors)
{
errors.Add(error.SchemaError(fileLookup));
}
errors.Add(ex.SchemaError());

schema = null;
return false;
}
}

private static bool TryValidateRequestAsync(
Schema schema,
ISchemaDefinition schema,
IReadOnlyList<GraphQLFile> executableFiles,
Dictionary<ISyntaxNode, string> fileLookup,
List<IError> errors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void ForEachProperty(
{
foreach (var property in descriptor.Properties)
{
if (property.Name.EqualsOrdinal(WellKnownNames.TypeName))
if (property.Name.Equals(WellKnownNames.TypeName, StringComparison.Ordinal))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private MethodCallBuilder CreateBuildDataStatement(ObjectTypeDescriptor concrete

foreach (var property in concreteType.Properties)
{
if (property.Name.EqualsOrdinal(WellKnownNames.TypeName))
if (property.Name.Equals(WellKnownNames.TypeName, StringComparison.Ordinal))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using HotChocolate.Language;
using HotChocolate.Types;

namespace StrawberryShake.CodeGeneration.Analyzers;

internal static class DirectiveValueHelper
{
public static string? GetStringArgument(
this IReadOnlyDirectiveCollection directives,
string directiveName,
string argumentName)
{
var directive = directives.FirstOrDefault(directiveName);

if (directive is not null
&& directive.Arguments.TryGetValue(argumentName, out var value)
&& value is StringValueNode stringValue)
{
return stringValue.Value;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate;
using HotChocolate.Types;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.Analyzers.Types;
using static StrawberryShake.CodeGeneration.Utilities.NameUtils;

namespace StrawberryShake.CodeGeneration.Analyzers;
Expand All @@ -9,41 +8,37 @@ public partial class DocumentAnalyzer
{
private static void CollectEnumTypes(IDocumentAnalyzerContext context)
{
var analyzer = new EnumTypeUsageAnalyzer((Schema)context.Schema);
var analyzer = new EnumTypeUsageAnalyzer(context.Schema);
analyzer.Analyze(context.Document);

foreach (var enumType in analyzer.EnumTypes)
{
RenameDirective? rename;
var values = new List<EnumValueModel>();

foreach (var enumValue in enumType.Values)
{
rename = enumValue.Directives.FirstOrDefault<RenameDirective>()?.ToValue<RenameDirective>();

var value = enumValue.Directives.FirstOrDefault<EnumValueDirective>()?.ToValue<EnumValueDirective>();
var rename = enumValue.Directives.GetStringArgument("rename", "name");
var value = enumValue.Directives.GetStringArgument("enumValue", "value");

values.Add(new EnumValueModel(
rename?.Name ?? GetEnumValue(enumValue.Name),
rename ?? GetEnumValue(enumValue.Name),
enumValue.Description,
enumValue,
value?.Value));
value));
}

rename = enumType.Directives.FirstOrDefault<RenameDirective>()?.ToValue<RenameDirective>();

var serializationType =
enumType.Directives.FirstOrDefault<SerializationTypeDirective>()?.ToValue<SerializationTypeDirective>();
var typeRename = enumType.Directives.GetStringArgument("rename", "name");
var serializationType = enumType.Directives.GetStringArgument("serializationType", "name");

var typeName = context.ResolveTypeName(rename?.Name ?? GetClassName(enumType.Name));
var typeName = context.ResolveTypeName(typeRename ?? GetClassName(enumType.Name));

context.RegisterModel(
typeName,
new EnumTypeModel(
typeName,
enumType.Description,
enumType,
serializationType?.Name,
serializationType,
values));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using HotChocolate;
using HotChocolate.Types;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.Analyzers.Types;
using static StrawberryShake.CodeGeneration.Utilities.NameUtils;

namespace StrawberryShake.CodeGeneration.Analyzers;
Expand All @@ -10,21 +8,21 @@ public partial class DocumentAnalyzer
{
private static void CollectInputObjectTypes(IDocumentAnalyzerContext context)
{
var analyzer = new InputObjectTypeUsageAnalyzer((Schema)context.Schema);
var analyzer = new InputObjectTypeUsageAnalyzer(context.Schema);
analyzer.Analyze(context.Document);

var namesOfInputTypesWithUploadScalar = CollectTypesWithUploadScalar(analyzer);

foreach (var namedInputType in analyzer.InputTypes)
{
if (namedInputType is InputObjectType inputObjectType)
if (namedInputType is IInputObjectTypeDefinition inputObjectType)
{
RegisterInputObjectType(
context,
inputObjectType,
namesOfInputTypesWithUploadScalar.Contains(namedInputType.Name));
}
else if (namedInputType is ILeafType)
else if (namedInputType.IsLeafType())
{
context.RegisterType(namedInputType);
}
Expand All @@ -33,18 +31,17 @@ private static void CollectInputObjectTypes(IDocumentAnalyzerContext context)

private static void RegisterInputObjectType(
IDocumentAnalyzerContext context,
InputObjectType inputObjectType,
IInputObjectTypeDefinition inputObjectType,
bool hasUpload)
{
RenameDirective? rename;
var fields = new List<InputFieldModel>();

foreach (var inputField in inputObjectType.Fields)
{
rename = inputField.Directives.FirstOrDefault<RenameDirective>()?.ToValue<RenameDirective>();
var rename = inputField.Directives.GetStringArgument("rename", "name");

fields.Add(new InputFieldModel(
GetClassName(rename?.Name ?? inputField.Name),
GetClassName(rename ?? inputField.Name),
inputField.Description,
inputField,
inputField.DefaultValue is not null
Expand All @@ -55,10 +52,10 @@ inputField.DefaultValue is not null
context.RegisterType(inputField.Type.NamedType());
}

rename = inputObjectType.Directives.FirstOrDefault<RenameDirective>()?.ToValue<RenameDirective>();
var typeRename = inputObjectType.Directives.GetStringArgument("rename", "name");

var typeName = context.ResolveTypeName(
GetClassName(rename?.Name ?? inputObjectType.Name));
GetClassName(typeRename ?? inputObjectType.Name));

context.RegisterModel(
typeName,
Expand Down Expand Up @@ -86,7 +83,7 @@ private static HashSet<string> CollectTypesWithUploadScalar(
continue;
}

if (namedInputType is InputObjectType type)
if (namedInputType is IInputObjectTypeDefinition type)
{
foreach (var field in type.Fields)
{
Expand All @@ -98,7 +95,7 @@ private static HashSet<string> CollectTypesWithUploadScalar(
}
}
}
else if (namedInputType is ScalarType { Name: "Upload" })
else if (namedInputType is IScalarTypeDefinition { Name: "Upload" })
{
detected = true;
namesOfInputTypesWithUploadScalar.Add("Upload");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ private static void VisitFieldSelectionSet(

EnqueueFields(selectionSetVariants, backlog);

if (namedType is UnionType or InterfaceType)
if (namedType is IUnionTypeDefinition or IInterfaceTypeDefinition)
{
s_selectionAnalyzer.Analyze(
context,
fieldSelection,
selectionSetVariants);
}
else if (namedType is ObjectType)
else if (namedType is IObjectTypeDefinition)
{
s_selectionAnalyzer.Analyze(
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types.Mutable;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using static StrawberryShake.CodeGeneration.Utilities.OperationDocumentHelper;

Expand All @@ -8,11 +8,11 @@ namespace StrawberryShake.CodeGeneration.Analyzers;
public partial class DocumentAnalyzer
{
private readonly List<DocumentNode> _documents = [];
private Schema? _schema;
private MutableSchemaDefinition? _schema;

public static DocumentAnalyzer New() => new();

public DocumentAnalyzer SetSchema(Schema schema)
public DocumentAnalyzer SetSchema(MutableSchemaDefinition schema)
{
_schema = schema;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using HotChocolate.Types.Mutable;
using StrawberryShake.CodeGeneration.Extensions;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using Path = HotChocolate.Path;

namespace StrawberryShake.CodeGeneration.Analyzers;
Expand All @@ -17,7 +18,7 @@ public class DocumentAnalyzerContext : IDocumentAnalyzerContext
private readonly FieldCollector _fieldCollector;

public DocumentAnalyzerContext(
Schema schema,
MutableSchemaDefinition schema,
DocumentNode document)
{
Schema = schema ?? throw new ArgumentNullException(nameof(schema));
Expand All @@ -34,7 +35,7 @@ public DocumentAnalyzerContext(

public DocumentNode Document { get; }

public ObjectType OperationType { get; }
public IObjectTypeDefinition OperationType { get; }

public OperationDefinitionNode OperationDefinition { get; }

Expand Down Expand Up @@ -99,16 +100,16 @@ public void RegisterModel(string name, ITypeModel typeModel)

public void RegisterType(ITypeDefinition type)
{
if (type is ILeafType leafType && _typeModels.Values.All(x => x.Type.Name != type.Name))
if (type.IsLeafType() && _typeModels.Values.All(x => x.Type.Name != type.Name))
{
_typeModels.Add(
leafType.Name,
type.Name,
new LeafTypeModel(
leafType.Name,
leafType.Description,
leafType,
leafType.GetSerializationType(),
leafType.GetRuntimeType()));
type.Name,
type.Description,
type,
type.GetSerializationType(),
type.GetRuntimeType()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace StrawberryShake.CodeGeneration.Analyzers;

internal sealed class EnumTypeUsageAnalyzer(Schema schema) : SyntaxWalker<object?>
internal sealed class EnumTypeUsageAnalyzer(ISchemaDefinition schema) : SyntaxWalker<object?>
{
private readonly HashSet<EnumType> _enumTypes = [];
private readonly HashSet<IEnumTypeDefinition> _enumTypes = [];
private readonly HashSet<IInputType> _visitedTypes = [];
private readonly Stack<IType> _typeContext = new();

public ISet<EnumType> EnumTypes => _enumTypes;
public ISet<IEnumTypeDefinition> EnumTypes => _enumTypes;

public void Analyze(DocumentNode document)
{
Expand Down Expand Up @@ -126,11 +126,11 @@ private void VisitInputType(IInputType type)
type = innerType;
continue;

case InputObjectType inputObjectType:
case IInputObjectTypeDefinition inputObjectType:
VisitInputObjectType(inputObjectType);
break;

case EnumType enumType:
case IEnumTypeDefinition enumType:
_enumTypes.Add(enumType);
break;
}
Expand All @@ -140,7 +140,7 @@ private void VisitInputType(IInputType type)
}
}

private void VisitInputObjectType(InputObjectType type)
private void VisitInputObjectType(IInputObjectTypeDefinition type)
{
foreach (var field in type.Fields)
{
Expand Down
Loading
Loading