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

Adding headers at the request level

Open anbo65 opened this issue 4 years ago • 2 comments
trafficstars

I understand that you can add headers at the client level (that get sent for every request) but how can I add a header at the request level.

e.g. This code shows how DefaultRequestHeaders can be set, but what if I wanted to add a request level header for say a Correlation Id?

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);

anbo65 avatar Mar 08 '21 14:03 anbo65

Inherit from GraphQLHttpRequest and override ToHttpRequestMessage, then use your new request class instead of the default one when creating requests.

rose-a avatar Apr 02 '21 07:04 rose-a

Solved my problem. Thanks @rose-a!

bogdanpetea avatar Oct 10 '21 15:10 bogdanpetea