graphql-dotnet: .net 8 Variable 'myObject' is invalid. Unable to parse input as a 'myObject' type. Did you provide a List or Scalar value accidentally?

Summary

Similar to issue raised in https://github.com/graphql-dotnet/graphql-dotnet/issues/3582

experiencing the error: 'areaInput' is invalid. Unable to parse input as a 'areaInput' type. Did you provide a List or Scalar value accidentally?

Recently upgraded to .net 8, and graphql-dotnet v7, inspected migration guides but still running into issues somewhere in setup.

Stepping through the source code, it seems its failing because it expects the variables to be a dictionary<string, object>, but instead its a JObject still.

Wondering if Model Binding is partly the root cause. Do I need to explicitly capture the Httpcontext and deserialize using GraphQLSerializer?

Relevant information

.net 8 using GraphQL.NewtonsoftJson;

startup.cs

services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddScoped<IDataLoaderContextAccessor, DataLoaderContextAccessor>();
services.AddScoped<IServiceProvider>(
    s => new FuncServiceProvider(s.GetRequiredService)
);
services.AddScoped<ISchema, NavigationSchema>();

services.AddGraphQL(b => b
    .AddNewtonsoftJson()
    .AddSchema<NavigationSchema>()
    .AddGraphTypes()
    .AddErrorInfoProvider(o => o.ExposeExceptionDetails = true)
    .AddUserContextBuilder(ctx => new GraphQLUserContext { User = ctx.User })
);

//authentication
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<GraphQLAuth.IAuthorizationEvaluator, AuthorizationEvaluator>();
services.AddTransient<GraphQLVal.IValidationRule, AuthorizationValidationRule>();

GraphQLController

using GraphQLRequest = GraphQL.Transport.GraphQLRequest;
using GraphQL.NewtonsoftJson;

 [HttpPost]
 public async Task<IActionResult> Post([FromBody] GraphQLRequest request, [FromServices] IEnumerable<IValidationRule> validationRules)
 {
     var r = await ExecuteGraphQLRequest(request, validationRules);
     return r;
 }

private async Task<IActionResult> ExecuteGraphQLRequest(GraphQLRequest request, IEnumerable<IValidationRule> validationRules)
{
    var result = await _documentExecuter.ExecuteAsync(s =>
    {
        s.Schema = _schema;
        s.Query = request.Query;
        s.Variables = request.Variables;
        s.ValidationRules = validationRules;
        s.OperationName = request.OperationName;
        s.RequestServices = HttpContext.RequestServices;
        s.ThrowOnUnhandledException = true;
        s.User = HttpContext.User;
        s.UserContext = new GraphQLUserContext
        {
            User = HttpContext.User,
        };
        s.CancellationToken = HttpContext.RequestAborted;
    }).ConfigureAwait(false);

    if (result.Errors?.Count > 0)
    {
        if (result.Errors.Select(e => e.Code).Contains("authorization"))
        {
            return Unauthorized();
        }
        else
        {
            return BadRequest(result);
        }
    }
    return new ExecutionResultActionResult(result);
}

example query:

export const addAreaQuery = gql`
mutation addArea($areaInput: AreaInput!) {
  createArea(area: $areaInput) {
    id
    parentId
    displayName
  ...
  }
}

example data payload:

{
  "operationName": "addArea",
  "variables": {
    "areaInput": {
      "displayName": "test",
      ...
    }
  },
  "query": "mutation addArea($areaInput: AreaInput!) {\n  createArea(area: $areaInput) {\n    id\n    parentId\n    displayName ...
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Nice! Thanks for sharing the solution also!

Close issue?