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

How to dynamically change headers #192

Open
dfritsch86 opened this issue Apr 23, 2019 · 3 comments
Open

How to dynamically change headers #192

dfritsch86 opened this issue Apr 23, 2019 · 3 comments

Comments

@dfritsch86
Copy link

dfritsch86 commented Apr 23, 2019

I need to dynamically change the request headers. How to accomplish this?

@j-collier
Copy link

j-collier commented Apr 26, 2019

You need to instantiate a new client each time you want to change the headers.

@tolgap
Copy link

tolgap commented Jun 19, 2019

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.

@Nittarab
Copy link

Nittarab commented Sep 1, 2021

I really think this issue can be closed. The @tolgap solution works well

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

No branches or pull requests

4 participants