Reputation: 51
Hello friends I want pass oauth_token
and client_id
in request body of a GraphQL Client. So how can I pass them, because GraphQLRequest has only three fields (i.e Query , Variables and OperationName). Please suggest.
using GraphQL.Client;
var heroRequest = new GraphQLRequest{ Query = Query };
var graphQLClient = new GraphQLClient("URL");
var graphQLResponse = await graphQLClient.PostAsync(heroRequest);
Upvotes: 5
Views: 10605
Reputation: 5731
var graphQLClient = new GraphQLHttpClient("localhost/graphql", new NewtonsoftJsonSerializer());
graphQLClient.HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer ????");
Upvotes: 0
Reputation: 61
Use GraphQLHttpClient instead of GraphQLClient
example:
public class DemoController : Controller
{
private readonly GraphQLHttpClient _client;
public DemoController(GraphQLHttpClient client)
{
_client = client;
}
public async Task<ActionResult> YourMethod()
{
_client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("JWT", yourToken);
var query = new GraphQLRequest
{
Query = "query{......}"
}
...
}
}
Of course you have to register your GraphQLHttpClient in Startup.
Example:
services.AddScoped(s => new GraphQLHttpClient(Configuration["YourGraphQLUri", new NewtonsoftJsonSerializer()));
Upvotes: 4
Reputation: 51
You can add the Authorization (or any other parameter) to the Header using the DefaultRequestHeaders from GraphQLClient.
var graphClient = new GraphQLClient("https://api.github.com/graphql");
graphClient.DefaultRequestHeaders.Add("Authorization", $"bearer {ApiKey}");
var request = new GraphQLRequest
{
Query = @"query { viewer { login } }"
};
var test = await graphClient.PostAsync(request);
Here you can find more detailed information about the DefaultRequestHeaders from HttpClient: Setting Authorization Header of HttpClient
Also, there's a related issue created on graphql-dotnet git.
https://github.com/graphql-dotnet/graphql-client/issues/32
Upvotes: 5