question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

readQuery without variables

See original GitHub issue

Intended outcome: I would like to be able to query the store without having to use the same variables in previous queries. For example, on a list page I have a query that looks like this:

query Company($cursor: String) {
    company {
      id
      locations(first: 10, after: $cursor) {
        pageInfo {
          # ...
        }
        edges {
          node {
            id
            # ...
          }
        }
      }
    }
}

I have a mutation to add a location and I’m attempting to load all locations associated with the company but it’s forcing me to add the first and after field arguments. I would like to grab all locations by using the following query:

const data = this.props.client.readQuery({
  query: gql` {
    company {
      healthySpots {
        edges {
          node {
            id
            # ...
          }
        }
      }
    }
  }`,
});

Actual outcome: The following error is being thrown:

Can't find field locations on object ($ROOT_QUERY.company) {
  "name": "App",
  "locations({\"first\":10,\"after\":null})": {
    "type": "id",
    "id": "$ROOT_QUERY.company.locations({\"first\":10,\"after\":null})",
    "generated": true
  },
  "__typename": "Company"
}.

If I include the first and after arguments with the same variables used on my list page then it works but this requires me to keep track of those variables from a different location in my app. It would be nice to just be able to skip variables if I preferred to not filter any fields.

How to reproduce the issue: If you are attempting to read a query from the store that was fulfilled with the use of variables then an error will be thrown.

Also, I have to applaud your efforts that have allowed me to be so much more productive than I was before Apollo was open sourced! Thank you for what you’re doing for the community!!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:22
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
devmetalcommented, Apr 11, 2018

Its still not easy, i have to track every single variables. The documentation holds just the easy use cases, but i have pagination, filtering and ordering, and the actions i have, modify records individually. The update mechanism is still hard and complicated

10reactions
kyosifovcommented, May 9, 2020

This is really embarrassing, how we cannot access a query without variables. I don’t think it will be much of a problem if we get queries with the same name. And we also can come up with different query names in GraphQL and get directly that query by the custom name, no by the whole GraphQL query with its variables.

It seems very frustrating to store queries with all their variables, just so we can update them later in some of the code. We need to create additional code even though apollo can crate an API that can be easily handled with custom name queries. With the custom names, we will not need to manage additional code for mutating queries.

Currently, in my real project, I have an add todo modal and want to modify the list that shows on the dashboard, to add the todo on top of the list, but I cannot access the query with readQuery, because it also requires the variables that are passed to the query. Well, this is a bummer because the components are not related.

I’ve been searching for any non-hacking ways, but it seems a bad API implementation to me.

Correct me if I am wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
Variables of mismatched types will return `null`. ... The query you provide readQuery can include fields that are not defined in your GraphQL...
Read more >
Updating Apollo Cache Without Variables - Stack Overflow
My question is, my readQuery calls always fail if I do not provide the exact same variables that I had previous provided to...
Read more >
Interacting with cached data - Apollo GraphQL - w3resource
You can provide GraphQL variables to readQuery like so: ... Unlike readQuery, calls to readFragment do not need to conform to the structure ......
Read more >
Reading and writing data to the cache - Client (React)
readQuery ({ query: gql` query ReadTodo($id: Int!) { todo(id: $id) { id text completed } } `, variables: { id: 5, }, });....
Read more >
Reading and Writing Data to the Cache - Ferry Graphql
You can provide GraphQL variables to readQuery like so: ... Unlike with readQuery , calls to readFragment do not need to conform to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found