【问题标题】:.Net core 3.1 + GraphQL ==> "error": "Unexpected end of JSON input".Net core 3.1 + GraphQL ==> “错误”:“JSON 输入意外结束”
【发布时间】:2020-05-31 10:27:14
【问题描述】:

我开发了一个 .net core 3.1 Web API,并在其中添加了 GraphQLPlayGround。当我尝试启动 Playground 时,我遇到了这个让我厌烦的错误。我用谷歌搜索了太多次,我已经阅读了很多关于它的文章和论坛,我已经尝试了他们建议的方法,但问题仍然存在。另外,我应该说 .net core 2.2 没有这个问题。

我决定简化我的解决方案并删除我项目的多余部分并将其上传给您,以便您可以帮助我。 我目前正在处理的那个项目的简化版本是here on google drive
此外,我找到了一个没有这个问题的.net core 3.1 解决方案,但我仍然找不到我的错误。我已经上传了这个正确的解决方案here

在这个简化版本中,我没有连接到数据库,也没有将任何相关的包添加到项目中。我在这个简化版本中返回内存中的数据,而不是联系数据库。

这是我面临的错误的图片:

而简化项目的.csproj文件如下:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>NetCore3._1GraphQL</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="graphiql" Version="1.2.0" />
    <PackageReference Include="GraphQL" Version="3.0.0-preview-1352" />
    <PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.4.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
    <PackageReference Include="RandomNameGeneratorLibrary" Version="1.2.2" />
  </ItemGroup>

</Project>

【问题讨论】:

    标签: graphql .net-core-3.1 graphql-playground


    【解决方案1】:

    根据简化和错误的项目,afaict 配置了将/graphql 端点映射到控制器但不包含匹配控制器的路由。因此,服务器返回 404 响应,并且 graphql 游乐场产生错误消息。添加控制器(或中间件)使其工作。

    以下是对其进行简化的错误项目的更改:

    1. 添加匹配/graphql路由的api控制器。
    // GraphqlController.cs
    // using statements are omitted
    namespace NetCore3._1GraphQL.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class GraphqlController : ControllerBase
        {
            private readonly IServiceProvider _provider; 
            private readonly IDocumentExecuter _executer;
            private readonly IDataLoaderContextAccessor _loader;
    
            public GraphqlController(
                IDocumentExecuter executer,
                IServiceProvider provider, 
                IDataLoaderContextAccessor loader)
            {
                _executer = executer;
                _provider = provider;
                _loader = loader;
            }
    
            [HttpPost]
            public async Task<IActionResult> Post([FromBody] GraphQLRequest graphQLRequest)
            {
                if (graphQLRequest == null)
                {
                    throw new ArgumentNullException(nameof(graphQLRequest));
                }
    
                var _query = graphQLRequest.Query;
                var _inputs = graphQLRequest.Variables?.ToObject<Inputs>();
                var _options = new ExecutionOptions
                {
                    Schema = new GraphQLSchema(_provider)
                    {
                        Query = new GraphQLQuery(_loader),
                    },
                    Query = _query,
                    Inputs = _inputs,
                    FieldNameConverter = new PascalCaseFieldNameConverter()
                };
    
                try
                {
                    var result = await _executer.ExecuteAsync(_options).ConfigureAwait(false);
    
                    if (result.Errors?.Count > 0)
                    {
                        return BadRequest(result);
                    }
    
                    return Ok(result);
                }
                catch (Exception ex)
                {
                    return BadRequest(ex);
                }
            }
        }
    }
    
    
    1. 取消注释BaseGraphQLQuery.cs 中的public JObject Variables { get; set; } 行。

    2. BaseGraphQLQuery 类是 GraphQL 请求的实现,而不是 GraphQL 查询。将其重命名为GraphQLRequest 或将控制器后操作中的参数名称更改为BaseGraphQLQuery

    【讨论】:

    • 谢谢@moustapha。我现在很忙,但我会尽快查看您的答案。
    • 我检查了你的答案,你是对的。一年前,当我从网上下载这个项目时,我是 GraphQL 的初学者,不知道必须有一个控制器来处理 GraphQL 请求。但是,让我们将这一点添加到您的答案中,以使其对这个问题的其他读者更好。更改应用程序使用 GraphQLPlayground 工具的第 76 行,如下所示:app.UseGraphQLPlayground(options: new GraphQLPlaygroundOptions() { Path = "/graphqlplayground", GraphQLEndPoint = "/graphql" });,PlayGround 将出现在此localhost:44313/graphqlplayground
    【解决方案2】:

    在startup.cs中添加

    public void ConfigureServices(IServiceCollection services)
        {
            //add this
            services.AddSingleton<MyAppSchema>();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      相关资源
      最近更新 更多