Filtering with Apollo cache resolvers












0















I've recently started using Apollo with React and trying to get my head around cache resolvers.



I'm trying to build a simple app to view information about TV shows/episodes, I have models for Show, Season and Episode.



The initial page is a list of shows, clicking on one will display information about that show and the list of seasons, then clicking on a season will display information about the season and a list of episodes.



Each component is being wrapped in a Query component to fetch the data it needs. When the app is started it fetches all the data for all shows/seasons so all future Queries should be getting the data from the cache.



I've added a cache resolver for shows which is correctly loading the show from the cache:



cacheRedirects: {
Query: {
show: (_, { id }, { getCacheKey }) => getCacheKey({
__typename: 'Show',
id,
}),
},
}


Where I'm having trouble is loading the data for the seasons. The query is:



query ShowSeasons($showId: ID!) {
seasons(showId: $showId) {
number
year
}
}


The data is already in the cache, the only difference to the Show query is that is is filtering by showId instead of id.



If I look at the cache using Apollo dev tools I can see that there is an entry for Show and Season which look like:



Show:5beec2785e33a2132a43ad98
id: "5beec2785e33a2132a43ad98"
title: "Show Title"
seasons: [Season]
0: Season:5beee33a378803199f23e0e9

Season:5beee33a378803199f23e0e9
id: "5beee33a378803199f23e0e9"
number: 1
year: 2018


I've tried playing around with the cache resolvers but every time I try to load the seasons it makes another network request instead of getting them from the cache.



How can I write a resolver that will fetch all seasons filtered by the showId?










share|improve this question



























    0















    I've recently started using Apollo with React and trying to get my head around cache resolvers.



    I'm trying to build a simple app to view information about TV shows/episodes, I have models for Show, Season and Episode.



    The initial page is a list of shows, clicking on one will display information about that show and the list of seasons, then clicking on a season will display information about the season and a list of episodes.



    Each component is being wrapped in a Query component to fetch the data it needs. When the app is started it fetches all the data for all shows/seasons so all future Queries should be getting the data from the cache.



    I've added a cache resolver for shows which is correctly loading the show from the cache:



    cacheRedirects: {
    Query: {
    show: (_, { id }, { getCacheKey }) => getCacheKey({
    __typename: 'Show',
    id,
    }),
    },
    }


    Where I'm having trouble is loading the data for the seasons. The query is:



    query ShowSeasons($showId: ID!) {
    seasons(showId: $showId) {
    number
    year
    }
    }


    The data is already in the cache, the only difference to the Show query is that is is filtering by showId instead of id.



    If I look at the cache using Apollo dev tools I can see that there is an entry for Show and Season which look like:



    Show:5beec2785e33a2132a43ad98
    id: "5beec2785e33a2132a43ad98"
    title: "Show Title"
    seasons: [Season]
    0: Season:5beee33a378803199f23e0e9

    Season:5beee33a378803199f23e0e9
    id: "5beee33a378803199f23e0e9"
    number: 1
    year: 2018


    I've tried playing around with the cache resolvers but every time I try to load the seasons it makes another network request instead of getting them from the cache.



    How can I write a resolver that will fetch all seasons filtered by the showId?










    share|improve this question

























      0












      0








      0








      I've recently started using Apollo with React and trying to get my head around cache resolvers.



      I'm trying to build a simple app to view information about TV shows/episodes, I have models for Show, Season and Episode.



      The initial page is a list of shows, clicking on one will display information about that show and the list of seasons, then clicking on a season will display information about the season and a list of episodes.



      Each component is being wrapped in a Query component to fetch the data it needs. When the app is started it fetches all the data for all shows/seasons so all future Queries should be getting the data from the cache.



      I've added a cache resolver for shows which is correctly loading the show from the cache:



      cacheRedirects: {
      Query: {
      show: (_, { id }, { getCacheKey }) => getCacheKey({
      __typename: 'Show',
      id,
      }),
      },
      }


      Where I'm having trouble is loading the data for the seasons. The query is:



      query ShowSeasons($showId: ID!) {
      seasons(showId: $showId) {
      number
      year
      }
      }


      The data is already in the cache, the only difference to the Show query is that is is filtering by showId instead of id.



      If I look at the cache using Apollo dev tools I can see that there is an entry for Show and Season which look like:



      Show:5beec2785e33a2132a43ad98
      id: "5beec2785e33a2132a43ad98"
      title: "Show Title"
      seasons: [Season]
      0: Season:5beee33a378803199f23e0e9

      Season:5beee33a378803199f23e0e9
      id: "5beee33a378803199f23e0e9"
      number: 1
      year: 2018


      I've tried playing around with the cache resolvers but every time I try to load the seasons it makes another network request instead of getting them from the cache.



      How can I write a resolver that will fetch all seasons filtered by the showId?










      share|improve this question














      I've recently started using Apollo with React and trying to get my head around cache resolvers.



      I'm trying to build a simple app to view information about TV shows/episodes, I have models for Show, Season and Episode.



      The initial page is a list of shows, clicking on one will display information about that show and the list of seasons, then clicking on a season will display information about the season and a list of episodes.



      Each component is being wrapped in a Query component to fetch the data it needs. When the app is started it fetches all the data for all shows/seasons so all future Queries should be getting the data from the cache.



      I've added a cache resolver for shows which is correctly loading the show from the cache:



      cacheRedirects: {
      Query: {
      show: (_, { id }, { getCacheKey }) => getCacheKey({
      __typename: 'Show',
      id,
      }),
      },
      }


      Where I'm having trouble is loading the data for the seasons. The query is:



      query ShowSeasons($showId: ID!) {
      seasons(showId: $showId) {
      number
      year
      }
      }


      The data is already in the cache, the only difference to the Show query is that is is filtering by showId instead of id.



      If I look at the cache using Apollo dev tools I can see that there is an entry for Show and Season which look like:



      Show:5beec2785e33a2132a43ad98
      id: "5beec2785e33a2132a43ad98"
      title: "Show Title"
      seasons: [Season]
      0: Season:5beee33a378803199f23e0e9

      Season:5beee33a378803199f23e0e9
      id: "5beee33a378803199f23e0e9"
      number: 1
      year: 2018


      I've tried playing around with the cache resolvers but every time I try to load the seasons it makes another network request instead of getting them from the cache.



      How can I write a resolver that will fetch all seasons filtered by the showId?







      reactjs apollo react-apollo apollo-client






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 8:50









      benben

      11




      11
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371105%2ffiltering-with-apollo-cache-resolvers%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371105%2ffiltering-with-apollo-cache-resolvers%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?