Skip to main content

GraphQL–HotChocolate–Read HTTP headers

Quick tip if you want to read the HTTP headers from the incoming GraphQL request in .NET Core using HotChocolate.

There are 2 ways I found that can be used to achieve this:

  1. Inject the IHttpContextAccessor in your resolvers.
  2. Create a custom Query Interceptor and set the context data

First one is rather obvious but let’s see an example on how to take the second approach:

In your resolvers you can now access the context property through the ContextData directly or by injecting it: