0

I'm trying to get this apollo-link-state to work for my app which relies on a tremendous amount of nested data. I went this route because front loading all that data takes 20seconds, can't have that. So, i figure to load the bare minimal, and then when a user moves thruout the site, I make the necessary calls and merge it into cache.

Here is the problem. I have to define A DEFAULT data set, AND then I have to create the query for it. Is it really necessary to have a query that expects hundreds of data points, THEN define defaults for all of that.....That is soooo much work just to cache data. Every example I see about apollo-link-state is some silly 2 liner and everyone says "see how easy it is..", yeah, but its wholly not practical UNLESS I am missing something here... what are my options...

So, in my client file, I have

const defaults = {
    id: '',
    session: {
      id: '',
      user_id: '',
      first_name: '',
      last_name: '',
      admin: '',

    advertisers: {
      /// bunch of values
      administrators: {
        // bunch of values
        permissions: {
           // bunch of values
        }
      }
    },
    products: {
      automotive: {
        // bunch of nested object with values
      },
      sports: {
        // bunch of nested object with values
      },
      entertainment: {
        // bunch of nested object with values
      },
    },
};

const resolvers = {};

const cache = new InMemoryCache();
const stateLink = withClientState({ cache, defaults, resolvers });
const apolloClient = new ApolloClient({
  cache,
  link: ApolloLink.from([stateLink, new HttpLink({
    uri: `${config.url}/graphql`,
    credentials: 'include'
  })]),
  addTypename: true,
  dataIdFromObject
});

That defaults is not even half of what I need.

Then in my query, I call what I am willing to front load, then add @client to those I will "merge in on the client" side... BUT, now I have to manage two seperate files to achieve and there is alot of redundancy.. But my query is just a GIANT file because I need to cache everything there because as a user moves thru the site, they need different items from different parts of the query.

So, when I run this, it errors out because it expects EVERYTHING to be in that defaults that is in the SESSION_QUERY... keeping two seperate, gigantic, needs seems so......ugggggh. I must be missing something.

<Query query={SESSION_QUERY}>
  {({ loading, error, data }) => {
    // render appropriate component depending on loading/error state
    console.log("IN QUERY:::: ", loading, error, data, this.props.client.readQuery({ query: SESSION_QUERY }));


    return null;
  }}
</Query>

Now, I am getting errors because the "default" needs literally everything in it... I just don't see how this is maintainable.... apollo couldn't think of a better way? What am I missing?

james emanon
  • 11,185
  • 11
  • 56
  • 97
  • You said "when a user moves throughout the site, I make the necessary calls and merge it into cache" -- where are you fetching all this data from? One or more REST endpoints? – Daniel Rearden Feb 14 '19 at 21:39
  • graphql mutations & other queries.. so the goal is to have a subset of data loaded so I can load the site and have bare minimum data. THEN, as a user moves thru different parts of the app, I will QUERY additional items.. merge that into the session. Then if they fire mutations, I merge the results of that mutation into the session etc..... This would be sorta trivial in Redux, but.. in apollo..maybe I am doing something wrong. – james emanon Feb 14 '19 at 21:44
  • Apollo manages your cache for you for queries and mutations, so there's no need to utilize `apollo-link-state` on top of it. `apollo-link-state` is meant to let you utilize the same mechanism for app state that's specifically not related to your queries and mutations – Daniel Rearden Feb 14 '19 at 21:54
  • but does it manage the cache in a "state machine"? If not, then I need a mechanism for that right? So, lets say I query a product and ALL its data.. I need that data on another part of the site, maybe with a bunch of things the user "clicked on and mutated etc..", how do I then access that data on another part of the site? I can't wrap everything in nested – james emanon Feb 14 '19 at 22:01
  • I use to wrap all my components with the SESSION query, and yeah.. it would cache because I front loaded everything and when I "mutated" it got the update. great... BUT, now I totally bare-bones it.. so HOW do I have get it back in piecemeal into this same "SESSION".... that was the source of truth.. I want to maintain that. – james emanon Feb 14 '19 at 22:03
  • "Wouldn't it be better to wrap everything in ONE QUERY" -- maybe in some respects, but that's not how these libraries are intended to be used and there's performance drawbacks to that approach (everything rerenders any time your "session" is updated). – Daniel Rearden Feb 14 '19 at 22:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188448/discussion-between-daniel-rearden-and-james-emanon). – Daniel Rearden Feb 14 '19 at 22:11

0 Answers0