This is for a list where items can reordered where the mutation input is the reordered array, and the mutation result is a list of photos in the new order after they have been saved to the database. The actual mutation works great, but I don't know what an optimisticResponse
for an array is supposed to look like because it doesn't seem to happen.
input InputItemOrder {
key: String
order: Int
ref: InputPhoto
}
input InputPhoto {
public_id: String
secure_url: String
}
type Mutation {
orderMyPhotos(itemOrder: [InputItemOrder]): [Photo]
}
type Photo {
public_id: String
secure_url: String
order: Int
}
const ORDER_MY_PHOTOS_MUTATION = gql`
mutation OrderMyPhotos($itemOrder: [InputItemOrder]) {
orderMyPhotos(itemOrder: $itemOrder) {
public_id
secure_url
order
}
}
`;
graphql(ORDER_MY_PHOTOS_MUTATION, {
props: ({ ownProps, mutate }) => ({
order: (itemOrder) => mutate({
variables: { itemOrder },
optimisticResponse: {
__typename: 'Mutation',
orderMyPhotos: itemOrder.map(i => ({
__typename: 'Photo',
public_id: i.ref.public_id,
secure_url: i.ref.secure_url,
order: i.order,
})),
},
updateQueries: {
MyPhotos: (prev, { mutationResult }) => {
const orderedPhotos = mutationResult.data.orderMyPhotos;
return update(prev, {
userPhotos: {
$set: orderedPhotos,
},
});
},
},
}),
}),
}),