graphql-dotnet: Error: The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Newtonsoft.Json.Linq.JToken]. Path: $.variables | LineNumber: 0 | BytePositionInLine: 116.

Summary

Error running when I try to send variables

Example:

graphql-error

Json Error Complete

{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "|dbaac12a-46633a6f98c669bc.",
  "errors": {
    "$.variables": [
      "The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Newtonsoft.Json.Linq.JToken]. Path: $.variables | LineNumber: 0 | BytePositionInLine: 84."
    ]
  }
}

Relevant information

I noticed that there is no request in the controller, I think it is in the middleware!

Environment (if relevant)

  • Visual Studio 2019
  • Last Version GrapQL
  • .NET Core

Class Map

public class StateMapType : ObjectGraphType<State>
{
      public StateMapType()
      {
         Name = "state";
         Field(x => x.Id).Name("id");
         Field(x => x.Uf).Name("uf");
         Field<ListGraphType<CountryMapType>>().Name("country");
      }
}

GraphQLQuery

Field<ListGraphType<StateMapType>>(
           name: "states",
           arguments: new QueryArguments(
             new QueryArgument<BooleanGraphType>() { Name = "load", DefaultValue = false }
           ),
           resolve: context =>
           {
              bool load = context.GetArgument<bool>("load");
              IQueryable<State> query = DatabaseAccess.State.AsQueryable();
              if (load)
              {
                 query = query.Include(x => x.Country);
              }
              return query.AsNoTracking().ToList();
       }
 );

What could it be?

Note: I’m developing a package and it already worked the Query part, but this part of passing variables I can’t make it work

https://www.nuget.org/packages/Canducci.GraphQLQuery/

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 31 (14 by maintainers)

Most upvoted comments

@fulviocanducci - Like @joemcbride and @sungam3r mentioned earlier, Gql.Net is not having the problem. The problem is with the Controller’s model binder. You’re using a .Net Core 3.x project, which uses Micrsoft’s System.Text.Json by default instead of Newtonsoft. It can’t deserialize a JObject (and it also can’t serialize Gql.Net’s ExecutionResult).

A quick/simple way to confirm this is to change your controller from this:

public async Task<IActionResult> Post([FromBody]GraphQLQuery query)
{
	// original code...
}

to this:

public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement rawQuery)
{
	string rawJson = rawQuery.ToString();
	var query = JsonConvert.DeserializeObject<GraphQLQuery>(rawJson);
	// original code...
}

What you in a middlweare I did in a controller is the same.

No, it’s not the same, and we have been trying to help you fix it. We can’t help you if you don’t want to be helped.

Closing as this is not a problem with this project.

This question is not specific to this project, it is better to ask the authors of System.Text.Json serializer.

@fulviocanducci - Like @joemcbride and @sungam3r mentioned earlier, Gql.Net is not having the problem. The problem is with the Controller’s model binder. You’re using a .Net Core 3.x project, which uses Micrsoft’s System.Text.Json by default instead of Newtonsoft. It can’t deserialize a JObject (and it also can’t serialize Gql.Net’s ExecutionResult).

A quick/simple way to confirm this is to change your controller from this:

public async Task<IActionResult> Post([FromBody]GraphQLQuery query)
{
	// original code...
}

to this:

public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement rawQuery)
{
	string rawJson = rawQuery.ToString();
	var query = JsonConvert.DeserializeObject<GraphQLQuery>(rawJson);
	// original code...
}

This is the best solution to this Issue Thanks, but is there are a way to handle the coming query as an strongly typed object

Change id: 1 to id: $id - it will be right. But honestly, I still don’t understand what was going on.

Can you show any server logs?

Last Version GrapQL

2.4.0 or latest 3.0.0-preview ?