【问题标题】:Cannot convert value to AST with Apollo Client & GraphQL .NET无法使用 Apollo Client 和 GraphQL .NET 将值转换为 AST
【发布时间】:2020-12-08 00:24:46
【问题描述】:

当我在 React 中使用 Apollo Client 调用 .NET GraphQL 后端时,我遇到了一个奇怪的问题。

由于某些未知原因,我的字符串类型出现以下错误:

Cannot convert value to AST

如果我使用 fetchaxios 发布查询和突变,我的后端工作正常,但这个错误只发生在 Apollo。

由于 Apollo,我不得不将我的 Variables 类型更改为 Inputs 而不是 JObject,并且错误开始发生。

这是我的新 GraphQL 请求类:

using GraphQL;

namespace xyz.com
{
    public class GraphQLQuery
    {
        public string OperationName { get; set; }
        public string NamedQuery { get; set; }
        public string Query { get; set; }
        public Inputs Variables { get; set; }
    }
}

以前是这样的:

public class GraphQLQuery
        {
            public string OperationName { get; set; }
            public string NamedQuery { get; set; }
            public string Query { get; set; }
            public JObject Variables { get; set; }
        }

如果我使用 JObject,我会在我的 post 方法中通过 Apollo 调用得到一个空对象。

【问题讨论】:

    标签: .net graphql apollo-client


    【解决方案1】:

    好的。几个小时后,我想出了如何解决这个问题。

    这就是我所做的。

    我将 API 方法参数更改为以下内容:

    public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement rawQuery)
    

    然后我添加以下行以将 rawQuery 值反序列化为 GraphQL 请求对象:

    var graphQLQuery = JsonConvert.DeserializeObject<GraphQLQuery>(rawQuery.ToString());
    

    现在代码完美运行。

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2019-11-16
      • 2019-12-09
      • 2020-04-06
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2017-05-06
      相关资源
      最近更新 更多