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.

FetchMore result not passed to component if using union types

See original GitHub issue

Issue from: https://github.com/apollographql/react-apollo/issues/602 that @rpylipow reported.

I wasn’t quite sure if this belongs here or apollo-client as it somewhat relates to apollographql/apollo-client#1523, however my fetchMoreResult returns the expected results. The issue is that the new result isn’t passed into my component if I’m using Union types.

Intended outcome:
I’m attempting to use fetchMore with limit/offset. My query contains union types. I’d expect that after I concat fetchMoreResult with my previous result it would be passed into my component.

Actual outcome:
The new result isn’t passed into the component. I have an author prop which indicates the author of the post. It can be an individual user or a company (each entity has unique properties). If I remove the author prop from my GetPosts query, everything works as expected with the newResult being passed into my component. This leads me to believe that the union type is the culprit.

How to reproduce the issue:

import React from 'react';
import { View, Text } from 'react-native';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const GetPosts = gql`query GetPosts($limit: Int, $offset: Int) {
  posts(limit: $limit, offset: $offset) {
    _id
    message
    createdAt
    scheduledAt
    authorType 
    author {
      ... on User {
        _id
        avatarUrl
        profile {
          firstName
          lastName
        }
      }
      ... on Company {
        _id
        name
        logo80
      }
    }
  }
}
`;

class TestComponent extends React.Component {
  render() {
    // I would expect this to log the new props after press "loadMore"
    console.log(this.props)
    return (
      <View style={{marginTop: 40}}>
        <Text onPress={this.props.loadMore}>Push me</Text>
      </View>
    )
  }
}

export default graphql(GetPosts, {
  options: () => ({
    variables: {
      offset: 0,
      limit: 1
    }
  }),
  props: ({ ownProps, data }) => {
    return {
      ...ownProps,
      posts: data.posts,
      loadMore: () => {
        return data.fetchMore({
          variables: {
            offset: 1,
            limit: 1
          },
          updateQuery: (previousResult, { fetchMoreResult }) => {            
            if (!fetchMoreResult) {
              return previousResult;
            }
            const newResult = Object.assign({}, previousResult, {
              posts: [...previousResult.posts, ...fetchMoreResult.posts]
            });
            // This logs exactly what I expect it to log, but it isn't re-rendered into the component.
            console.log(newResult);

            return newResult;
          },
        })
      }
    };
  }
})(TestComponent);

Version

  • apollo-client@^1.0.1
  • react-apollo@^1.0.0
  • graphql-tag@^2.0.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lucasfelicianocommented, May 3, 2017

@helfer Indeed using the fragment matcher not only fix this issue also fix other issues related to caching.

1reaction
yury-dymovcommented, Apr 21, 2017

same problem for me

Here is reproduction: https://github.com/yury-dymov/react-apollo-error-template I figured out that for 0.9.0 everything works fine: https://github.com/yury-dymov/react-apollo-error-template/tree/0.9.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

FetchMore result not passed to component if using union types
The issue is that the new result isn't passed into my component if I'm using Union types. Intended outcome: I'm attempting to use...
Read more >
Advanced topics on caching in Apollo Client
This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache. Sometimes you shouldn't use the cache for...
Read more >
Apollo fetchMore updated props not re-rendering component
For re rendering the TestComponent you have change the state of the component. you can basically do this.setState({ posts: [old values + new ......
Read more >
Advanced Topics on Caching – Angular
Sometimes it makes sense to not use the cache for a specific operation. ... fetchMore can be used to update the result of...
Read more >
Redwood Utility Types | RedwoodJS Docs
Redwood Utility Types. Besides generating types for you, Redwood exposes a handful of utility types for Cells, Scenarios, and DbAuth.
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 Reddit Thread

No results found

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