Summary: My problem is that when I create a node using a Relay
mutation, the mutation creates the record in my database, but my client-side local data isn't updating, and the query response appears to be missing the new node.
Discussion: I think the RANGE_ADD
should add the new node, ListingRating
, to my local collection of ListingRating
s, which should automatically update the connected nodes (Listing
and User
). However, according to the relay
panel in react-dev-tools
, the mutation doesn't include any of the fields I need updated.
Questions:
Should RANGE_ADD
be sufficient here? If so, what's wrong with my implementation?
Do I have to update the Listing
and User
nodes manually? If so, how? Those nodes are only available deeply nested in the mutation payload
, and FIELDS_CHANGE
requires a fieldId
in the top-level of the payload
, right?
Schema: My schema holds Listing
s (adverts), User
s, and Rating
s (users' ratings of those Listings
). it looks something like this:
type Listing = {
...
ratings: [ListingRating]
}
type User = {
...
ratings: [ListingRating]
}
type ListingRating = {
rating: Int
user: User
listing: Listing
}
Mutation: I'm using scaphold.io as a back end, so don't have control over the mutation structures (though they're to Relay spec). They look like this:
mutation createListingRating(input: CreateListingRatingInput!) { CreateListingRatingPayload }
// CreateListingRatingInput
{
listingId: ID
listing: CreateListingInput
rating: Int
userId: ID
user: CreateUserInput
clientMutationId: ID
}
// CreateRatingPayload
{
changedListingRating: ListingRating
changedEdge: ListingRatingEdge
viewer: Viewer
clientMutationId: String
}
Relay mutation:
export default class CreateListingRating extends Relay.Mutation {
getMutation() {
return Relay.QL`
mutation {
createListingRating
}
`
}
getVariables() {
return {
userId: this.props.userId,
listingId: this.props.listingId,
rating: this.props.newRating,
}
}
getFatQuery() {
return Relay.QL`
fragment on CreateListingRatingPayload @relay(pattern: true){
changedEdge
changedListingRating {
listing
user
rating
}
viewer
}
`
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentID: this.props.viewer.id,
parentName: 'viewer',
connectionName: 'allListingRatings',
edgeName: 'changedEdge',
rangeBehaviors: {
'': 'append',
},
}]
}
}
Query: (not sure this is relevant) In my app, the query I use to display the ListingRating
s goes something like this:
query {
getUserGroup(id: 'some-group-id') {
users {
edges {
node {
listings {
edges {
node {
ratings {
edges {
node {
rating
}
}
}
}
}
}
}
}
}
}
}
Mutation request: (ie, what displays in the relay panel in react-dev-tools)
mutation CreateListingRating($input_0:CreateListingRatingInput!) {
createListingRating(input:$input_0) {
...F1
,
clientMutationId
}
}
fragment F0 on Viewer {
id,
user {
firstName
,
id
}
}
fragment F1 on CreateListingRatingPayload {
viewer {
...F0
}
}