Nithin raj
Nithin raj

Reputation: 81

`fetch': key not found: "data" (KeyError) : graphql-client error

I was trying to use the graphql query within ruby to fetch github repositories. Before writing the query, I was working on getting the graphql-client working. I'm facing issues with graphql client.

I was following this link for graphql client: https://github.com/github/graphql-client

require 'graphql'
require 'graphql/client'
require 'graphql/client/http'

module MyGraphQL
  HTTP = GraphQL::Client::HTTP.new('https://github.com/graphql') do
    def client_context
      { access_token: 'xxxxxxxxxxxxxxxxxx' }
    end

    def headers(_context)
      client_context
    end
  end

  Schema = GraphQL::Client.load_schema(HTTP)

  Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
end

I'm getting the following errors in a terminal:

    'fetch': key not found: "data" (KeyError)
    'load'
    'load_schema'
    'load_schema'

Upvotes: 6

Views: 3670

Answers (2)

woto
woto

Reputation: 3077

Also faced with same problem. Replacing body method to this helped:

    def headers(context)
      {
        "Authorization" => "bearer #{TOKEN}"
      }
    end

Where is the TOKEN is the Personal access token gotten from here: https://github.com/settings/tokens

Upvotes: 2

TolichP
TolichP

Reputation: 134

This error appears due to response you receive via fetching https://github.com/graphql.

You can use binding.pry (this gem), for example, to see what happening, when you try to run load_schema method. It tries to fetch data from response here: https://i.sstatic.net/DCTe0.png

But there is no data attribute, because you get {"errors"=>[{"message"=>"422 Unprocessable Entity"}]}

Try to fetch http://graphql-swapi.parseapp.com/, for example, worked for me.

Upvotes: 3

Related Questions