【问题标题】:Error: The type or name space "GraphQLRequest" could not be found错误:找不到类型或命名空间“GraphQL 请求”
【发布时间】:2019-11-08 10:02:51
【问题描述】:

当我编写以下代码时,Visual Studio 显示一个错误,表明它无法找到名为 GraphQL 的东西,即使我在此链接上从 nuget 安装了包: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 }
};

【问题讨论】:

  • 灯泡告诉你做什么?你引用正确了吗?
  • 它只是建议创建一个新类。我不知道我是否正确引用了它,但我尝试输入所有可能的内容。你能检查一下吗@DenisSchaf

标签: c# .net namespaces graphql using


【解决方案1】:

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; }
}

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

您的 API 控制器中的代码如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2019-09-14
    相关资源
    最近更新 更多