【发布时间】:2020-11-19 13:27:19
【问题描述】:
我的开发环境
- 视觉工作室 2019
- dotnetcore 3.1 API
- GraphIQL 2.0.0
- GraphQL 2.4.0
我测试了 GraphQL。下图显示了我得到的错误。
我的 StartUp.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<miracleContext>(opt => opt.UseMySql(Configuration["ConnectionStrings:MiracleMariaDB"]));
services.AddScoped<IDependencyResolver>(_ => new FuncDependencyResolver(_.GetRequiredService));
services.AddScoped<IDocumentExecuter, DocumentExecuter>();
services.AddScoped<IDocumentWriter, DocumentWriter>();
services.AddScoped<ICompanyRepository, CompanyRepository>();
services.AddScoped<CompanyService>();
services.AddScoped<CompanyRepository>();
services.AddScoped<CompanyQuery>();
services.AddScoped<CompanyType>();
services.AddScoped<ISchema, MiracleSchema>();
services.AddControllers();
services.AddRazorPages();
//services.Configure<KestrelServerOptions>(options => options.AllowSynchronousIO = true);
//services.Configure<IISServerOptions>(options => options.AllowSynchronousIO = true);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseGraphiQl("/graphql");
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
}
GraphQLController.cs
[Route("[Controller]")]
[ApiController]
public class GraphQLController : Controller
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _documentExecuter;
public GraphQLController(ISchema _schema, IDocumentExecuter _documentExecuter)
{
this._schema = _schema;
this._documentExecuter = _documentExecuter;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
{
if(query == null)
{
throw new ArgumentException(nameof(query));
}
var inputs = query.Variables?.ToInputs();
var executionOptions = new ExecutionOptions
{
Schema = _schema,
Query = query.Query,
OperationName = query.OperationName,
Inputs = inputs
};
var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false); ;
if (result.Errors?.Count>0)
{
return BadRequest(result);
}
return Ok(result);
}
}
CompanyType.cs
public class CompanyType : ObjectGraphType<Company>
{
public CompanyType()
{
Field(X => X.address_1);
Field(X => X.address_2);
Field(X => X.banner);
Field(X => X.business_type);
Field(X => X.city);
Field(X => X.company_name);
Field(X => X.contact_designation);
Field(X => X.contact_person);
Field(X => X.country_code);
Field(X => X.created_by);
Field(X => X.created_date, nullable: true);
Field(X => X.email);
Field(X => X.Id);
Field(X => X.landline);
Field(X => X.logo);
Field(X => X.mobile);
Field(X => X.registered_date);
Field(X => X.status);
Field(X => X.updated_by, nullable: true);
Field(X => X.updated_date, nullable: true);
Field(X => X.web);
}
}
CompanyQuery.cs
public class CompanyQuery:ObjectGraphType
{
public CompanyQuery(CompanyService companyService)
{
Field<ListGraphType<CompanyType>>(
name : "companies",
resolve: context => {
return companyService.GetAll();
} );
}
}
MiracleSchema.cs
public class MiracleSchema : GraphQL.Types.Schema
{
public MiracleSchema(IDependencyResolver resolver) : base(resolver)
{
Query = resolver.Resolve<CompanyQuery>();
//Mutation = resolver.Resolve<CompanyMutation>();
}
}
CompanyRepository.cs
public class CompanyRepository: ICompanyRepository
{
private readonly miracleContext _dbContext;
public CompanyRepository(miracleContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<Company> GetAll()
{
return _dbContext.Companies;
}
}
CompanyService.cs
public class CompanyService
{
private readonly CompanyRepository _companyRepository;
public CompanyService(CompanyRepository companyRepository)
{
_companyRepository = companyRepository;
}
public IEnumerable<Company> GetAll()
{
return _companyRepository.GetAll();
}
}
【问题讨论】:
标签: c# sql-server .net-core graphql entity-framework-core