updates.ts 3.9 KB

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