我可以使用没有实体框架的graphql-dotnet吗?
我目前正在查看这个存储库https://github.com/mmacneil/fullstack-jobs,并注意到它使用的是EF。我在互联网上到处寻找例子,我只能找到使用EF的graphql-dotnet实现。
我想在EF不合适的现有数据库结构中使用graphql-dotnet。直接编写SQL是我希望应用程序工作的方式。有谁有什么例子吗?
我不明白graphql-dotnet如何与我所链接的存储库中的数据库绑定在一起
发布于 2020-07-24 21:05:43
您可以使用不带EF的GraphQL。我对你的github链接和我发现的东西做了一些调查。
首先,您应该检查这些接口:
public interface IRepository<T>
{
Task<T> GetById(int id);
Task<List<T>> ListAll();
Task<T> GetSingleBySpec(ISpecification<T> spec);
Task<List<T>> List(ISpecification<T> spec);
Task<T> Add(T entity);
Task Update(T entity);
Task Delete(T entity);
}public interface IJobRepository : IRepository<Job>
{
}这些接口(主要是IRepository)定义了如何使用数据库的约定。接下来,您需要检查这些类:EfRepository,我省略了一些实现细节,因为它们对理解并不重要
public abstract class EfRepository<T> : IRepository<T> where T : class
{
protected readonly AppDbContext _appDbContext;
protected EfRepository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public virtual async Task<T> GetById(int id)
{
return await _appDbContext.Set<T>().FindAsync(id);
}
public async Task<List<T>> ListAll()
{
return await _appDbContext.Set<T>().ToListAsync();
}
public async Task<T> GetSingleBySpec(ISpecification<T> spec)
{
//implementation omitted
}
public async Task<List<T>> List(ISpecification<T> spec)
{
//implementation omitted
}
public async Task<T> Add(T entity)
{
//implementation omitted
}
public async Task Update(T entity)
{
//implementation omitted
}
public async Task Delete(T entity)
{
//implementation omitted
}
} public sealed class JobRepository : EfRepository<Job>, IJobRepository
{
public JobRepository(AppDbContext appDbContext) : base(appDbContext)
{
}
}这些类包含上述接口的实现,EfRepository包含使用db的所有代码。在这种情况下,我们有EF,但它可以使用您想要的任何东西,任何数据库或集合等。
所以下一步我们需要的是ContextServiceLocator (如果更准确的说,我们这里需要JobRepository字段)
public class ContextServiceLocator
{
public IJobRepository JobRepository => _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IJobRepository>();
public IHumanizer Humanizer => _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IHumanizer>();
private readonly IHttpContextAccessor _httpContextAccessor;
public ContextServiceLocator(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
}最后但并非最不重要的是FullStackJobsQuery
public class FullStackJobsQuery : ObjectGraphType
{
public FullStackJobsQuery(ContextServiceLocator contextServiceLocator)
{
FieldAsync<JobType>("job",
arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
resolve: async context => await contextServiceLocator.JobRepository.GetSingleBySpec(new JobSpecification(j => j.Id == context.GetArgument<int>("id", default))));
FieldAsync<ListGraphType<JobSummaryType>>("employerJobs",
resolve: async context =>
{
// Extract the user id from the name claim to fetch the target employer's jobs
var jobs = await contextServiceLocator.JobRepository.List(new JobSpecification(j => j.Employer.Id == context.GetUserId()));
return jobs.OrderByDescending(j => j.Modified);
});
FieldAsync<ListGraphType<JobSummaryType>>("publicJobs",
resolve: async context =>
{
// Fetch published Jobs from all employers
var jobs = await contextServiceLocator.JobRepository.List(new JobSpecification(j => j.Status == Status.Published));
return jobs.OrderByDescending(j => j.Modified);
});
}
}在这个类中,你可以看到如何使用contextServiceLocator.JobRepository来解析GraphQL域。
发布于 2020-07-24 09:58:53
我可以告诉你,在没有EF的情况下使用graphql-dotnet是完全可行的。问题是,建立一个示例并不是微不足道的,并且需要一些基础设施。我建议您尝试自己的实现,当您遇到用自己的代码切换出EF代码的困难时,您可以发布一个问题,其中包含您正在尝试做的具体内容。
实现上的主要区别将在于您如何处理您在架构中定义的每个字段中的解析器。您可以以任何您想要的方式访问数据。您只需要确保您在方法中提供的结果与您在graphql模式对象中定义的类型相匹配。
如果没有一些初步的框架可供构建,我真的不能给你更多的细节。
https://stackoverflow.com/questions/63065328
复制相似问题