【问题标题】:Why Graphql Result gives more json details than query requested为什么 Graphql Result 提供的 json 详细信息比请求的查询多
【发布时间】: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


    【解决方案1】:

    至于“为什么不显示架构?”问题,可能是因为您的 MiracleSchema 类定义错误。

    如果您查看GraphQL Examples project,其StarWarsSchema class 通过其构造函数定义并期望一个 IServiceProvider 实例:

    using GraphQL.Types;
    using GraphQL.Utilities;
    using System;
    
    namespace StarWars
    {
        public class StarWarsSchema : Schema
        {
            public StarWarsSchema(IServiceProvider provider)
                : base(provider)
            {
                Query = provider.GetRequiredService<StarWarsQuery>();
                Mutation = provider.GetRequiredService<StarWarsMutation>();
            }
        }
    }
    

    【讨论】:

    • 仍然,我遇到了同样的问题..任何人都可以与我分享一个使用 Dotnet core 3.1 API+ GraphQl 开发的良好工作项目示例,我在这个问题上苦苦挣扎了一个多星期..谢谢大家
    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多