无法使用Apollo Client和GraphQL .NET将值转换为AST

问题描述

当我在React中使用Apollo客户端调用.NET GraphQL后端时,我面临一个奇怪的问题。

由于某种未知原因,我的String类型出现以下错误:

Cannot convert value to AST

如果我使用 fetch axios 发布查询和变异,则后端工作正常,但是此错误仅发生在Apollo上。

由于阿波罗,我不得不将我的变量类型更改为输入,而不是 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,则在Apollo调用的post方法中会得到一个空对象。

解决方法

好。几个小时后,我想出了解决该问题的方法。

这就是我所做的。

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

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

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

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

现在代码可以完美运行了。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...