c# - 错误 : The type or name space "GraphQLRequest" could not be found

标签 c# .net namespaces graphql using

当我编写以下代码时,visual studio 显示一个错误,表明即使我在此链接上安装了来自 nuget 的包,它也找不到名为 GraphQL 的东西:https://www.nuget.org/packages/GraphQL/并使用 GraphQL.Http 输入;

var query = @"query($id: String) { device (id: $id) { displayName, id } }";
var request = new GraphQLRequest()
{
    Query = query,
    Variables = new { id = 123 }
};

最佳答案

GraphQLRequest 不是作为 GraphQL 包的一部分提供的类,它是您自己创建的类。

例如,

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

此类定义了您尝试设置的 Query 和 Variables 属性。然后,您将传递此对象以进行查询。

API Controller 中的代码如下所示:
public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
{
    var inputs = query.Variables.ToInputs();
    var result = await new DocumentExecuter().ExecuteAsync(_ =>
                 {
                    _.Schema = productSchema;
                    _.Query = query.Query;
                    _.OperationName = query.OperationName;
                    _.Inputs = inputs;
                 }).ConfigureAwait(false);

    return Ok(result);
}

更多信息的实现来源:https://github.com/JacekKosciesza/StarWars

关于c# - 错误 : The type or name space "GraphQLRequest" could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773130/

相关文章:

c# - ComboBox 绑定(bind)到枚举类型的值,同时还具有 "blank"条目?

c# - n层架构: best place to store business objects?

C#命名空间问题: Must avoid namespace containing any class name overlap?

javascript - 如何在个人项目中使用此代码?它到底对你有什么作用

C# 日期时间到 W3CDTF

c# - 进程不适用于 Ubuntu 但适用于 Windows

c# - 在xml文件中搜索数据c#

c# - 如何在 C# 中使用热键启动程序?

.net - 确定当前的 AWS 区域 .Net?

python - Python 3 中变量的简单表达式