-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced HotChocolate.Data.AutoMapper (#4563)
- Loading branch information
1 parent
1413abe
commit 3217ebd
Showing
23 changed files
with
1,061 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/HotChocolate/Data/src/AutoMapper/AutoMapperQueryableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using HotChocolate.Data.Projections.Expressions; | ||
using HotChocolate.Resolvers; | ||
using static HotChocolate.Data.Projections.Expressions.QueryableProjectionProvider; | ||
|
||
namespace HotChocolate.Data; | ||
|
||
/// <summary> | ||
/// Common extensions for automapper and <see cref="IQueryable{T}"/> | ||
/// </summary> | ||
public static class AutoMapperQueryableExtensions | ||
{ | ||
/// <summary> | ||
/// Extension method to project from a queryable using the <see cref="IResolverContext"/> | ||
/// to project <typeparamref name="TSource"/> into <typeparamref name="TResult"/> based on | ||
/// the GraphQL selection. | ||
/// </summary> | ||
/// <param name="queryable">The Queryable that holds the selection</param> | ||
/// <param name="context">The resolver context of the resolver</param> | ||
/// <typeparam name="TSource">The source type</typeparam> | ||
/// <typeparam name="TResult">The result type</typeparam> | ||
/// <returns>The projected queryable</returns> | ||
public static IQueryable<TResult> ProjectTo<TSource, TResult>( | ||
this IQueryable<TSource> queryable, | ||
IResolverContext context) | ||
{ | ||
IMapper mapper = context.Service<IMapper>(); | ||
|
||
// ensure projections are only applied once | ||
context.LocalContextData = context.LocalContextData.SetItem(SkipProjectionKey, true); | ||
|
||
QueryableProjectionContext visitorContext = | ||
new(context, context.ObjectType, context.Selection.Field.Type.UnwrapRuntimeType()); | ||
|
||
QueryableProjectionVisitor.Default.Visit(visitorContext); | ||
|
||
Expression<Func<TResult, object>> projection = visitorContext.Project<TResult, object>(); | ||
|
||
return queryable.ProjectTo(mapper.ConfigurationProvider, projection); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/HotChocolate/Data/src/AutoMapper/HotChocolate.Data.AutoMapper.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>HotChocolate.Data.AutoMapper</PackageId> | ||
<AssemblyName>HotChocolate.Data.AutoMapper</AssemblyName> | ||
<RootNamespace>HotChocolate.Data</RootNamespace> | ||
<NoWarn>$(NoWarn);CA1062</NoWarn> | ||
<TargetFrameworks Condition="'$(IsMacOsArm)' != 'true'">net6.0; net5.0; netstandard2.1</TargetFrameworks> | ||
<TargetFrameworks Condition="'$(IsMacOsArm)' == 'true'">net6.0</TargetFrameworks> | ||
<Description>Contains extensions for easier integration of AutoMapper into HotChocolate</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" /> | ||
<ProjectReference Include="..\Data\HotChocolate.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="10.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="$(MSBuildThisFileDirectory)..\MSBuild\HotChocolate.Data.props" Pack="true" PackagePath="build/HotChocolate.Data.EntityFramework.props" Visible="false" /> | ||
<None Include="$(MSBuildThisFileDirectory)..\MSBuild\HotChocolate.Data.targets" Pack="true" PackagePath="build/HotChocolate.Data.EntityFramework.targets" Visible="false" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.