2
0

updates.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { UpdatesConfig } from "@urql/exchange-graphcache"
  2. import { MyTeamsDocument } from "../graphql"
  3. export const updatesDef: Partial<UpdatesConfig> = {
  4. Mutation: {
  5. deleteTeam: (_r, { teamID }, cache, _info) => {
  6. cache.updateQuery(
  7. {
  8. query: MyTeamsDocument,
  9. },
  10. (data: any) => {
  11. data.myTeams = (data as any).myTeams.filter(
  12. (x: any) => x.id !== teamID
  13. )
  14. return data
  15. }
  16. )
  17. cache.invalidate({
  18. __typename: "Team",
  19. id: teamID as any,
  20. })
  21. },
  22. leaveTeam: (_r, { teamID }, cache, _info) => {
  23. cache.updateQuery(
  24. {
  25. query: MyTeamsDocument,
  26. },
  27. (data: any) => {
  28. data.myTeams = (data as any).myTeams.filter(
  29. (x: any) => x.id !== teamID
  30. )
  31. return data
  32. }
  33. )
  34. cache.invalidate({
  35. __typename: "Team",
  36. id: teamID as any,
  37. })
  38. },
  39. createTeam: (result, _args, cache, _info) => {
  40. cache.updateQuery(
  41. {
  42. query: MyTeamsDocument,
  43. },
  44. (data: any) => {
  45. data.myTeams.push(result.createTeam)
  46. return data
  47. }
  48. )
  49. },
  50. removeTeamMember: (_result, { teamID, userUid }, cache) => {
  51. const newMembers = (
  52. cache.resolve(
  53. {
  54. __typename: "Team",
  55. id: teamID as string,
  56. },
  57. "members"
  58. ) as string[]
  59. )
  60. .map((x) => [x, cache.resolve(x, "user") as string])
  61. .map(([key, userKey]) => [key, cache.resolve(userKey, "uid") as string])
  62. .filter(([_key, uid]) => uid !== userUid)
  63. .map(([key]) => key)
  64. cache.link(
  65. { __typename: "Team", id: teamID as string },
  66. "members",
  67. newMembers
  68. )
  69. },
  70. },
  71. }