0
votes

I'm trying to build a mutation that accepts a collection. The GraphQL server is written in c# .net core and uses 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;

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). 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.

How do I pass a collection on PupilMetricCollectionInputType? Do I need some sort of ListGraphInputType?

1

1 Answers

0
votes

I was new to graphQL and as such presumed that the error I had made was in my code. 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; 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!