如何解决在asp.net core中拆分热巧克力qraphql中的查询类型
这是我在启动 .cs 中的配置
.AddGraphQLServer()
.AddQueryType<Query>()
.AddType<CustomerQuery>()
.AddType<ProductQuery>()
.AddType<CustomerType>()
.AddType<ProductType>()
.AddFiltering()
.AddSorting();
我有两个单独的查询类
[ExtendObjectType(Name = "Query")]
public class CustomerQuery
{
private readonly IRepository<Customer> customerRepository;
public CustomerQuery(IRepository<Customer> customerRepository)
{
this.customerRepository = customerRepository;
}
public IQueryable<Customer> customers() => customerRepository.Table.Where(i=>i.Active==true);
public Customer customer (string id) => customerRepository.GetById(Convert.ToInt16(id));
}
[ExtendObjectType(Name = "Query")]
public class ProductQuery
{
private readonly IRepository<Product> productRepository;
public ProductQuery(IRepository<Product> productRepository)
{
this.productRepository = productRepository;
}
public IQueryable<Product> Products() => productRepository.Table.AsQueryable();
}
我还有另外两个类来配置 ProductQuery 和 CustomerQuery
public class ProductQueryType:ObjectType<ProductQuery>
{
protected override void Configure(IObjectTypeDescriptor<ProductQuery> descriptor)
{
descriptor
.Field(f => f.Products())
.UsePaging()
.UseFiltering()
.UseSorting()
.Type<ProductType>();
}
}
public class CustomerQueryType:ObjectType<CustomerQuery>
{
protected override void Configure(IObjectTypeDescriptor<CustomerQuery> descriptor)
{
descriptor
.Field(f => f.customers())
.UsePaging()
.UseFiltering()
.UseSorting()
.Type<CustomerType>();
}
}
我无法在我的 startup.cs 中添加这两个配置,它使图形端点出现一些错误, 如何添加查询配置管道?
解决方法
老实说,您的代码中存在大量误解,因此,我决定完全重写它并为您提供一个可行的起点。
using System;
using System.Linq;
using HotChocolate;
using HotChocolate.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Ademchenko.GraphQLWorkshop
{
public interface IRepository<out T>
{
IQueryable<T> Table { get; }
T GetById(short id);
}
public class CustomerRepository : IRepository<Customer>
{
private readonly Customer[] _staticCustomers = {
new Customer {Active = true,FirstName = "FooCustomer",Id = 1},new Customer {Active = true,FirstName = "BarCustomer",Id = 2},};
public IQueryable<Customer> Table => _staticCustomers.AsQueryable();
public Customer GetById(short id) => _staticCustomers.Single(c => c.Id == id);
}
public class ProductRepository : IRepository<Product>
{
private readonly Product[] _staticProducts = {
new Product {Name = "FooProduct",Id = 3},new Product {Name = "BarOfHotChocolateProduct",Id = 4},};
public IQueryable<Product> Table => _staticProducts.AsQueryable();
public Product GetById(short id) => _staticProducts.Single(c => c.Id == id);
}
public class CustomerQuery
{
private readonly IRepository<Customer> _customerRepository;
public CustomerQuery([Service] IRepository<Customer> customerRepository) => _customerRepository = customerRepository;
public IQueryable<Customer> Customers() => _customerRepository.Table.Where(i => i.Active);
public Customer Customer(Int16 id) => _customerRepository.GetById(id);
}
public class CustomerQueryType : ObjectTypeExtension<CustomerQuery>
{
protected override void Configure(IObjectTypeDescriptor<CustomerQuery> descriptor) =>
descriptor.Name("Query").Field(f => f.Customers()).UsePaging().UseFiltering().UseSorting();
}
public class ProductQuery
{
private readonly IRepository<Product> _productRepository;
public ProductQuery([Service] IRepository<Product> productRepository) => _productRepository = productRepository;
public IQueryable<Product> Products() => _productRepository.Table.AsQueryable();
}
public class ProductQueryType : ObjectTypeExtension<ProductQuery>
{
protected override void Configure(IObjectTypeDescriptor<ProductQuery> descriptor) =>
descriptor.Name("Query").Field(f => f.Products()).UsePaging().UseFiltering().UseSorting();
}
public class Customer
{
public Int16 Id { get; set; }
public string FirstName { get; set; }
public bool Active { get; set; }
}
public class Product
{
public Int16 Id { get; set; }
public string Name { get; set; }
}
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<IRepository<Customer>,CustomerRepository>();
services.AddSingleton<IRepository<Product>,ProductRepository>();
services
.AddGraphQLServer()
.AddQueryType(t => t.Name("Query"))
.AddType<ProductQueryType>()
.AddType<CustomerQueryType>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env) =>
app.UseRouting().UseEndpoints(endpoints => endpoints.MapGraphQL());
}
}
如果您设法编译(使用 HotChocolate v.11 库)并运行它,您将能够打开 Balana Cake Pop 并运行以下查询:
{
products(first: 1,order_by: {id: ASC},where: {name_contains: "Foo"})
{
pageInfo
{
startCursor
hasNextPage
}
nodes
{
id
name
}
}
customers(first: 1,where: {firstName: "BarCustomer"})
{
pageInfo
{
startCursor
hasPreviousPage
hasNextPage
}
nodes
{
id
firstName
active
}
}
customer(id: 1)
{
id
firstName
}
}
系统将返回以下答案:
{
"data": {
"products": {
"pageInfo": {
"startCursor": "MA==","hasNextPage": false
},"nodes": [
{
"id": 3,"name": "FooProduct"
}
]
},"customers": {
"pageInfo": {
"startCursor": "MA==","hasPreviousPage": false,"nodes": [
{
"id": 2,"firstName": "BarCustomer","active": true
}
]
},"customer": {
"id": 1,"firstName": "FooCustomer"
}
}
}
希望,这会有所帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。