How to access instance variable inside block

Viewed 142

I'm working with gem graphql-client & want to make it dynamic for different URL and token.

The example given in gem documentation & other places has hard coded URL & token.

I'm trying to access @shop.token inside def headers block of GraphQL::Client::HTTP

class Graphql

  def initialize(shop_id)
    @shop = Shop.find_by(shop_id)
  end

  def http
    GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/") do
      def headers(context)
        { 
          "User-Agent": "My Client",
          "Token" : @shop.token
        }
      end
    end
  end

  def client
      schema = GraphQL::Client.load_schema(http{@shop})
      
      @client ||= GraphQL::Client.new(schema: schema, execute: http{@shop}).tap do |client|
        client.allow_dynamic_queries = true
      end
  end

end

Rails can provide accessing instance variables inside block by yield or self.instance_eval(&block) But can we access @shop inside the headers method to make token as dynamic.

graphql-client http class is defined here

0 Answers
Related