link.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { ApolloLink, createHttpLink, from } from '@apollo/client/core'
  3. import { BatchHttpLink } from '@apollo/client/link/batch-http'
  4. import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename'
  5. import { getMainDefinition } from '@apollo/client/utilities'
  6. import ActionCableLink from 'graphql-ruby-client/subscriptions/ActionCableLink'
  7. import { consumer } from '#shared/server/action_cable/consumer.ts'
  8. import connectedStateLink from './link/connectedState.ts'
  9. import csrfLink from './link/csrf.ts'
  10. import debugLink from './link/debug.ts'
  11. import errorLink from './link/error.ts'
  12. import setAuthorizationLink from './link/setAuthorization.ts'
  13. import testFlagsLink from './link/testFlags.ts'
  14. import getBatchContext from './utils/getBatchContext.ts'
  15. import type { Operation } from '@apollo/client/core'
  16. import type { FragmentDefinitionNode, OperationDefinitionNode } from 'graphql'
  17. // Should subsequent HTTP calls be batched together?
  18. const enableBatchLink = true
  19. // Should queries and mutations be sent over ActionCable?
  20. const enableQueriesOverWebsocket = false
  21. const connectionSettings = {
  22. uri: '/graphql',
  23. credentials: 'same-origin', // Must have for CSRF validation via Rails.
  24. }
  25. const noBatchLink = createHttpLink(connectionSettings)
  26. const batchLink = new BatchHttpLink({
  27. ...connectionSettings,
  28. batchMax: 5,
  29. batchInterval: 20,
  30. })
  31. const operationIsLoginLogout = (
  32. definition: OperationDefinitionNode | FragmentDefinitionNode,
  33. ) => {
  34. return !!(
  35. definition.kind === 'OperationDefinition' &&
  36. definition.operation === 'mutation' &&
  37. definition.name?.value &&
  38. ['login', 'logout'].includes(definition.name?.value)
  39. )
  40. }
  41. // TODO: Maybe we can also add a generic solution with the query context to exclude operation for batching or
  42. // run operations over websocket.
  43. const operationIsFormUpdater = (
  44. definition: OperationDefinitionNode | FragmentDefinitionNode,
  45. ) => {
  46. return !!(
  47. definition.kind === 'OperationDefinition' &&
  48. definition.operation === 'query' &&
  49. definition.name?.value &&
  50. definition.name?.value === 'formUpdater'
  51. )
  52. }
  53. const requiresBatchLink = (op: Operation) => {
  54. if (!enableBatchLink) return false
  55. const batchContext = getBatchContext(op)
  56. return batchContext.active
  57. }
  58. const httpLink = ApolloLink.split(requiresBatchLink, batchLink, noBatchLink)
  59. const requiresHttpLink = (op: Operation) => {
  60. const definition = getMainDefinition(op.query)
  61. if (!enableQueriesOverWebsocket) {
  62. // Only subscriptions over websocket.
  63. return (
  64. !(
  65. definition.kind === 'OperationDefinition' &&
  66. definition.operation === 'subscription'
  67. ) && !operationIsFormUpdater(definition)
  68. )
  69. }
  70. // Everything over websocket except login/logout as that changes cookies.
  71. return operationIsLoginLogout(definition)
  72. }
  73. const actionCableLink = new ActionCableLink({ cable: consumer })
  74. const splitLink = ApolloLink.split(requiresHttpLink, httpLink, actionCableLink)
  75. const link = from([
  76. ...(VITE_TEST_MODE ? [testFlagsLink] : []),
  77. csrfLink,
  78. errorLink,
  79. setAuthorizationLink,
  80. debugLink,
  81. connectedStateLink,
  82. removeTypenameFromVariables(),
  83. splitLink,
  84. ])
  85. export default link