graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

KeyError: key not found: "data"

Open tomaszwojcik opened this issue 4 years ago • 6 comments

I have an issue with query, it states the error from topic: KeyError: key not found: "data". In the playground I can make the request without problems. I'm not quite sure if there is schema issue or I'm doing something wrong.

I can do other query (login) without problems from the application level.

Screenshot from playground (It also has token header added, omitted in the screenshot) Screenshot 2020-12-04 at 16 03 13

My query in the application:

ProductsFeedFileUrlQuery = Api::Tim::Client::Client.parse <<~'GRAPHQL'
        query {
          productsFeed {
            products_feed_file_url
          }
        }
      GRAPHQL

Is there a way to omit validation? I know that that query exists :)

tomaszwojcik avatar Dec 04 '20 15:12 tomaszwojcik

I am not sure if this is still an issue but I ran into this issue as well. The data key it is talking about (or at least for me) was not referencing the query. It is referencing the introspection. So if you are stubbing a request in rspec and giving an example of the schema as a JSON (maybe using something like this to generate it. Sometimes, the output JSON from that does starts like this:

{
  "__schema": {
    ...
  }
}

instead of

{
  "data":{
    "__schema": {
      ...
    }
  }
}

This was the data key my error was referring to. Simply adding this to your introspection JSON fixes this issue. Hope this helps

svenjr avatar May 14 '21 11:05 svenjr

I have encounter the same issue.

G-shuu avatar Jun 04 '21 08:06 G-shuu

Looking at GraphQL::Schema::Loader, it appears to be incompatible with the schema provided by Tray.io; expecting to be able to access paths in the json like data -> __schema -> types -> names. None of that exists in this schema. I'm not sure if Tray.io is doing something strange or if graphql-client's parser only supports a subset of the spec.

jamesgecko avatar Feb 18 '22 01:02 jamesgecko

I had the same format problem as described by @svenjr and after that continued to get KeyError: key not found: "data" when using load_schema method.

I finally stopped using it and now just doing this :

Client = GraphQL::Client.new(schema: Rails.root.join("PATH/TO/SCHEMA.json").to_s, execute: HTTP)

Hope this can help.

micaeldekleyn avatar Feb 18 '22 11:02 micaeldekleyn

This morning I discovered that this library can support schema definitions written in the GraphQL SDL format (.graphql files) as well as introspection results (.json files).

SCHEMA_PATH = Rails.root.join('vendor', 'schemas', 'tray_io', 'schema.graphql').to_s
SCHEMA = ::GraphQL::Schema.from_definition(SCHEMA_PATH)
CLIENT = ::GraphQL::Client.new(schema: SCHEMA, execute: HTTP)

Note that I'm loading the schema using the underlying graphql gem, not the graphql-client gem. This works because graphql-client uses the schema class from graphql rather than defining it's own. This approach could break in the future if that ever changes.

This is similar to what PRs such as #254 are doing under the hood.

jamesgecko avatar Feb 18 '22 17:02 jamesgecko

This morning I discovered that this library can support schema definitions written in the GraphQL SDL format (.graphql files) as well as introspection results (.json files).

SCHEMA_PATH = Rails.root.join('vendor', 'schemas', 'tray_io', 'schema.graphql').to_s
SCHEMA = ::GraphQL::Schema.from_definition(SCHEMA_PATH)
CLIENT = ::GraphQL::Client.new(schema: SCHEMA, execute: HTTP)

Note that I'm loading the schema using the underlying graphql gem, not the graphql-client gem. This works because graphql-client uses the schema class from graphql rather than defining it's own. This approach could break in the future if that ever changes.

This is similar to what PRs such as #254 are doing under the hood.

This worked for me as I had failing CI tests trying to load a schema that was non-conforming.

ajistrying avatar May 05 '22 00:05 ajistrying