0

将我在 .net 5.0 中的 GraphQL.net 项目迁移到 GraphQL.net 4.7.1。主要目的是在 Apollo Federation 中将其用作子图。

.csproj 如下所示;

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>   
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GraphQL.MicrosoftDI" Version="4.7.1" />
    <PackageReference Include="GraphQL.NewtonsoftJson" Version="4.7.1" />
    <PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.4.0" />
    <PackageReference Include="GraphQL" Version="4.7.1" />    
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="4.0.1" />    
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.14" />       
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.6">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />   
    <PackageReference Include="System.Reactive" Version="4.4.1" />   
    <PackageReference Include="System.Reactive.Compatibility" Version="4.4.1" />   
  </ItemGroup>

启动.cs;

 public class Startup
    {
        private readonly IWebHostEnvironment _env;
        private readonly string _connectionString;
        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            _env = env;
            _connectionString = Configuration["ConnectionString"];
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DatabaseContext>(options =>
            {
                options.UseNpgsql(_connectionString);
            });
            services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
            services.AddScoped<IFieldService, FieldService>();
            services.AddScoped<MainMutation>();
            services.AddScoped<MainQuery>();
            services.AddScoped<RestaurantGType>();
            services.AddScoped<TagGType>();
            services.AddScoped<ISchema, FoodSchema>();
            services.AddGraphQL()                
              .AddGraphTypes(ServiceLifetime.Scoped);

            services.AddCors();
            
            services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });

            services.AddControllers()
                .AddNewtonsoftJson(options =>
                       options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                );
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {     
            app.UseCors(builder =>
               builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
           
            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
          
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }
    }

控制器是;

[Route("graphql")]
    public class GraphQLController : Controller
    {
        private IDocumentExecuter _executer;       
        private ISchema _schema;

        public GraphQLController(
             IDocumentExecuter executer,            
             ISchema schema)
        {
            _executer = executer;           
            _schema = schema;
        }

        [HttpPost]
        public async Task<IActionResult> PostAsync(HttpRequestMessage request, [FromBody] GraphQLQuery query)
        {
            var inputs = query.Variables.ToInputs();
            var queryToExecute = query.Query;

            var result = await _executer.ExecuteAsync(_ =>
            {
                _.Schema = _schema;
                _.Query = queryToExecute;
                _.OperationName = query.OperationName;
                _.Inputs = inputs;

                _.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };

            }).ConfigureAwait(false);

            if (result.Errors?.Count > 0)
            {                
                return BadRequest(new { result, result.Errors });
            }
            return Ok(result);
        }
    }

    public class GraphQLQuery
    {
        public string OperationName { get; set; }
        public string Query { get; set; }
        public Newtonsoft.Json.Linq.JObject Variables { get; set; }
    }

我可以构建和运行。但是,在游乐场模式未加载时,微调器会继续显示。没有收到错误。我应该做出什么样的改变?

您可以在github repo中查看该项目

4

0 回答 0