opened
aaron-c2
username_0
Hi, sorry if this is a simple question but I haven't been able to find an answer in the examples. I've used this library before and had a Controller with a snippet similar to the below to pass the User Context. 
```
var result = await _documentExecuter.ExecuteAsync(_ => {
            _.Schema = _schema;
            _.Query = query.Query;
            _.OperationName = query.OperationName;
            _.Inputs = inputs;
            _.UserContext = HttpContext.User;
        });
```
Then the user context is visible on the field resolver. 
I've recently picked up an API which is using the Schema first approach, so no controllers at all and I don't understand how to access the UserContext in my query - the rest of the context comes through but the UserContext on the resolve field context is always empty. 
```
public async Task<MyResult> MyQuery(ResolveFieldContext context) {                                    
  var userContext = context.UserContext //always count =0                                             
}
```
I have added the following to the Startup.cs as the docs suggest but don't see how this works?
```
services.AddGraphQL.AddUserContextBuilder(httpContext => new GraphQlUserContext { User = httpContext.User });
```
How to access UserContext in Query with Schema First approach
issue
created
DiogoVCS
username_1
I dont think you need that last line in the Startup.cs....

Try to create a context class as follows:

```java
    public class GraphQlUserContextDictionary : Dictionary<string, object>
    {
        public ClaimsPrincipal User { get; set; }

        public GraphQlUserContextDictionary(ClaimsPrincipal User)
        {
            this.User = User;
        }
    }
```
and then in the controller create a new context: GraphQlUserContextDictionary

```java
var graphQlUserContext = new GraphQlUserContextDictionary(User);
```
and then pass it to the _.UserContext in the ExecutionResult.
For accessing it from the Query do the following: 

```java
Field<SomeType>(
      "something",
      resolve: context =>
      {
          var userName = (context.UserContext as GraphQlUserContextDictionary).User.Identity.Name;
          return repository.getAll()
      }
  );
```
comment
created
sungam3r
username_2
@username_0 Did you manage to solve this issue?
comment
created
aaron-c2
username_0
Hi username_2. Yes, I've solved the problem now, apologies for leaving this hanging.
comment
closed
aaron-c2
username_0
issue
created
itabaiyu
username_3
I'm actually running into this same issue and can't quite resolve it. @username_1 what you described uses the GraphType first approach and doesn't solve the problem that was initially posted. Here is some mock code to describe what I'm facing:

Schema:
```
public ISchema GetSchema()
{
    return Schema.For(@"
        type Book {
            id: ID
            name: String,
            genre: String
        }

        type Query {
            book: Book
        }",
        _ =>
        {
            _.Types.Include<Query>();
        }
    );
}
```

Query.cs:
```
[GraphQLMetadata("Query")]
public class Query
{
    [GraphQLMetadata("book")]
    public async Task<Book> GetBook(ResolveFieldContext context, int bookId)
    {
        var userContext = context.UserContext; // <- this is currently always null

        // do work with userContext and bookId to get a book
    }
}
```

Controller code:
```
var result = await _documentExecuter.ExecuteAsync(_ => {
            _.Schema = _schema;
            _.Query = query.Query;
            _.OperationName = query.OperationName;
            _.Inputs = inputs;
            _.UserContext = HttpContext.User;
        });
```

With the above code, `ResolveFieldContext` is always null. Am I missing anything here?
comment
created
sungam3r
username_2
https://github.com/graphql-dotnet/graphql-dotnet/blob/master/src/GraphQL/Reflection/ReflectionHelper.cs#L85

Change `ResolveFieldContext` to `IResolveFieldContext`. Some time ago we introduced this interface and split context to different implementation types.
comment
created
sungam3r
username_2
I mean that actual resolve context type **is not** `ResolveFieldContext`. It is likely `ReadonlyResolveFieldContext` so changing argument type to `ReadonlyResolveFieldContext` should work too.
comment
created
itabaiyu
username_3
Thanks @username_2  that worked. It would be great if someone could update the docs with an example of getting context into resolver methods for the schema-first approach. I looked through them and wasn't able to find anything. Apologies if it's already there and I missed it somehow.
comment
created
sungam3r
username_2
@username_4 Would you like to update docs with that info?
comment
created
Shane32
username_4
Well, I've never used the schema-first approach.  I'd feel better if you updated the existing docs, and I'll see what you changed and include applicable notes in the upgrade docs.
comment
created
Shane32
username_4
Or if you want to give me a little direction as to where you feel the additional documentation should be added, I can take a stab at it.
comment
created
itabaiyu
username_3
@username_4 I can make a PR to update the docs tonight, if I have permission to do so.

There should be a schema-first example side by side with the other user context info [here](https://graphql-dotnet.github.io/docs/getting-started/user-context/).
comment
created
sungam3r
username_2
@username_4  I mean document that there are more than one form of resolve context. You may place it here https://github.com/graphql-dotnet/graphql-dotnet/blob/master/docs2/site/docs/getting-started/introduction.md#schema-first-approach.
comment