Skip to content

Commit

Permalink
Reworked the invalid GraphQL name error message. (#7939)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jan 22, 2025
1 parent a329840 commit 4eb11c3
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
For more details look at the `Errors` property.

1. The specified name is not a valid GraphQL name. (Parameter 'definition.Name') (HotChocolate.Data.Sorting.SortConventionTests.FooSortType)
1. `` is not a valid GraphQL name.
https://spec.graphql.org/October2021/#sec-Names
(Parameter 'definition.Name') (HotChocolate.Data.Sorting.SortConventionTests.FooSortType)
9 changes: 9 additions & 0 deletions src/HotChocolate/Primitives/HotChocolate.Primitives.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{42689032-400
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Primitives", "src\Primitives\HotChocolate.Primitives.csproj", "{F5CEC44C-8AB9-4639-9B38-4F523E3ABF2A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B598E71D-74BD-4055-A2F7-10B7DB892ECB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Primitives.Tests", "test\Primitives.Tests\HotChocolate.Primitives.Tests.csproj", "{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,8 +24,13 @@ Global
{F5CEC44C-8AB9-4639-9B38-4F523E3ABF2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5CEC44C-8AB9-4639-9B38-4F523E3ABF2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5CEC44C-8AB9-4639-9B38-4F523E3ABF2A}.Release|Any CPU.Build.0 = Release|Any CPU
{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F5CEC44C-8AB9-4639-9B38-4F523E3ABF2A} = {42689032-4007-47A4-B045-12D2681BF2CF}
{F7643027-2CEF-4F06-8DFA-46FFA8C17C4F} = {B598E71D-74BD-4055-A2F7-10B7DB892ECB}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/HotChocolate/Primitives/src/Primitives/NameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string EnsureGraphQLName(
return name!;
}

throw new ArgumentException(NameUtils_InvalidGraphQLName, argumentName);
throw new ArgumentException(string.Format(NameUtils_EnsureGraphQLName_InvalidName, name), argumentName);
}

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NameUtils_InvalidGraphQLName" xml:space="preserve">
<value>The specified name is not a valid GraphQL name.</value>
</data>
<data name="ThrowHelper_SchemaCoordinate_ArgumentNameCannotBeSetWithoutMemberName" xml:space="preserve">
<value>A argument name without a member name is only allowed on directive coordinates.</value>
</data>
<data name="ThrowHelper_SchemaCoordinate_MemberNameCannotBeSetOnADirectiveCoordinate" xml:space="preserve">
<value>A directive cannot contain a member name.</value>
</data>
<data name="DirectiveTypeFactory_LocationNotSupported" xml:space="preserve">
<value>The specified location `{0}` is not supported.</value>
</data>
<data name="NameUtils_EnsureGraphQLName_InvalidName" xml:space="preserve">
<value>`{0}` is not a valid GraphQL name.
https://spec.graphql.org/October2021/#sec-Names
</value>
</data>
</root>
4 changes: 2 additions & 2 deletions src/HotChocolate/Primitives/test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" />

<PropertyGroup>
<TargetFrameworks>$(TestTargetFrameworks)</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>
Expand All @@ -12,7 +11,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble\CookieCrumble.csproj" />
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.Xunit\CookieCrumble.Xunit.csproj" />
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.Analyzers\CookieCrumble.Analyzers.csproj" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">

<PropertyGroup>
<AssemblyName>HotChocolate.Primitives.Tests</AssemblyName>
<RootNamespace>HotChocolate.Primitives</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Primitives\HotChocolate.Primitives.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using HotChocolate.Utilities;

namespace HotChocolate.Primitives;

public class NameUtilsTests
{
[Theory]
[InlineData("1Bar")]
[InlineData("$Bar")]
[InlineData("1_Bar")]
[InlineData("B/ar")]
[InlineData("B+ar")]
public void InvalidName(string name)
{
var message = Assert.Throws<ArgumentException>(() => name.EnsureGraphQLName()).Message;
Assert.Equal(
$"`{name}` is not a valid GraphQL name.{Environment.NewLine}"
+ $"https://spec.graphql.org/October2021/#sec-Names{Environment.NewLine}"
+ $" (Parameter 'name')",
message);
}

[Theory]
[InlineData("_1Bar")]
[InlineData("Bar")]
[InlineData("_Bar")]
[InlineData("Bar123")]
[InlineData("Bar_123")]
[InlineData("ABCDEFGHIJKLMNOPQRSTUVWXYZ")]
[InlineData("abcdefghijklmnopqrstuvwxyz")]
[InlineData("_1234567890")]
public void ValidName(string name)
{
name.EnsureGraphQLName();
}
}

0 comments on commit 4eb11c3

Please sign in to comment.