Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom HttpClient for GraphQLClient #32

Closed
OneCyrus opened this issue Mar 13, 2018 · 6 comments
Closed

Custom HttpClient for GraphQLClient #32

OneCyrus opened this issue Mar 13, 2018 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@OneCyrus
Copy link

It would be very helpful to supply my own instance of HttpClient to the GraphQLClient constructor. Currently it allows to supply a HttpMessageHandler. But this way I can't specify authorization tokens.

Or is there another way to acomplish this?

@deinok
Copy link
Member

deinok commented Mar 13, 2018

Well, if you specify better what is your use case i can give help.

If you need to add a Header you can use something like this:

var graphClient = new GraphQLClient("https://api.github.com/graphql");
graphClient.DefaultRequestHeaders.Add("Authorization", $"bearer {gitHubAccessToken}");
//var test = await graphClient.GetIntrospectionQueryAsync();
var request = new GraphQLRequest
{
    Query = @"query { viewer { login } }"
};
var test = await graphClient.PostAsync(request);

IDK if this solves your issue or you have a better idea of how to implement your case

@deinok deinok self-assigned this Mar 13, 2018
@deinok deinok added the question Further information is requested label Mar 13, 2018
@OneCyrus
Copy link
Author

thanks that did the trick!

my use case is integration testing a graphql endpoint which is started as a .net core TestServer. it was a bit tricky because it's not an endpoint which is accessible as any other real endpoint.

@Adebayo-Adesegun
Copy link

Adebayo-Adesegun commented Jul 30, 2020

I can't add DefaultRequestHeaders with IGraphQLClient, how can this be done, please?

@Version of GraphQL.Client is 3.1.3

@ikelds
Copy link

ikelds commented Nov 6, 2020

I can't add DefaultRequestHeaders with IGraphQLClient, how can this be done, please?

@Version of GraphQL.Client is 3.1.3

Try:

GraphQLHttpClient client = new GraphQLHttpClient("https://example.com/graphql", new NewtonsoftJsonSerializer());
client.HttpClient.DefaultRequestHeaders.Add("Authorization", accessToken);

@Adebayo-Adesegun
Copy link

Adebayo-Adesegun commented Nov 6, 2020

I can't add DefaultRequestHeaders with IGraphQLClient, how can this be done, please?
@Version of GraphQL.Client is 3.1.3

Try:

GraphQLHttpClient client = new GraphQLHttpClient("https://example.com/graphql", new NewtonsoftJsonSerializer());
client.HttpClient.DefaultRequestHeaders.Add("Authorization", accessToken);

I had to use the HttpClient Library to get around this party issue as IGraphQLClient didn't seem to have a direct support for adding Default HTTP Headers, you can try this example on this GitHub gist :

https://gist.github.com/Adebayo-Adesegun/0bc086050625a1e5f30fbba8a697ca35:

You can just treat it like a normal HTTP request. The GraphQL server endpoint figures the rest out from your structure.

@sungam3r
Copy link
Member

sungam3r commented Nov 6, 2020

https://github.com/graphql-dotnet/graphql-client/blob/master/src/GraphQL.Client/GraphQLHttpClientOptions.cs#L49-L53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants