How to extract request headers and pass it to the business logic in case of asp.net core 2 graphql endpoint?

2.4k Views Asked by At

I have the following code snippet developed using asp.net web api 2 and EntityFramework 6.

public class TestController : BaseApiController
{
    private readonly ITestService _testService;
    private readonly ICommonService _commonService;
    private readonly IImageService _imageService;
    public TestController(ITestService testService, ICommonService commonService, IImageService imageService)
    {
        _testService = testService;
        _commonService = commonService;
        _imageService = imageService;
    }

    [Route("test")]
    public IHttpActionResult Get()
    {
        var resp = _testService.GetDetailsForLocation(locale);
        return Ok(resp);
    }
}

public class BaseApiController : ApiController
{
    public string locale
    {
        get
        {
            if (Request.Headers.Contains("Accept-Language"))
            {
                return Request.Headers.GetValues("Accept-Language").First();
            }
            else
            {
                return string.Empty;
            }
        }
    }

    public string GetCookieId()
    {
        string value = string.Empty;
        IEnumerable<CookieHeaderValue> cookies = this.Request.Headers.GetCookies("mycookie");
        if (cookies.Any())
        {
            IEnumerable<CookieState> cookie = cookies.First().Cookies;
            if (cookie.Any())
            {
                var cookieValue = cookie.FirstOrDefault(x => x.Name == "mycookie");
                if (cookieValue != null)
                    value = cookieValue.Value.ToLower();
            }
        }

        return value;
    }
}

I am converting the existing restapi endpoint to graphql endpoint using asp.net core 2 and graphql.net. In the below method, at present I am sending "en" as the value but I want to pass the locale value as exactly done in case of asp.net web api 2 in the above implementation.

Here I would like to know what is the best way to read the request headers and pass the value to the business loigc (i.e in this case to the method:GetDetailsForLocation("en")

public class TestQuery : ObjectGraphType<object>
{
    public TestQuery(ITestService testService)
    {
        Field<TestResultType>("result", resolve: context => testService.GetDetailsForLocation("en"), description: "Test data");
    }
}

Can anyone help me to provide their guidance in resolving the issue?

1

There are 1 best solutions below

1
On BEST ANSWER

The easiest route would be to use IHttpContextAccessor. Register IHttpContextAccessor as a singleton.

https://adamstorr.azurewebsites.net/blog/are-you-registering-ihttpcontextaccessor-correctly

In StartUp.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

GraphQL class:

public class TestQuery : ObjectGraphType<object>
{
    public TestQuery(ITestService testService, IHttpContextAccessor accessor)
    {
        Field<TestResultType>(
            "result",
            description: "Test data",
            resolve: context => testService.GetDetailsForLocation(accessor.HttpContext...)
        );
    }
}