I'm rebuilding a small, internal web app with React/Relay/GraphQL to get familiar with this stack. Basically, it monitors analytics of a list of "active" videos. The only mutation is to replace the list of active video IDs with a new list. The problem is that after replacing the IDs, Relay continues to deliver the old list of IDs instead of the new.
I haven't been able to figure out how to manipulate the store that's passed to commitMutation()
's updater
and optimisticUpdater
callbacks. I just need to either clear out the stored list of active videos so it knows to call a fresh one or have it re-run the graphql query to refresh the cache.
Specifically, I need to clear the results from this query:
const ActiveVideosQuery = graphql`
query App_ActiveVideos_Query {
activeVideos {
...SetActiveVideosPage_activeVideos
...VideoList_activeVideos
}
}
`
The mutation (TypeScript):
const { commitMutation, graphql } = require('react-relay')
const mutation = graphql`
mutation SetActiveVideosMutation($input: SetActiveVideosInput!) {
setActiveVideos(input: $input) {
clientMutationId
}
}
`
let nextClientMutationId = 0
function commit(environment, ids: string[]) {
const clientMutationId = nextClientMutationId++
return commitMutation(environment, {
mutation,
variables: { input: { ids, clientMutationId } },
})
}
export default { commit }
And the schema:
type Channel {
id: ID!
name: String!
}
type Mutation {
setActiveVideos(input: SetActiveVideosInput!): SetActiveVideosPayload
}
type Query {
activeVideos: [Video]!
}
input SetActiveVideosInput {
ids: [ID]!
clientMutationId: String!
}
type SetActiveVideosPayload {
clientMutationId: String!
}
type Video {
id: ID!
active: Boolean!
details: VideoDetails
statsByAge(seconds: Int!): [VideoStats]!
}
type VideoDetails {
title: String!
description: String!
thumbnailURL: String!
publishedAt: String!
channel: Channel!
}
type VideoStats {
videoID: ID!
recordedAt: String!
views: String!
likes: String!
dislikes: String!
favorites: String!
comments: String!
}