Skip to content

Commit 60d3c01

Browse files
committed
support Add a property name to exclude from mapping
1 parent 6882bb4 commit 60d3c01

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/GraphQL.EntityFramework/Mapping/Mapper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
public static class Mapper<TDbContext>
44
where TDbContext : DbContext
55
{
6+
static List<Func<string, bool>> ignoredNames = [];
7+
8+
/// <summary>
9+
/// Add a property name to exclude from mapping.
10+
/// </summary>
11+
public static void AddIgnoredName(Func<string, bool> ignore) =>
12+
ignoredNames.Add(ignore);
13+
14+
/// <summary>
15+
/// Add a property name to exclude from mapping.
16+
/// </summary>
17+
public static void AddIgnoredName(string name) =>
18+
ignoredNames.Add(_=> string.Equals(_, name, StringComparison.OrdinalIgnoreCase));
19+
620
static HashSet<Type> ignoredTypes = [];
721

822
/// <summary>
@@ -177,6 +191,11 @@ static bool ShouldIgnore(IComplexGraphType graphType, string name, Type property
177191
}
178192
}
179193

194+
if (ignoredNames.Any(_ => _.Invoke(name)))
195+
{
196+
return true;
197+
}
198+
180199
if (FieldExists(graphType, name))
181200
{
182201
return true;

src/Tests/Mapping/MappingParent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ public class MappingParent
55
public string? Property { get; set; }
66
public IList<MappingChild> Children { get; set; } = [];
77
public IList<string> JsonProperty { get; set; } = [];
8+
public string? IgnoreByName { get; set; }
89
}

src/Tests/Mapping/MappingTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ public class MappingTests
44
{
55
static SqlInstance<MappingContext> sqlInstance;
66

7-
static MappingTests() =>
7+
static MappingTests()
8+
{
9+
Mapper<MappingContext>.AddIgnoredName("IgnoreByName");
810
sqlInstance = new(builder =>
911
{
1012
builder.ConfigureWarnings(_ =>
@@ -15,6 +17,7 @@ static MappingTests() =>
1517
CoreEventId.CollectionWithoutComparer));
1618
return new(builder.Options);
1719
});
20+
}
1821

1922
[Fact]
2023
public async Task SchemaPrint()

0 commit comments

Comments
 (0)