Code Monkey home page Code Monkey logo

Comments (5)

ntaranov avatar ntaranov commented on July 18, 2024 2

I use this code:

    private async Task WriteResponseAsync(HttpContext context, ExecutionResult result)
    {
        var json = _writer.Write(result);

        context.Response.ContentType = "application/json";
        context.Response.StatusCode = result.Errors?.Any() != true
            ? (int)HttpStatusCode.OK
            : result.Errors?.Any(er => (er as ValidationError)?.ErrorCode  == "authorization") == true
                ? (int)HttpStatusCode.Unauthorized
                : (int)HttpStatusCode.BadRequest;

        await context.Response.WriteAsync(json);
    }

I actually would be happy to see this integrated in the project.

from authorization.

alonstar avatar alonstar commented on July 18, 2024 1

oh, I got it , thanks.

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

You would need to handle that in your http server. If you are using the server project then this is the code that would need to be changed:

https://github.com/graphql-dotnet/server/blob/a54a95f3807ade7aa4e23cce76fe8e58211053a0/src/AspNetCore/GraphQLHttpMiddleware.cs#L77

At present it just does a 400 error on any error.

from authorization.

okarlsson avatar okarlsson commented on July 18, 2024

If anyone else bumps into this:

I just build a simple if statement that checks for authorization errors in the ExecutionResult like this:

if (result.Errors?.Count > 0)
{
        if (result.Errors.Select(e => e.Code).Contains("authorization"))
	{
		var retval = new ErrorResponse()
		{
			MessageForUser = "Unauthorized",
			MessageForDev = GetErrorMessage(result.Errors)
		};
                return new JsonResult(retval)
		{
			StatusCode = 401
		};
	}
	else
	{
		var retval = new ErrorResponse()
		{
			MessageForUser = GetErrorMessage(result.Errors),
			MessageForDev = GetErrorMessage(result.Errors)
		};

		return BadRequest(retval);
	}
}

return Ok(result);
}

private string GetErrorMessage(ExecutionErrors errors)
{
	return string.Join(", ", errors.Select(e => e.Message));
}

from authorization.

giososa24 avatar giososa24 commented on July 18, 2024

I use this code:

    private async Task WriteResponseAsync(HttpContext context, ExecutionResult result)
    {
        var json = _writer.Write(result);

        context.Response.ContentType = "application/json";
        context.Response.StatusCode = result.Errors?.Any() != true
            ? (int)HttpStatusCode.OK
            : result.Errors?.Any(er => (er as ValidationError)?.ErrorCode  == "authorization") == true
                ? (int)HttpStatusCode.Unauthorized
                : (int)HttpStatusCode.BadRequest;

        await context.Response.WriteAsync(json);
    }

I actually would be happy to see this integrated in the project.

Hi, I have a question, where do you put this code?

from authorization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.