relay-subscriptions
relay-subscriptions copied to clipboard
How to pass variables($input) to fields written within getSubscription?
My components have query fragments of the following form:
initialVariables : {
type : 'videos',
pageSize : 10,
community : 3
},
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
id,
newItemsAvailable(type : $type, community : $community),
newsFeedItems(first : $pageSize, type : $type, community : $community) {
pageInfo {
hasNextPage
},
edges {
cursor,
node {
...
}
}
}
}
`
},
I want to subscribe to changes in the field newItemsAvailable
. To do so I would need the following getSubscription function:
getSubscription() {
return Relay.QL`
subscription {
newsFeedItemsAvailableSubscription(input: $input) {
viewer {
newItemsAvailable(type : $type, community : $community)
}
}
}
`;
}
I can't omit (type : $type, community : $community)
from here since that would give an error. In the worst case I would have to send dummy hardcoded values.
I guess
subscriptions: [
({ viewer, type, community }) => new NewsFeedItemsAvailableSubscription({ viewer, type, community }),
]
and
getVariables() {
return {
id: this.props.viewer.id,
type: this.props.type,
community: this.props.community
};
}
feed the values of type and community into $input through ...subscription.getVariables()
. However, how can I make them available in newItemsAvailable(type : $type, community : $community)
. I haven't looked into the source code of Relay so have no clue of how all this generally works.
If the above isn't possible(using data from $input next to subfields), I feel the above snippets are sufficient to get an understanding of what I am trying to do here. What solution do you suggest? ${input.type} gives an error(Error: You supplied a GraphQL document that uses template substitution for an argument value, but variable substitution has not been enabled.) too. Does Relay store fields fetched using separate variables separately? If so, I think it makes sense to subscribe multiple times on the same field using different variables.
Since subscriptions are based on mutations, https://github.com/facebook/relay/issues/1046 seems relevant. Especially the comment by nodkz on the "new Mutation API".