GQLSession.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { distinctUntilChanged, pluck } from "rxjs/operators"
  2. import { GQLHeader, HoppGQLRequest, makeGQLRequest } from "@hoppscotch/data"
  3. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  4. import { useStream } from "~/helpers/utils/composables"
  5. type GQLSession = {
  6. request: HoppGQLRequest
  7. schema: string
  8. response: string
  9. }
  10. export const defaultGQLSession: GQLSession = {
  11. request: makeGQLRequest({
  12. name: "",
  13. url: "https://echo.hoppscotch.io/graphql",
  14. headers: [],
  15. variables: `{ "id": "1" }`,
  16. query: `query Request {
  17. method
  18. url
  19. headers {
  20. key
  21. value
  22. }
  23. }
  24. `,
  25. }),
  26. schema: "",
  27. response: "",
  28. }
  29. const dispatchers = defineDispatchers({
  30. setSession(_: GQLSession, { session }: { session: GQLSession }) {
  31. return session
  32. },
  33. setName(curr: GQLSession, { newName }: { newName: string }) {
  34. return {
  35. request: {
  36. ...curr.request,
  37. name: newName,
  38. },
  39. }
  40. },
  41. setURL(curr: GQLSession, { newURL }: { newURL: string }) {
  42. return {
  43. request: {
  44. ...curr.request,
  45. url: newURL,
  46. },
  47. }
  48. },
  49. setHeaders(curr: GQLSession, { headers }: { headers: GQLHeader[] }) {
  50. return {
  51. request: {
  52. ...curr.request,
  53. headers,
  54. },
  55. }
  56. },
  57. addHeader(curr: GQLSession, { header }: { header: GQLHeader }) {
  58. return {
  59. request: {
  60. ...curr.request,
  61. headers: [...curr.request.headers, header],
  62. },
  63. }
  64. },
  65. removeHeader(curr: GQLSession, { headerIndex }: { headerIndex: number }) {
  66. return {
  67. request: {
  68. ...curr.request,
  69. headers: curr.request.headers.filter((_x, i) => i !== headerIndex),
  70. },
  71. }
  72. },
  73. updateHeader(
  74. curr: GQLSession,
  75. {
  76. headerIndex,
  77. updatedHeader,
  78. }: { headerIndex: number; updatedHeader: GQLHeader }
  79. ) {
  80. return {
  81. request: {
  82. ...curr.request,
  83. headers: curr.request.headers.map((x, i) =>
  84. i === headerIndex ? updatedHeader : x
  85. ),
  86. },
  87. }
  88. },
  89. setQuery(curr: GQLSession, { newQuery }: { newQuery: string }) {
  90. return {
  91. request: {
  92. ...curr.request,
  93. query: newQuery,
  94. },
  95. }
  96. },
  97. setVariables(curr: GQLSession, { newVariables }: { newVariables: string }) {
  98. return {
  99. request: {
  100. ...curr.request,
  101. variables: newVariables,
  102. },
  103. }
  104. },
  105. setResponse(_: GQLSession, { newResponse }: { newResponse: string }) {
  106. return {
  107. response: newResponse,
  108. }
  109. },
  110. })
  111. export const gqlSessionStore = new DispatchingStore(
  112. defaultGQLSession,
  113. dispatchers
  114. )
  115. export function setGQLURL(newURL: string) {
  116. gqlSessionStore.dispatch({
  117. dispatcher: "setURL",
  118. payload: {
  119. newURL,
  120. },
  121. })
  122. }
  123. export function setGQLHeaders(headers: GQLHeader[]) {
  124. gqlSessionStore.dispatch({
  125. dispatcher: "setHeaders",
  126. payload: {
  127. headers,
  128. },
  129. })
  130. }
  131. export function addGQLHeader(header: GQLHeader) {
  132. gqlSessionStore.dispatch({
  133. dispatcher: "addHeader",
  134. payload: {
  135. header,
  136. },
  137. })
  138. }
  139. export function updateGQLHeader(headerIndex: number, updatedHeader: GQLHeader) {
  140. gqlSessionStore.dispatch({
  141. dispatcher: "updateHeader",
  142. payload: {
  143. headerIndex,
  144. updatedHeader,
  145. },
  146. })
  147. }
  148. export function removeGQLHeader(headerIndex: number) {
  149. gqlSessionStore.dispatch({
  150. dispatcher: "removeHeader",
  151. payload: {
  152. headerIndex,
  153. },
  154. })
  155. }
  156. export function clearGQLHeaders() {
  157. gqlSessionStore.dispatch({
  158. dispatcher: "setHeaders",
  159. payload: {
  160. headers: [],
  161. },
  162. })
  163. }
  164. export function setGQLQuery(newQuery: string) {
  165. gqlSessionStore.dispatch({
  166. dispatcher: "setQuery",
  167. payload: {
  168. newQuery,
  169. },
  170. })
  171. }
  172. export function setGQLVariables(newVariables: string) {
  173. gqlSessionStore.dispatch({
  174. dispatcher: "setVariables",
  175. payload: {
  176. newVariables,
  177. },
  178. })
  179. }
  180. export function setGQLResponse(newResponse: string) {
  181. gqlSessionStore.dispatch({
  182. dispatcher: "setResponse",
  183. payload: {
  184. newResponse,
  185. },
  186. })
  187. }
  188. export function getGQLSession() {
  189. return gqlSessionStore.value
  190. }
  191. export function setGQLSession(session: GQLSession) {
  192. gqlSessionStore.dispatch({
  193. dispatcher: "setSession",
  194. payload: {
  195. session,
  196. },
  197. })
  198. }
  199. export function useGQLRequestName() {
  200. return useStream(gqlName$, "", (newName) => {
  201. gqlSessionStore.dispatch({
  202. dispatcher: "setName",
  203. payload: { newName },
  204. })
  205. })
  206. }
  207. export const gqlName$ = gqlSessionStore.subject$.pipe(
  208. pluck("request", "name"),
  209. distinctUntilChanged()
  210. )
  211. export const gqlURL$ = gqlSessionStore.subject$.pipe(
  212. pluck("request", "url"),
  213. distinctUntilChanged()
  214. )
  215. export const gqlQuery$ = gqlSessionStore.subject$.pipe(
  216. pluck("request", "query"),
  217. distinctUntilChanged()
  218. )
  219. export const gqlVariables$ = gqlSessionStore.subject$.pipe(
  220. pluck("request", "variables"),
  221. distinctUntilChanged()
  222. )
  223. export const gqlHeaders$ = gqlSessionStore.subject$.pipe(
  224. pluck("request", "headers"),
  225. distinctUntilChanged()
  226. )
  227. export const gqlResponse$ = gqlSessionStore.subject$.pipe(
  228. pluck("response"),
  229. distinctUntilChanged()
  230. )