Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate empty requests using GraphQL.NET? #4

Open
santoshpatro opened this issue Nov 16, 2018 · 0 comments
Open

How to validate empty requests using GraphQL.NET? #4

santoshpatro opened this issue Nov 16, 2018 · 0 comments

Comments

@santoshpatro
Copy link

Hi Glen,
There is possibility of sending empty requests to Graphql server implemented using GraphQL.NET and asp.net core 2.

I tried to implement it using asp.net core 2 action filter but it seems not to be working.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddMvc(options =>
    {
        options.Filters.Add(typeof (CheckModelForNullAttribute));
        options.Filters.Add(typeof (ValidateModelStateAttribute));
    }

    ).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// GraphQLController.cs

[Route("[controller]")]
public class GraphQLController : Controller
{
    private readonly IDocumentExecuter _documentExecuter;
    private readonly ISchema _schema;
    public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter)
    {
        _schema = schema;
        _documentExecuter = documentExecuter;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
    {
        if (query == null)
        {
            throw new ArgumentNullException(nameof(query));
        }

        var inputs = query.Variables.ToInputs();
        var executionOptions = new ExecutionOptions{Schema = _schema, Query = query.Query, Inputs = inputs, UserContext = Request.Headers, //UserContext = new GraphQLUserContext { Headers = Request.Headers }
 };
        var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);
        if (result.Errors?.Count > 0)
        {
            return BadRequest(result);
        }

        return Ok(result);
    }
}

// CheckModelForNullAttribute.cs

public class CheckModelForNullAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (context.ActionArguments.Any(c => c.Value == null))
        {
            context.Result = new BadRequestObjectResult(context.ModelState);
        }
    }
}

// ValidateModelStateAttribute.cs

public class ValidateModelStateAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            context.Result = new BadRequestObjectResult(context.ModelState);
        }
    }
}

Can you please help me to fix this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant