link.ts 3.2 KB

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