graphql-client
graphql-client copied to clipboard
How to dynamically change headers
I need to dynamically change the request headers. How to accomplish this?
You need to instantiate a new client each time you want to change the headers.
Or use the context that you pass into the headers
method.
require "graphql/client"
require "graphql/client/http"
# Star Wars API example wrapper
module SWAPI
# Configure GraphQL endpoint using the basic HTTP network adapter.
HTTP = GraphQL::Client::HTTP.new("https://example.com/graphql") do
def headers(context)
{ "X-Api-Key" => context[:api_key] }
end
end
end
And when you perform a query on your GraphQL client, pass in the context:
storefront_api_key = "dynamic api key"
result = SWAPI::Client.query(SWAPI::MyQuery, variables: {}, context: {api_key: storefront_api_key})
So everytime you perform a query, you have the opportunity to pass in a context to alter your headers.
I really think this issue can be closed. The @tolgap solution works well