Skip to content

Commit 46bbc02

Browse files
AddArguments<TArguments>(TArguments arguments) supports inner object (#28)
Co-authored-by: Carlos H. Carniatto <[email protected]> Co-authored-by: Charles de Vandière <[email protected]>
1 parent 32e4b78 commit 46bbc02

File tree

14 files changed

+349
-161
lines changed

14 files changed

+349
-161
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"dotnet-reportgenerator-globaltool": {
12-
"version": "4.8.1",
12+
"version": "5.1.3",
1313
"commands": [
1414
"reportgenerator"
1515
]

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "coreclr",
77
"request": "launch",
88
"preLaunchTask": "build",
9-
"program": "${workspaceFolder}/sample/Pokedex/bin/Debug/netcoreapp3.1/Pokedex.dll",
9+
"program": "${workspaceFolder}/sample/Pokedex/bin/Debug/net6.0/Pokedex.dll",
1010
"args": [
1111
"pikachu"
1212
],

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Charles de Vandière
3+
Copyright (c) 2022 Charles de Vandière
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A tool to build GraphQL query from a C# model.
1111

1212
See complete documentation [here](https://charlesdevandiere.github.io/graphql-query-builder-dotnet/)
1313

14-
See sample [here](sample/Pokedex)
14+
See sample [here](https://github.com/charlesdevandiere/graphql-query-builder-dotnet/tree/master/sample/Pokedex)
1515

1616
## Install
1717

azure-pipelines.yml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ variables:
66
buildConfiguration: Release
77

88
trigger:
9-
batch: true
9+
batch: 'true'
1010
branches:
1111
include:
1212
- master
@@ -16,42 +16,39 @@ trigger:
1616
- README.md
1717

1818
steps:
19-
- checkout: self
20-
clean: true
21-
persistCredentials: true
22-
2319
- task: UseGitVersion@5
2420
displayName: GitVersion
2521
inputs:
2622
versionSpec: '5.x'
2723

2824
- script: dotnet tool restore
29-
displayName: 'dotnet tool restore'
25+
displayName: 'Restore tools'
26+
27+
- script: dotnet build -c $(buildConfiguration)
28+
displayName: 'Build'
3029

31-
- script: dotnet test -c $(buildConfiguration) --logger xunit -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:Exclude="[*]Shared.Models.*"
32-
displayName: 'dotnet test'
30+
- script: dotnet test --no-build -c $(buildConfiguration) --logger xunit -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:Exclude="[*]Shared.Models.*"
31+
displayName: 'Test'
3332

34-
- script: dotnet pack ./src/GraphQL.Query.Builder/GraphQL.Query.Builder.csproj -c $(buildConfiguration) -o $(Build.ArtifactStagingDirectory)/out -p:Version=$(GitVersion.SemVer) --include-symbols
35-
displayName: 'dotnet pack'
33+
- script: dotnet pack ./src/GraphQL.Query.Builder/GraphQL.Query.Builder.csproj --no-build -c $(buildConfiguration) -o $(Build.ArtifactStagingDirectory)/out -p:Version=$(GitVersion.SemVer)
34+
displayName: 'Pack'
3635

3736
- task: PublishTestResults@2
38-
displayName: 'publish test results'
37+
displayName: 'Publish test results'
3938
inputs:
4039
testRunner: xUnit
4140
testResultsFiles: '**/TestResults.xml'
4241

4342
- script: dotnet tool run reportgenerator -reports:**/coverage.cobertura.xml -targetdir:tests/coverage -reportTypes:Cobertura
44-
displayName: 'generate code coverage report'
43+
displayName: 'Generate code coverage report'
4544

4645
- task: PublishCodeCoverageResults@1
47-
displayName: 'publish code coverage'
46+
displayName: 'Publish code coverage'
4847
inputs:
4948
codeCoverageTool: cobertura
5049
summaryFileLocation: '**/Cobertura.xml'
5150

52-
- task: PublishBuildArtifacts@1
53-
displayName: 'publish artifact'
51+
- publish: $(Build.ArtifactStagingDirectory)/out
5452
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
55-
inputs:
56-
PathtoPublish: $(Build.ArtifactStagingDirectory)/out
57-
ArtifactName: out
53+
artifact: out
54+
displayName: Publish artifact

src/GraphQL.Query.Builder/GraphQL.Query.Builder.csproj

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,42 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
55
<Version>0.0.0</Version>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
68
<Authors>Charles de Vandière</Authors>
9+
<Copyright>© 2022 Charles de Vandière</Copyright>
10+
<Product>GraphQL.Query.Builder</Product>
11+
<Title>GraphQL.Query.Builder</Title>
712
<Description>A tool to build GraphQL query from a C# model.</Description>
13+
<Summary>A tool to build GraphQL query from a C# model.</Summary>
14+
<PackageIcon>logo.png</PackageIcon>
15+
<PackageIconUrl>https://raw.githubusercontent.com/charlesdevandiere/graphql-query-builder-dotnet/master/logo.png</PackageIconUrl>
816
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17+
<PackageProjectUrl>https://charlesdevandiere.github.io/graphql-query-builder-dotnet</PackageProjectUrl>
18+
<PackageReadmeFile>README.md</PackageReadmeFile>
19+
<PackageTags>graphql</PackageTags>
20+
<RepositoryType>git</RepositoryType>
921
<RepositoryUrl>https://github.com/charlesdevandiere/graphql-query-builder-dotnet</RepositoryUrl>
10-
<PackageIconUrl>https://raw.githubusercontent.com/charlesdevandiere/graphql-query-builder-dotnet/master/logo.png</PackageIconUrl>
11-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
22+
23+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
24+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
25+
<IncludeSymbols>true</IncludeSymbols>
26+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1227
</PropertyGroup>
1328

29+
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
30+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<None Include="..\..\logo.png" Pack="true" PackagePath="/"/>
35+
<None Include="..\..\LICENSE" Pack="true" PackagePath="LICENSE" />
36+
<None Include="..\..\README.md" Pack="true" PackagePath="/" />
37+
</ItemGroup>
38+
1439
<ItemGroup>
1540
<PackageReference Include="Dawn.Guard" Version="1.12.0" />
1641
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
42+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
1743
</ItemGroup>
1844
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Reflection;
3+
using Dawn;
4+
using Newtonsoft.Json;
5+
6+
namespace GraphQL.Query.Builder
7+
{
8+
/// <summary>The query formatter class.</summary>
9+
internal static class PropertyNameFormatter
10+
{
11+
/// <summary>Tries to get property name from JSON property attribute or from optional formater.</summary>
12+
/// <param name="property">The property.</param>
13+
/// <param name="formatter">The formatter.</param>
14+
/// <returns>The property name.</returns>
15+
internal static string GetPropertyName(PropertyInfo property, Func<string, string> formatter = null)
16+
{
17+
Guard.Argument(property, nameof(property)).NotNull();
18+
19+
Attribute jsonAttribute = property.GetCustomAttribute(typeof(JsonPropertyAttribute));
20+
21+
if (jsonAttribute != null)
22+
{
23+
if (!string.IsNullOrEmpty((jsonAttribute as JsonPropertyAttribute).PropertyName))
24+
{
25+
return (jsonAttribute as JsonPropertyAttribute).PropertyName;
26+
}
27+
}
28+
29+
if (formatter != null)
30+
{
31+
return formatter.Invoke(property.Name);
32+
}
33+
34+
return property.Name;
35+
}
36+
}
37+
}

src/GraphQL.Query.Builder/QueryOf{T}.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Reflection;
56
using System.Runtime.CompilerServices;
67
using Dawn;
7-
using Newtonsoft.Json;
88

99
[assembly: InternalsVisibleTo("GraphQL.Query.Builder.UnitTests")]
1010
namespace GraphQL.Query.Builder
@@ -71,7 +71,7 @@ public IQuery<TSource> AddField<TProperty>(Expression<Func<TSource, TProperty>>
7171
Guard.Argument(selector, nameof(selector)).NotNull();
7272

7373
PropertyInfo property = GetPropertyInfo(selector);
74-
string name = GetPropertyName(property);
74+
string name = PropertyNameFormatter.GetPropertyName(property, this.options?.Formatter);
7575

7676
this.SelectList.Add(name);
7777

@@ -104,7 +104,7 @@ public IQuery<TSource> AddField<TSubSource>(
104104
Guard.Argument(build, nameof(build)).NotNull();
105105

106106
PropertyInfo property = GetPropertyInfo(selector);
107-
string name = GetPropertyName(property);
107+
string name = PropertyNameFormatter.GetPropertyName(property, this.options?.Formatter);
108108

109109
return AddField(name, build);
110110
}
@@ -123,7 +123,7 @@ public IQuery<TSource> AddField<TSubSource>(
123123
Guard.Argument(build, nameof(build)).NotNull();
124124

125125
PropertyInfo property = GetPropertyInfo(selector);
126-
string name = GetPropertyName(property);
126+
string name = PropertyNameFormatter.GetPropertyName(property, this.options?.Formatter);
127127

128128
return AddField(name, build);
129129
}
@@ -185,10 +185,16 @@ public IQuery<TSource> AddArguments<TArguments>(TArguments arguments) where TArg
185185
{
186186
Guard.Argument(arguments, nameof(arguments)).NotNull();
187187

188-
PropertyInfo[] properties = typeof(TArguments).GetProperties();
188+
IEnumerable<PropertyInfo> properties = arguments
189+
.GetType()
190+
.GetProperties()
191+
.Where(property => property.GetValue(arguments) != null)
192+
.OrderBy(property => property.Name);
189193
foreach (PropertyInfo property in properties)
190194
{
191-
this.Arguments.Add(this.GetPropertyName(property), property.GetValue(arguments));
195+
this.Arguments.Add(
196+
PropertyNameFormatter.GetPropertyName(property, this.options?.Formatter),
197+
property.GetValue(arguments));
192198
}
193199

194200
return this;
@@ -238,30 +244,5 @@ private static PropertyInfo GetPropertyInfo<TProperty>(Expression<Func<TSource,
238244

239245
return propertyInfo;
240246
}
241-
242-
/// <summary>Tries to get property name from JSON property attribute or from optional formater.</summary>
243-
/// <param name="property">The property.</param>
244-
/// <returns>The property name.</returns>
245-
private string GetPropertyName(PropertyInfo property)
246-
{
247-
Guard.Argument(property, nameof(property)).NotNull();
248-
249-
Attribute attribute = property.GetCustomAttribute(typeof(JsonPropertyAttribute));
250-
251-
if (attribute != null)
252-
{
253-
if (!string.IsNullOrEmpty((attribute as JsonPropertyAttribute).PropertyName))
254-
{
255-
return (attribute as JsonPropertyAttribute).PropertyName;
256-
}
257-
}
258-
259-
if (this.options?.Formatter != null)
260-
{
261-
return this.options.Formatter.Invoke(property.Name);
262-
}
263-
264-
return property.Name;
265-
}
266247
}
267248
}

0 commit comments

Comments
 (0)