-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddComment.tsx
102 lines (95 loc) · 3.2 KB
/
AddComment.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { useState } from 'react';
import { gql, useMutation, Reference } from '@apollo/client';
import { Modifier } from '@apollo/client/cache/core/types/common';
const addCommentMutation = gql`
mutation addComment($articleId: ID!, $content: String!) {
addComment(articleId: $articleId, content: $content) {
id
content
}
}
`;
function AddComment({articleId}: any) {
const [content, setContent] = useState('');
const [addComment] = useMutation(addCommentMutation, {
update(cache, {data: mutationData}) {
if (mutationData) {
const newCommentRef = cache.writeFragment({
data: mutationData.addComment,
fragment: gql`
fragment Comment on Comment {
id
content
}
`,
fragmentName: 'Comment',
});
const cursor = mutationData.addComment.id;
if (!newCommentRef) {
throw new Error("Couldn't write fragment to cache");
}
const handleComments: Modifier<{edges: Array<{node: Reference}>}> = (connection) => {
if (
connection.edges.some(
({node}) => node.__ref === newCommentRef.__ref,
)
) {
return connection;
}
return {
...connection,
edges: [
{
__typename: 'CommentEdge',
cursor,
node: newCommentRef,
},
...connection.edges,
],
};
};
// This won't work because we use cache redirects (how is the component supposed to know?)
// But since fetchMore will write a new query to the cache with the new elements appended it would be needed
// to cover such case as well
// Also, this would insert a new comment in ALL articles, because cache.modify doesn't expose args!
// You may think we could use writeQuery instead, but we don't have any knowledge about the "first"/"last" args
// You could omit "first"/"last" from the key args, but then how are you supposed to know if you need to
// append or prepend the new comment? Because you can paginate both forward AND backward depending on which
// argument you specify between "first" or "last".
// If cache.modify exposed the args that would be easy to fix
cache.modify({
fields: {
comments: handleComments,
},
});
// This will work with cache redirects, but if you triggered fetchMore you will have to run the previous one
cache.modify({
id: cache.identify({
__typename: 'Article',
id: articleId,
}),
fields: {
comments: handleComments,
},
});
}
},
});
return (
<>
<form onSubmit={event => {
addComment({
variables: {articleId, content},
});
event.preventDefault();
}}>
<label>
Nome:
<input type="text" value={content} onChange={event => setContent(event.target.value)} />
</label>
<input type="submit" value="Add comment" />
</form>
</>
);
}
export default AddComment;