GQLSession.ts 5.1 KB

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