0
class GithubGraphqlService
    URL = "https://api.github.com/graphql".freeze
    attr_reader :access_token
    
    def initialize(access_token)
        @access_token = access_token
    end

    def client
        http_adapter ||= GraphQL::Client::HTTP.new(URL) do
          def headers(_context)
            {
              "Authorization" => "Bearer #{access_token}",  //access_token is undefined
              "User-Agent" => "Ruby"
            }
          end
        end
    
        schema ||= GraphQL::Client.load_schema(http_adapter)
        @_client ||= GraphQL::Client.new(schema:, execute: http_adapter)
   end
end

I have tried the following fixes but nothing worked.

Fix: 1

def client
    http_adapter ||= GraphQL::Client::HTTP.new(URL) do
      def headers(_context)
        {
          "Authorization" => "Bearer #{@access_token}",  //@access_token is nil
          "User-Agent" => "Ruby"
        }
      end
    end

    schema ||= GraphQL::Client.load_schema(http_adapter)
    @_client ||= GraphQL::Client.new(schema:, execute: http_adapter)
end

Fix: 2

def client
    github_access_token = @access_token
    http_adapter ||= GraphQL::Client::HTTP.new(URL) do
      def headers(_context)
        {
          "Authorization" => "Bearer #{github_access_token}",  //github_access_token is undefined
          "User-Agent" => "Ruby"
        }
      end
    end

    schema ||= GraphQL::Client.load_schema(http_adapter)
    @_client ||= GraphQL::Client.new(schema:, execute: http_adapter)
end

The headers method is defined within the context of the GraphQL::Client::HTTP class, which means it cannot access instance variables defined within the GithubGraphqlService class, including @access_token.

Is there any way to access, the @access_token instance variable inside the headers method?

Thanks!

2 Answers2

0

So, by design they want you to inject it at query level

It'd look something like this

# ...
def client
  http_adapter ||= GraphQL::Client::HTTP.new(URL) do
    def headers(context)
      access_token = context.fetch(:access_token)

      {
        "Authorization" => "Bearer #{access_token}",
        "User-Agent" => "Ruby"
      }
    end
  end

  schema ||= GraphQL::Client.load_schema(http_adapter)

  @_client ||= GraphQL::Client.new(schema:, execute: http_adapter)
end
# ...
client.query(query_definition, context: { access_token: '...' })
v0rs4
  • 106
  • 5
0

I find a fix for this, maybe this will help others.

After going through these two github comments:

https://github.com/github/graphql-client/issues/192#issuecomment-503493557 https://github.com/github/graphql-client/issues/210#issuecomment-542693381

I updated my code something like this:

def client
  http_adapter ||= GraphQL::Client::HTTP.new(URL) do
    def headers(context)
      access_token = context[:access_token]

      {
        "Authorization" => "Bearer #{access_token}",
        "User-Agent" => "Ruby"
      }
    end
  end

  schema ||= GraphQL::Client.load_schema(GraphQL::Client.dump_schema(http_adapter, nil, context: { access_token: 'YOUR-ACCESS-TOKEN' }))

  @_client ||= GraphQL::Client.new(schema:, execute: http_adapter)
end
client.query(query_definition, context: { access_token: 'YOUR-ACCESS-TOKEN' })

Then it worked for me.