updates.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { gql } from "@urql/core"
  2. import { GraphCacheUpdaters, MyTeamsDocument } from "../graphql"
  3. export const updatesDef: GraphCacheUpdaters = {
  4. Subscription: {
  5. teamMemberAdded: (_r, { teamID }, cache, _info) => {
  6. cache.invalidate(
  7. {
  8. __typename: "Team",
  9. id: teamID,
  10. },
  11. "teamMembers"
  12. )
  13. },
  14. teamMemberUpdated: (_r, { teamID }, cache, _info) => {
  15. cache.invalidate(
  16. {
  17. __typename: "Team",
  18. id: teamID,
  19. },
  20. "teamMembers"
  21. )
  22. cache.invalidate(
  23. {
  24. __typename: "Team",
  25. id: teamID,
  26. },
  27. "myRole"
  28. )
  29. },
  30. teamMemberRemoved: (_r, { teamID }, cache, _info) => {
  31. cache.invalidate(
  32. {
  33. __typename: "Team",
  34. id: teamID,
  35. },
  36. "teamMembers"
  37. )
  38. },
  39. teamInvitationAdded: (_r, { teamID }, cache, _info) => {
  40. cache.invalidate(
  41. {
  42. __typename: "Team",
  43. id: teamID,
  44. },
  45. "teamInvitations"
  46. )
  47. },
  48. teamInvitationRemoved: (_r, { teamID }, cache, _info) => {
  49. cache.invalidate(
  50. {
  51. __typename: "Team",
  52. id: teamID,
  53. },
  54. "teamInvitations"
  55. )
  56. },
  57. },
  58. Mutation: {
  59. deleteTeam: (_r, { teamID }, cache, _info) => {
  60. cache.updateQuery(
  61. {
  62. query: MyTeamsDocument,
  63. },
  64. (data) => {
  65. if (data) {
  66. data.myTeams = data.myTeams.filter((x) => x.id !== teamID)
  67. }
  68. return data
  69. }
  70. )
  71. cache.invalidate({
  72. __typename: "Team",
  73. id: teamID,
  74. })
  75. },
  76. leaveTeam: (_r, { teamID }, cache, _info) => {
  77. cache.updateQuery(
  78. {
  79. query: MyTeamsDocument,
  80. },
  81. (data) => {
  82. if (data) {
  83. data.myTeams = data.myTeams.filter((x) => x.id !== teamID)
  84. }
  85. return data
  86. }
  87. )
  88. cache.invalidate({
  89. __typename: "Team",
  90. id: teamID,
  91. })
  92. },
  93. createTeam: (result, _args, cache, _info) => {
  94. cache.updateQuery(
  95. {
  96. query: MyTeamsDocument,
  97. },
  98. (data) => {
  99. if (data) data.myTeams.push(result.createTeam as any)
  100. return data
  101. }
  102. )
  103. },
  104. removeTeamMember: (_result, { teamID, userUid }, cache) => {
  105. const newMembers = (
  106. (cache.resolve(
  107. {
  108. __typename: "Team",
  109. id: teamID,
  110. },
  111. "teamMembers"
  112. ) as string[]) ?? []
  113. )
  114. .map((x) => [x, cache.resolve(x, "user") as string])
  115. .map(([key, userKey]) => [key, cache.resolve(userKey, "uid") as string])
  116. .filter(([_key, uid]) => uid !== userUid)
  117. .map(([key]) => key)
  118. cache.link({ __typename: "Team", id: teamID }, "teamMembers", newMembers)
  119. },
  120. createTeamInvitation: (result, _args, cache, _info) => {
  121. cache.invalidate(
  122. {
  123. __typename: "Team",
  124. id: result.createTeamInvitation.teamID!,
  125. },
  126. "teamInvitations"
  127. )
  128. },
  129. acceptTeamInvitation: (_result, _args, cache, _info) => {
  130. cache.invalidate({ __typename: "Query" }, "myTeams")
  131. },
  132. revokeTeamInvitation: (_result, args, cache, _info) => {
  133. const targetTeamID = cache.resolve(
  134. {
  135. __typename: "TeamInvitation",
  136. id: args.inviteID,
  137. },
  138. "teamID"
  139. )
  140. if (typeof targetTeamID === "string") {
  141. const newInvites = (
  142. cache.resolve(
  143. {
  144. __typename: "Team",
  145. id: targetTeamID,
  146. },
  147. "teamInvitations"
  148. ) as string[]
  149. ).filter(
  150. (inviteKey) =>
  151. inviteKey !==
  152. cache.keyOfEntity({
  153. __typename: "TeamInvitation",
  154. id: args.inviteID,
  155. })
  156. )
  157. cache.link(
  158. { __typename: "Team", id: targetTeamID },
  159. "teamInvitations",
  160. newInvites
  161. )
  162. }
  163. },
  164. createShortcode: (result, _args, cache, _info) => {
  165. cache.writeFragment(
  166. gql`
  167. fragment _ on Shortcode {
  168. id
  169. request
  170. }
  171. `,
  172. {
  173. id: result.createShortcode.id,
  174. request: result.createShortcode.request,
  175. }
  176. )
  177. },
  178. },
  179. }