Sibelius Seraphini
Sibelius Seraphini

Reputation: 5633

How can I check if a connection has new items in Relay?

I have the following Relay container in my Feed component, that it is a simple list of Posts

export default Relay.createContainer(Feed, {
  initialVariables: {
    count: 5,
  },
  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        posts(first: $count) {
          pageInfo {
              hasNextPage
          }
          edges {
            node {
              id,
              body
            },
          },
        },
      },
    `,
  },
});

Theses posts are organized by the most recent ones, how can I check if there are newer posts in my graphql server? I want to do a pulling strategy until I migrate it to graphql/relay subscriptions

Can I use forceFetch? I don't wanna to update Relay Store, I just wanna to notify the user that has new posts

Upvotes: 1

Views: 77

Answers (1)

velop
velop

Reputation: 3224

One way I could think of is to have an endpoint which you supply with the latest timestamp. This endpoint returns for example the ids of the newer posts. On relay side you do a setTimeout(() => { this.props.relay.setVariables({timestamp: timestamp}); }, 60000) and in the fragment something like:

const FooBar = Relay.createContainer(FooBarClass, {
initialVariables: {
    timestamp: null
},
prepareVariables: vars => {
    vars['timestampIsTruthy'] = !!vars['timestamp'];
    return vars;
},
fragments: {
    store: () => Relay.QL`
        fragment on Query {
            newerPosts(timestamp: $timestamp, first: 3) @include(if: $queryIsTruthy) {
                totalCount
                edges {
                    node {
                        id
                    }
                }
            }
        }`,
},
});

Upvotes: 1

Related Questions