【问题标题】:How do I pass a collection to a GraphQL mutation with GraphQL.Net and c#?如何使用 GraphQL.Net 和 c# 将集合传递给 GraphQL 突变?
【发布时间】:2019-09-18 19:48:45
【问题描述】:

我正在尝试构建一个接受集合的突变。 GraphQL 服务器是用 c# .net core 编写的,使用 GraphQL.Net。

这是我的输入类型;

public class PupilReportInputType : InputObjectGraphType<PupilReport>
{
    public PupilReportInputType()
    {
        Name = "PupilReportInput";
        Field(pr => pr.PupilId);
        Field(pr => pr.PupilMetricCollections, type: typeof(ListGraphType<PupilMetricCollectionInputType>));
    }
}

使用 GraphiQL 我尝试发布以下突变;

mutation getPupilReportText($pupilReport: PupilReportInput!) {
  getPupilReportText(pupilReport: $pupilReport)
}

使用以下变量;

{
  "pupilReport": {
    "pupilId" : 5,
    "pupilMetricCollections": {
      "pupilMetricCollection" : {
         "pupilId" : 5
      }
    }
  }
}

我得到的回应是;

{
  "errors": [
    {
      "message": "Variable '$pupilReport.pupilMetricCollections[0]' is invalid. Unable to parse input as a 'PlayerMetricCollectionInput' type. Did you provide a List or Scalar value accidentally?",
      "extensions": {
        "code": "INVALID_VALUE"
      }
    }
  ]
}

如果PupilReportInputType 删除了PupilMetricCollections 字段并且对变异变量进行了类似调整,则查询的行为与预期一致(不是错误)。同样,如果我修改变量输入和PupilReportInputType,使该字段只是PupilMetricCollectionInputType 而不是ListGraphType&lt;PupilMetricCollectionInputType&gt;,那么我可以让它全部工作。

如何在PupilMetricCollectionInputType 上传递集合?我需要某种 ListGraphInputType 吗?

【问题讨论】:

    标签: c# graphql graphql.net


    【解决方案1】:

    我是 graphQL 的新手,因此我认为我犯的错误在我的代码中。事实上,我收到的回复信息提供了我需要的所有信息。我发送的可变数据确实格式错误。

    我发了这个;

    {
      "pupilReport": {
        "pupilId" : 5,
        "pupilMetricCollections": {
          "pupilMetricCollection" : {
             "pupilId" : 5
          }
        }
      }
    }
    

    但是,StudentMetricCollections 是数组类型;所以我需要使用数组语法。以下工作正常;

    {
      "pupilReport": {
        "pupilId" : 5,
        "pupilMetricCollections": [
          {
            "pupilId" : 1
          },
          {
            "pupilId" : 2
          }
        ]
      }
    }
    

    我最大的收获是相信 GraphQL 错误响应,不要忘记寻找明显的东西!

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 2020-09-22
      • 2019-05-12
      • 2020-02-07
      • 2021-06-22
      • 2017-09-22
      • 2017-01-26
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多