mark_h
mark_h

Reputation: 5477

How can I access the Request object from a Field within an ObjectGraphType when using GraphQL.net?

I'm using GraphQL.net with C#. I have a Field within an ObjectGraphType that looks something like this;

        Field<FooGraphType>(
            "baz",
            resolve: context =>
            {
                //I want to inspect the Request object here
            });

The one and only endpoint I have looks like this;

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
    {
        // I can access the Request object here
        if (query == null) { throw new ArgumentNullException(nameof(query)); }
        var inputs = query.Variables.ToInputs();
        var executionOptions = new ExecutionOptions
        {
            Schema = _schema,
            Query = query.Query,
            Inputs = inputs
        };

        var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);

        if (result.Errors?.Count > 0)
        {       
            return BadRequest(result);
        }

        return Ok(result);
    }

How can I let the resolve function of my ObjectGraphType field gain access to the HttpRequest object found in the controller action? Although I am interested in getting the entire HttpRequest object, being able to pass specific pieces of information from the Request into the resolve function would be sufficient.

Upvotes: 0

Views: 628

Answers (1)

mark_h
mark_h

Reputation: 5477

You can either inject IHttpContextAccessor or store information in the userContext property.

If you set the UserContext of the ExceptionOptions you can access it in the context of the field resolve function.

Upvotes: 1

Related Questions