Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added entity framework helpers. (#2435)
- Loading branch information
1 parent
6d79503
commit 2a0605a
Showing
17 changed files
with
507 additions
and
1 deletion.
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
60 changes: 60 additions & 0 deletions
60
...ate/Data/src/EntityFramework/Extensions/EntityFrameworkObjectFieldDescriptorExtensions.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,60 @@ | ||
using System; | ||
using System.Linq; | ||
using HotChocolate.Data; | ||
using HotChocolate.Resolvers; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using static HotChocolate.Resolvers.FieldClassMiddlewareFactory; | ||
|
||
namespace HotChocolate.Types | ||
{ | ||
public static class EntityFrameworkObjectFieldDescriptorExtensions | ||
{ | ||
public static IObjectFieldDescriptor UseDbContext<TDbContext>( | ||
this IObjectFieldDescriptor descriptor) | ||
where TDbContext : DbContext | ||
{ | ||
string scopedServiceName = typeof(TDbContext).FullName ?? typeof(TDbContext).Name; | ||
FieldMiddleware placeholder = next => context => throw new NotSupportedException(); | ||
|
||
descriptor | ||
.Use(next => async context => | ||
{ | ||
await using TDbContext dbContext = context.Services | ||
.GetRequiredService<IDbContextFactory<TDbContext>>() | ||
.CreateDbContext(); | ||
try | ||
{ | ||
context.SetLocalValue(scopedServiceName, dbContext); | ||
await next(context).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
context.RemoveLocalValue(scopedServiceName); | ||
} | ||
}) | ||
.Use(placeholder) | ||
.Extend() | ||
.OnBeforeNaming((c, d) => | ||
{ | ||
if (d.ResultType is not null && | ||
typeof(IQueryable).IsAssignableFrom(d.ResultType) && | ||
d.ResultType.IsGenericType) | ||
{ | ||
Type entity = d.ResultType.GenericTypeArguments[0]; | ||
Type middleware = typeof(ToListMiddleware<>).MakeGenericType(entity); | ||
var index = d.MiddlewareComponents.IndexOf(placeholder); | ||
d.MiddlewareComponents[index] = Create(middleware); | ||
} | ||
else | ||
{ | ||
d.MiddlewareComponents.Remove(placeholder); | ||
} | ||
}); | ||
|
||
return descriptor; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/HotChocolate/Data/src/EntityFramework/HotChocolate.Data.EntityFramework.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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>HotChocolate.Data.EntityFramework</PackageId> | ||
<AssemblyName>HotChocolate.Data</AssemblyName> | ||
<RootNamespace>HotChocolate.Data</RootNamespace> | ||
<NoWarn>$(NoWarn);CA1062</NoWarn> | ||
<TargetFrameworks>netstandard2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0-rc.2.20475.6" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Update="Properties\EntityFrameworkResources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>EntityFrameworkResources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="Properties\EntityFrameworkResources.Designer.cs"> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>EntityFrameworkResources.resx</DependentUpon> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
</Project> |
54 changes: 54 additions & 0 deletions
54
src/HotChocolate/Data/src/EntityFramework/Properties/EntityFrameworkResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
src/HotChocolate/Data/src/EntityFramework/Properties/EntityFrameworkResources.resx
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,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<root> | ||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||
<xsd:element name="root" msdata:IsDataSet="true"> | ||
|
||
</xsd:element> | ||
</xsd:schema> | ||
<resheader name="resmimetype"> | ||
<value>text/microsoft-resx</value> | ||
</resheader> | ||
<resheader name="version"> | ||
<value>1.3</value> | ||
</resheader> | ||
<resheader name="reader"> | ||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<resheader name="writer"> | ||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<data name="UseDbContextAttribute_OnConfigure_NoDbContextType" xml:space="preserve"> | ||
<value>The `{0}` must inherit from `Microsoft.EntityFrameworkCore`.</value> | ||
</data> | ||
</root> |
29 changes: 29 additions & 0 deletions
29
src/HotChocolate/Data/src/EntityFramework/ToListMiddleware.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,29 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Resolvers; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotChocolate.Data | ||
{ | ||
internal class ToListMiddleware<TEntity> | ||
{ | ||
private readonly FieldDelegate _next; | ||
|
||
public ToListMiddleware(FieldDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async ValueTask InvokeAsync(IMiddlewareContext context) | ||
{ | ||
await _next(context).ConfigureAwait(false); | ||
|
||
if (context.Result is IQueryable<TEntity> queryable) | ||
{ | ||
context.Result = await queryable | ||
.ToListAsync(context.RequestAborted) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/HotChocolate/Data/src/EntityFramework/UseDbContextAttribute.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,44 @@ | ||
using System; | ||
using System.Reflection; | ||
using HotChocolate.Data.Properties; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
using Microsoft.EntityFrameworkCore; | ||
using static HotChocolate.Data.Properties.EntityFrameworkResources; | ||
|
||
namespace HotChocolate.Data | ||
{ | ||
public class UseDbContextAttribute : ObjectFieldDescriptorAttribute | ||
{ | ||
private static readonly MethodInfo _useDbContext = | ||
typeof(EntityFrameworkObjectFieldDescriptorExtensions) | ||
.GetMethod(nameof(EntityFrameworkObjectFieldDescriptorExtensions.UseDbContext), | ||
BindingFlags.Public | BindingFlags.Static)!; | ||
|
||
private readonly Type _dbContext; | ||
|
||
public UseDbContextAttribute(Type dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
public override void OnConfigure( | ||
IDescriptorContext context, | ||
IObjectFieldDescriptor descriptor, | ||
MemberInfo member) | ||
{ | ||
if (!typeof(DbContext).IsAssignableFrom(_dbContext)) | ||
{ | ||
throw new SchemaException( | ||
SchemaErrorBuilder.New() | ||
.SetMessage( | ||
UseDbContextAttribute_OnConfigure_NoDbContextType, | ||
_dbContext.FullName ?? _dbContext.Name) | ||
.SetExtension(nameof(member), member) | ||
.Build()); | ||
} | ||
|
||
_useDbContext.MakeGenericMethod(_dbContext).Invoke(null, new object?[] { descriptor }); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/HotChocolate/Data/test/Data.EntityFramework.Tests/Author.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,19 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace HotChocolate.Data | ||
{ | ||
public class Author | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
public string? Name { get; set; } | ||
|
||
public virtual ICollection<Book> Books { get; set; } = | ||
new List<Book>(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/HotChocolate/Data/test/Data.EntityFramework.Tests/Book.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,20 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace HotChocolate.Data | ||
{ | ||
public class Book | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
public int AuthorId { get; set; } | ||
|
||
[Required] | ||
public string? Title { get; set; } | ||
|
||
public virtual Author? Author { get; set; } | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/HotChocolate/Data/test/Data.EntityFramework.Tests/BookContext.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,23 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotChocolate.Data | ||
{ | ||
public class BookContext : DbContext | ||
{ | ||
public BookContext(DbContextOptions options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Book> Books { get; set; } = default!; | ||
|
||
public DbSet<Author> Authors { get; set; } = default!; | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Author>() | ||
.HasMany(t => t.Books) | ||
.WithOne(t => t.Author!) | ||
.HasForeignKey(t => t.AuthorId); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...olate/Data/test/Data.EntityFramework.Tests/HotChocolate.Data.EntityFramework.Tests.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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>HotChocolate.Data</RootNamespace> | ||
<TargetFrameworks>net5.0; netcoreapp3.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Core\test\Types.Tests\HotChocolate.Types.Tests.csproj" /> | ||
<ProjectReference Include="..\..\src\EntityFramework\HotChocolate.Data.EntityFramework.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0-rc.2.20475.6" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Update="Properties\EfResources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>EfResources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.