简体   繁体   English

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

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

I'm trying to build a mutation that accepts a collection. 我正在尝试构建一个接受集合的变异。 The GraphQL server is written in c# .net core and uses GraphQL.Net. GraphQL服务器使用c#.net核心编写,并使用GraphQL.Net。

This is my input type; 这是我的输入类型;

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

Using GraphiQL I tried to post the following mutation; 使用GraphiQL我尝试发布以下变异;

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

with the following variable; 使用以下变量;

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

The response I get is; 我得到的回应是;

{
  "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"
      }
    }
  ]
}

If PupilReportInputType has the PupilMetricCollections field removed and the mutation variables adjusted similarly then the query behaves as expected (not errors). 如果PupilReportInputType删除了PupilMetricCollections字段并且类似地调整了变异变量,则查询将按预期运行(而不是错误)。 Similarly if I modify the variable input and PupilReportInputType so that the field is just PupilMetricCollectionInputType rather than ListGraphType<PupilMetricCollectionInputType> then I can get it all working. 类似地,如果我修改变量输入和PupilReportInputType以便该字段只是PupilMetricCollectionInputType而不是ListGraphType<PupilMetricCollectionInputType>那么我可以将它全部工作。

How do I pass a collection on PupilMetricCollectionInputType ? 如何在PupilMetricCollectionInputType上传递集合? Do I need some sort of ListGraphInputType? 我需要某种ListGraphInputType吗?

I was new to graphQL and as such presumed that the error I had made was in my code. 我是graphQL的新手,因此假设我所犯的错误出现在我的代码中。 In truth, the response message I was getting was giving me all the information I needed; 事实上,我收到的回复信息正在向我提供我需要的所有信息; the variable data I was sending was indeed malformed. 我发送的可变数据确实是错误的。

I sent this; 我发了这个;

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

However, pupilMetricCollections is an array type; 但是,pupilMetricCollections是一种数组类型; so I needed to use array syntax. 所以我需要使用数组语法。 The following worked fine; 以下工作正常;

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

My big takeaway here is trust the GraphQL error responses and don't forget to look for the obvious! 我最重要的是相信GraphQL错误响应,不要忘记寻找明显的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM