graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

GraphQLLocalExecutionClient: Add user credentials/token

Open StefanKoenigMUC opened this issue 2 years ago • 6 comments
trafficstars

Hi there,

for querying a local instance of my graphql instance I'm using the GraphQLLocalExecutionClient. But for specific reasons, I also need to provide user details (username, credentials, ..) for the execution/permissions checks.

Is that somehow possible, without falling back to the HTTP client?

Thanks in advance for your help :-)

StefanKoenigMUC avatar Jul 03 '23 12:07 StefanKoenigMUC

GraphQLLocalExecutionClient directly passes the request to a IDocumentExecuter instance of GraphQL.NET (which is injected through the constructor of GraphQLLocalExecutionClient).

You need to find out in which way your GraphQL "instance" expects to receive the user details (remember, the Authorization HTTP header is a transport thing not directly related to the GraphQL engine). graphql-dotnet/server uses this validation rule, which won't work with the local execution client since this does not execute the request in a ASP.Net Core pipeline.

To test GraphQL authorization in an HTTP context I would recommend to actually use a http connection (see integration tests in this repo for some examples). Otherwise you need to build your own validation system, there are some pointers in the GraphQL.NET documentation regarding this topic.

rose-a avatar Jul 03 '23 12:07 rose-a

hmm so basically i don't need authentication, passing an additional object would be totally fine for me (-> as I'm already within the application context, I have the authenticated user already - just need the user somehow accessible within the query itself).

Thanks a lot for your answer!

StefanKoenigMUC avatar Jul 03 '23 13:07 StefanKoenigMUC

thanks a lot, did it now using the variable section. Not very nice but working for now :-) Thanks a lot again!

StefanKoenigMUC avatar Jul 03 '23 13:07 StefanKoenigMUC

hmm still a strange problem

        var graphqlRequest = new GraphQLRequest(request.Query, new
        {
            userName = user.LoginName
        });

        graphqlRequest.Variables = new { id = "foo" };
        graphqlRequest.Extensions = new { id = "foo" };

Variables doesnt show up on server side - Extension is filled properly (problem exists indepent of serializer - tried newtonsoft and system.text). Do you have an idea?

StefanKoenigMUC avatar Jul 03 '23 14:07 StefanKoenigMUC

thanks a lot, did it now using the variable section. Not very nice but working for now :-) Thanks a lot again!

I'd recommend using the extensions map for that

Variables doesnt show up on server side - Extension is filled properly (problem exists indepent of serializer - tried newtonsoft and system.text). Do you have an idea?

What is showing up? I'd expect variables to be { "id": "foo" }...

rose-a avatar Jul 03 '23 16:07 rose-a

What is showing up? I'd expect variables to be { "id": "foo" }...

Nothing, the variables enumerator is empty

StefanKoenigMUC avatar Jul 04 '23 06:07 StefanKoenigMUC