updates.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { gql } from "@urql/core"
  2. import { GraphCacheUpdaters } 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. createTeamInvitation: (result, _args, cache, _info) => {
  60. cache.invalidate(
  61. {
  62. __typename: "Team",
  63. id: result.createTeamInvitation.teamID!,
  64. },
  65. "teamInvitations"
  66. )
  67. },
  68. acceptTeamInvitation: (_result, _args, cache, _info) => {
  69. cache.invalidate({ __typename: "Query" }, "myTeams")
  70. },
  71. revokeTeamInvitation: (_result, args, cache, _info) => {
  72. const targetTeamID = cache.resolve(
  73. {
  74. __typename: "TeamInvitation",
  75. id: args.inviteID,
  76. },
  77. "teamID"
  78. )
  79. if (typeof targetTeamID === "string") {
  80. const newInvites = (
  81. cache.resolve(
  82. {
  83. __typename: "Team",
  84. id: targetTeamID,
  85. },
  86. "teamInvitations"
  87. ) as string[]
  88. ).filter(
  89. (inviteKey) =>
  90. inviteKey !==
  91. cache.keyOfEntity({
  92. __typename: "TeamInvitation",
  93. id: args.inviteID,
  94. })
  95. )
  96. cache.link(
  97. { __typename: "Team", id: targetTeamID },
  98. "teamInvitations",
  99. newInvites
  100. )
  101. }
  102. },
  103. createShortcode: (result, _args, cache, _info) => {
  104. cache.writeFragment(
  105. gql`
  106. fragment _ on Shortcode {
  107. id
  108. request
  109. }
  110. `,
  111. {
  112. id: result.createShortcode.id,
  113. request: result.createShortcode.request,
  114. }
  115. )
  116. },
  117. },
  118. }