graphql-client
graphql-client copied to clipboard
KeyError: key not found: "data"
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)
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 :)
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
I have encounter the same issue.
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.
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.
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 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 thegraphql-client
gem. This works becausegraphql-client
uses the schema class fromgraphql
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.