您可以在没有 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);
}
IJobRepository
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 字段。