Skip to content

Commit 5361656

Browse files
committed
support scalar fields with args
1 parent f91cb2b commit 5361656

6 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/dotnet-gqlgen/SchemaInfo.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,7 @@ public string DotNetTypeSingle
172172
}
173173
}
174174

175-
public bool ShouldBeProperty
176-
{
177-
get
178-
{
179-
return (Args.Count == 0 && !schemaInfo.Types.ContainsKey(TypeName) && !schemaInfo.Inputs.ContainsKey(TypeName)) || schemaInfo.Scalars.Contains(TypeName);
180-
}
181-
}
175+
public bool ShouldBeProperty => Args.Count == 0 && IsScalar;
182176

183177
/// <summary>
184178
/// Outputs the method signature with all arguments and a object selection argument if applicable

src/dotnet-gqlgen/dotnet-gqlgen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<RootNamespace>dotnet_gqlgen</RootNamespace>
66
<PreserveCompilationContext>true</PreserveCompilationContext>
7-
<PackageVersion>0.7.5</PackageVersion>
7+
<PackageVersion>0.7.6</PackageVersion>
88
</PropertyGroup>
99

1010
<PropertyGroup>

src/tests/DotNetGraphQLQueryGen.Tests/Generated/GeneratedTypes.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ public interface Movie
231231
/// </summary>
232232
[GqlFieldName("type")]
233233
int? Type { get; }
234+
/// <summary>
235+
/// External identifier by name
236+
/// </summary>
237+
[GqlFieldName("externalId")]
238+
string ExternalId(string name);
234239
}
235240
public interface Actor
236241
{

src/tests/DotNetGraphQLQueryGen.Tests/TestMakeQuery.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ public void TestComplexValueArg()
179179
}}", query.Query, ignoreLineEndingDifferences: true);
180180
}
181181

182+
[Fact]
183+
public void ScalarFieldWithArgOnEntityType()
184+
{
185+
var client = new TestClient();
186+
var query = client.MakeQuery(q => new {
187+
Movie = q.Movie(1, m => new {
188+
ExternalId = m.ExternalId("mySystem"),
189+
}),
190+
});
191+
Assert.Equal($@"query TestQuery {{
192+
Movie: movie(id: 1) {{
193+
ExternalId: externalId(name: ""mySystem"")
194+
}}
195+
}}", query.Query, ignoreLineEndingDifferences: true);
196+
}
197+
182198
[Fact]
183199
public void TestErrorOnInvalidPropertySelection()
184200
{

src/tests/DotNetGraphQLQueryGen.Tests/VisitorTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,21 @@ public void TestSchemaTypeDef()
7575

7676
var typeDef = results.Types["Movie"];
7777
Assert.Equal("This is a movie entity", typeDef.Description);
78-
Assert.Equal(10, typeDef.Fields.Count);
78+
Assert.Equal(11, typeDef.Fields.Count);
7979
Assert.Equal("id", typeDef.Fields.ElementAt(0).Name);
8080
Assert.Equal("String", typeDef.Fields.ElementAt(1).TypeName);
8181
Assert.False(typeDef.Fields.ElementAt(1).IsArray);
8282
Assert.Equal("actors", typeDef.Fields.ElementAt(4).Name);
8383
Assert.Equal("Person", typeDef.Fields.ElementAt(4).TypeName);
8484
Assert.True(typeDef.Fields.ElementAt(4).IsArray);
85+
86+
var externalIdField = typeDef.Fields.First(f => f.Name == "externalId");
87+
Assert.Equal("String", externalIdField.TypeName);
88+
Assert.Single(externalIdField.Args);
89+
Assert.Equal("name", externalIdField.Args.First().Name);
90+
Assert.True(externalIdField.Args.First().Required);
91+
Assert.False(externalIdField.ShouldBeProperty);
92+
Assert.True(externalIdField.IsScalar);
8593
}
8694
}
8795
}

src/tests/DotNetGraphQLQueryGen.Tests/schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ type Movie {
6767
rating: Float
6868
"Just testing using gql schema keywords here"
6969
type: Int
70+
"External identifier by name"
71+
externalId(name: String!): String
7072
}
7173

7274
type Actor {

0 commit comments

Comments
 (0)