如何使用 GraphQL.Net 和 c# 将集合传递给 GraphQL 突变?

How do I pass a collection to a GraphQL mutation with GraphQL.Net and c#?

我正在尝试构建一个接受集合的突变。 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 post 以下突变;

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<PupilMetricCollectionInputType> 那么我可以让它全部工作。

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

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

我发送了这个;

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

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

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

我在这里的主要收获是相信 GraphQL 错误响应,并且不要忘记寻找明显的!