Vue-apollo: ApolloQuery scoped-slot supports `fetchMore` `refetch` etc

Created on 19 Oct 2018  路  4Comments  路  Source: vuejs/vue-apollo

Apollo React supports injecting functions like fetchMore, 'refetchetc together withdataloadingerror`: https://www.apollographql.com/docs/react/api/react-apollo.html#query-render-prop

Currently in vue-apollo it seems that we can only call fetchMore from an apollo option in the component. It would be wonderful to inject these functions to the ApolloQuery scoped-slot.

enhancement

Most helpful comment

I think the query is enough.
I have here is so works:

Product.vue:

...    
<ApolloQuery
  :query="require('@/graphql/Product.gql')"
  :variables="{ limit: $options.numOnPage }"
  #default="{ result: { loading, error, data }, query }"
>
  <masonry :products="data.product" />
  <infinite-loading @infinite="loadMore(query, $event)" />
</ApolloQuery>
...
<script>
...
  data: () => ({
    page: 1,
  }),
  numOnPage: NUM_ON_PAGE,
  methods: {
    loadMore(query, $state) {
      query.fetchMore({
        variables: {
          offset: this.page++ * NUM_ON_PAGE,
        },
        updateQuery: (prev, { fetchMoreResult }) => {
          if (!fetchMoreResult || fetchMoreResult.product.length === 0) {
            $state.complete();
            return prev;
          }
          $state.loaded();
          return Object.assign({}, prev, {
            product: [...prev.product, ...fetchMoreResult.product],
          });
        },
      });
    },
  },
...

All 4 comments

Would love this feature. $this.apollo.queries is always an empty object when Apollo components are used.

That would be very useful for pagination

I think the query is enough.
I have here is so works:

Product.vue:

...    
<ApolloQuery
  :query="require('@/graphql/Product.gql')"
  :variables="{ limit: $options.numOnPage }"
  #default="{ result: { loading, error, data }, query }"
>
  <masonry :products="data.product" />
  <infinite-loading @infinite="loadMore(query, $event)" />
</ApolloQuery>
...
<script>
...
  data: () => ({
    page: 1,
  }),
  numOnPage: NUM_ON_PAGE,
  methods: {
    loadMore(query, $state) {
      query.fetchMore({
        variables: {
          offset: this.page++ * NUM_ON_PAGE,
        },
        updateQuery: (prev, { fetchMoreResult }) => {
          if (!fetchMoreResult || fetchMoreResult.product.length === 0) {
            $state.complete();
            return prev;
          }
          $state.loaded();
          return Object.assign({}, prev, {
            product: [...prev.product, ...fetchMoreResult.product],
          });
        },
      });
    },
  },
...

that works great thank you !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wangxiangyao picture wangxiangyao  路  4Comments

ais-one picture ais-one  路  4Comments

apertureless picture apertureless  路  4Comments

danthareja picture danthareja  路  4Comments

AruXc picture AruXc  路  4Comments