【问题标题】:Can I use graphql-dotnet without Entity Framework?我可以在没有实体框架的情况下使用 graphql-dotnet 吗?
【发布时间】:2020-11-13 19:59:41
【问题描述】:

我可以在没有实体框架的情况下使用 graphql-dotnet 吗?

我目前正在查看此存储库https://github.com/mmacneil/fullstack-jobs,并注意到它正在使用 EF。我在互联网上到处寻找示例,我只能找到带有 EF 的 graphql-dotnet 实现。

我想在不适合 EF 的现有数据库结构中使用 graphql-dotnet。编写直接 SQL 是我希望应用程序工作的方式。有人有例子吗?

我不明白 graphql-dotnet 如何与我链接的存储库中的数据库联系在一起

【问题讨论】:

  • 你为 ADO.NET 做过这个吗?如果是的话,你能提供接口的实现吗?

标签: c# .net graphql graphql-dotnet


【解决方案1】:

您可以在没有 EF 的情况下使用 GraphQL。我对你的 github 链接和我的发现做了一些调查。

首先你应该检查这些接口:

IRepository

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
        }
    }

JobRepository

 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 字段。

【讨论】:

  • 哇,非常感谢您抽出宝贵时间来回答:)这是一个很大的帮助!
【解决方案2】:

我可以告诉你,在没有 EF 的情况下使用 graphql-dotnet 是完全可行的。问题是,建立一个示例并非易事,需要一些基础设施。我建议您自己尝试实现,当您在自己切换 EF 代码时遇到困难时,您可以发布一个问题,说明您正在尝试做什么。

实现的主要区别在于您在架构中定义的每个字段中如何处理解析器。您可以以任何您希望的方式访问数据。您只需要确保您在方法中提供的结果与您在 graphql 模式对象中定义的类型相匹配。

如果没有至少一些可以构建的初始框架,我真的无法为您提供更多细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多