Sidebar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <template>
  2. <SmartTabs styles="sticky bg-primary z-10 top-0" vertical>
  3. <SmartTab
  4. :id="'history'"
  5. icon="clock"
  6. :label="`${t('tab.history')}`"
  7. :selected="true"
  8. >
  9. <History
  10. ref="graphqlHistoryComponent"
  11. :page="'graphql'"
  12. @useHistory="handleUseHistory"
  13. />
  14. </SmartTab>
  15. <SmartTab
  16. :id="'collections'"
  17. icon="folder"
  18. :label="`${t('tab.collections')}`"
  19. >
  20. <CollectionsGraphql />
  21. </SmartTab>
  22. <SmartTab
  23. :id="'docs'"
  24. icon="book-open"
  25. :label="`${t('tab.documentation')}`"
  26. >
  27. <AppSection label="docs">
  28. <div
  29. v-if="
  30. queryFields.length === 0 &&
  31. mutationFields.length === 0 &&
  32. subscriptionFields.length === 0 &&
  33. graphqlTypes.length === 0
  34. "
  35. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  36. >
  37. <img
  38. :src="`/images/states/${$colorMode.value}/add_comment.svg`"
  39. loading="lazy"
  40. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  41. :alt="`${t('empty.documentation')}`"
  42. />
  43. <span class="mb-4 text-center">
  44. {{ t("empty.documentation") }}
  45. </span>
  46. </div>
  47. <div v-else>
  48. <div class="bg-primary sticky top-0 z-10 flex">
  49. <input
  50. v-model="graphqlFieldsFilterText"
  51. type="search"
  52. autocomplete="off"
  53. :placeholder="`${t('action.search')}`"
  54. class="flex w-full p-4 py-2 bg-transparent"
  55. />
  56. <div class="flex">
  57. <ButtonSecondary
  58. v-tippy="{ theme: 'tooltip' }"
  59. to="https://docs.hoppscotch.io/quickstart/graphql"
  60. blank
  61. :title="t('app.wiki')"
  62. svg="help-circle"
  63. />
  64. </div>
  65. </div>
  66. <SmartTabs
  67. ref="gqlTabs"
  68. styles="border-t border-b border-dividerLight bg-primary sticky z-10 top-sidebarPrimaryStickyFold"
  69. >
  70. <div class="gqlTabs">
  71. <SmartTab
  72. v-if="queryFields.length > 0"
  73. :id="'queries'"
  74. :label="`${t('tab.queries')}`"
  75. :selected="true"
  76. class="divide-dividerLight divide-y"
  77. >
  78. <GraphqlField
  79. v-for="(field, index) in filteredQueryFields"
  80. :key="`field-${index}`"
  81. :gql-field="field"
  82. :jump-type-callback="handleJumpToType"
  83. class="p-4"
  84. />
  85. </SmartTab>
  86. <SmartTab
  87. v-if="mutationFields.length > 0"
  88. :id="'mutations'"
  89. :label="`${t('graphql.mutations')}`"
  90. class="divide-dividerLight divide-y"
  91. >
  92. <GraphqlField
  93. v-for="(field, index) in filteredMutationFields"
  94. :key="`field-${index}`"
  95. :gql-field="field"
  96. :jump-type-callback="handleJumpToType"
  97. class="p-4"
  98. />
  99. </SmartTab>
  100. <SmartTab
  101. v-if="subscriptionFields.length > 0"
  102. :id="'subscriptions'"
  103. :label="`${t('graphql.subscriptions')}`"
  104. class="divide-dividerLight divide-y"
  105. >
  106. <GraphqlField
  107. v-for="(field, index) in filteredSubscriptionFields"
  108. :key="`field-${index}`"
  109. :gql-field="field"
  110. :jump-type-callback="handleJumpToType"
  111. class="p-4"
  112. />
  113. </SmartTab>
  114. <SmartTab
  115. v-if="graphqlTypes.length > 0"
  116. :id="'types'"
  117. ref="typesTab"
  118. :label="`${t('tab.types')}`"
  119. class="divide-dividerLight divide-y"
  120. >
  121. <GraphqlType
  122. v-for="(type, index) in filteredGraphqlTypes"
  123. :key="`type-${index}`"
  124. :gql-type="type"
  125. :gql-types="graphqlTypes"
  126. :is-highlighted="isGqlTypeHighlighted(type)"
  127. :highlighted-fields="getGqlTypeHighlightedFields(type)"
  128. :jump-type-callback="handleJumpToType"
  129. />
  130. </SmartTab>
  131. </div>
  132. </SmartTabs>
  133. </div>
  134. </AppSection>
  135. </SmartTab>
  136. <SmartTab :id="'schema'" icon="box" :label="`${t('tab.schema')}`">
  137. <AppSection ref="schema" label="schema">
  138. <div
  139. v-if="schemaString"
  140. class="bg-primary border-dividerLight sticky top-0 z-10 flex items-center justify-between flex-1 pl-4 border-b"
  141. >
  142. <label class="text-secondaryLight font-semibold">
  143. {{ t("graphql.schema") }}
  144. </label>
  145. <div class="flex">
  146. <ButtonSecondary
  147. v-tippy="{ theme: 'tooltip' }"
  148. to="https://docs.hoppscotch.io/quickstart/graphql"
  149. blank
  150. :title="t('app.wiki')"
  151. svg="help-circle"
  152. />
  153. <ButtonSecondary
  154. v-tippy="{ theme: 'tooltip' }"
  155. :title="t('state.linewrap')"
  156. :class="{ '!text-accent': linewrapEnabled }"
  157. svg="corner-down-left"
  158. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  159. />
  160. <ButtonSecondary
  161. ref="downloadSchema"
  162. v-tippy="{ theme: 'tooltip' }"
  163. :title="t('action.download_file')"
  164. :svg="downloadSchemaIcon"
  165. @click.native="downloadSchema"
  166. />
  167. <ButtonSecondary
  168. ref="copySchemaCode"
  169. v-tippy="{ theme: 'tooltip' }"
  170. :title="t('action.copy')"
  171. :svg="copySchemaIcon"
  172. @click.native="copySchema"
  173. />
  174. </div>
  175. </div>
  176. <div v-if="schemaString" ref="schemaEditor"></div>
  177. <div
  178. v-else
  179. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  180. >
  181. <img
  182. :src="`/images/states/${$colorMode.value}/blockchain.svg`"
  183. loading="lazy"
  184. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  185. :alt="`${t('empty.schema')}`"
  186. />
  187. <span class="mb-4 text-center">
  188. {{ t("empty.schema") }}
  189. </span>
  190. </div>
  191. </AppSection>
  192. </SmartTab>
  193. </SmartTabs>
  194. </template>
  195. <script setup lang="ts">
  196. import { computed, nextTick, reactive, ref } from "@nuxtjs/composition-api"
  197. import { GraphQLField, GraphQLType } from "graphql"
  198. import { map } from "rxjs/operators"
  199. import { useCodemirror } from "~/helpers/editor/codemirror"
  200. import { GQLConnection } from "~/helpers/GQLConnection"
  201. import { GQLHeader } from "~/helpers/types/HoppGQLRequest"
  202. import { copyToClipboard } from "~/helpers/utils/clipboard"
  203. import {
  204. useReadonlyStream,
  205. useI18n,
  206. useToast,
  207. } from "~/helpers/utils/composables"
  208. import {
  209. setGQLHeaders,
  210. setGQLQuery,
  211. setGQLResponse,
  212. setGQLURL,
  213. setGQLVariables,
  214. } from "~/newstore/GQLSession"
  215. const t = useI18n()
  216. function isTextFoundInGraphqlFieldObject(
  217. text: string,
  218. field: GraphQLField<any, any>
  219. ) {
  220. const normalizedText = text.toLowerCase()
  221. const isFilterTextFoundInDescription = field.description
  222. ? field.description.toLowerCase().includes(normalizedText)
  223. : false
  224. const isFilterTextFoundInName = field.name
  225. .toLowerCase()
  226. .includes(normalizedText)
  227. return isFilterTextFoundInDescription || isFilterTextFoundInName
  228. }
  229. function getFilteredGraphqlFields(
  230. filterText: string,
  231. fields: GraphQLField<any, any>[]
  232. ) {
  233. if (!filterText) return fields
  234. return fields.filter((field) =>
  235. isTextFoundInGraphqlFieldObject(filterText, field)
  236. )
  237. }
  238. function getFilteredGraphqlTypes(filterText: string, types: GraphQLType[]) {
  239. if (!filterText) return types
  240. return types.filter((type) => {
  241. const isFilterTextMatching = isTextFoundInGraphqlFieldObject(
  242. filterText,
  243. type as any
  244. )
  245. if (isFilterTextMatching) {
  246. return true
  247. }
  248. const isFilterTextMatchingAtLeastOneField = Object.values(
  249. (type as any)._fields || {}
  250. ).some((field) => isTextFoundInGraphqlFieldObject(filterText, field as any))
  251. return isFilterTextMatchingAtLeastOneField
  252. })
  253. }
  254. function resolveRootType(type: GraphQLType) {
  255. let t: any = type
  256. while (t.ofType) t = t.ofType
  257. return t
  258. }
  259. type GQLHistoryEntry = {
  260. url: string
  261. headers: GQLHeader[]
  262. query: string
  263. response: string
  264. variables: string
  265. }
  266. const props = defineProps<{
  267. conn: GQLConnection
  268. }>()
  269. const toast = useToast()
  270. const queryFields = useReadonlyStream(
  271. props.conn.queryFields$.pipe(map((x) => x ?? [])),
  272. []
  273. )
  274. const mutationFields = useReadonlyStream(
  275. props.conn.mutationFields$.pipe(map((x) => x ?? [])),
  276. []
  277. )
  278. const subscriptionFields = useReadonlyStream(
  279. props.conn.subscriptionFields$.pipe(map((x) => x ?? [])),
  280. []
  281. )
  282. const graphqlTypes = useReadonlyStream(
  283. props.conn.graphqlTypes$.pipe(map((x) => x ?? [])),
  284. []
  285. )
  286. const downloadSchemaIcon = ref("download")
  287. const copySchemaIcon = ref("copy")
  288. const graphqlFieldsFilterText = ref("")
  289. const gqlTabs = ref<any | null>(null)
  290. const typesTab = ref<any | null>(null)
  291. const filteredQueryFields = computed(() => {
  292. return getFilteredGraphqlFields(
  293. graphqlFieldsFilterText.value,
  294. queryFields.value as any
  295. )
  296. })
  297. const filteredMutationFields = computed(() => {
  298. return getFilteredGraphqlFields(
  299. graphqlFieldsFilterText.value,
  300. mutationFields.value as any
  301. )
  302. })
  303. const filteredSubscriptionFields = computed(() => {
  304. return getFilteredGraphqlFields(
  305. graphqlFieldsFilterText.value,
  306. subscriptionFields.value as any
  307. )
  308. })
  309. const filteredGraphqlTypes = computed(() => {
  310. return getFilteredGraphqlTypes(
  311. graphqlFieldsFilterText.value,
  312. graphqlTypes.value as any
  313. )
  314. })
  315. const isGqlTypeHighlighted = (gqlType: GraphQLType) => {
  316. if (!graphqlFieldsFilterText.value) return false
  317. return isTextFoundInGraphqlFieldObject(
  318. graphqlFieldsFilterText.value,
  319. gqlType as any
  320. )
  321. }
  322. const getGqlTypeHighlightedFields = (gqlType: GraphQLType) => {
  323. if (!graphqlFieldsFilterText.value) return []
  324. const fields = Object.values((gqlType as any)._fields || {})
  325. if (!fields || fields.length === 0) return []
  326. return fields.filter((field) =>
  327. isTextFoundInGraphqlFieldObject(graphqlFieldsFilterText.value, field as any)
  328. )
  329. }
  330. const handleJumpToType = async (type: GraphQLType) => {
  331. gqlTabs.value.selectTab(typesTab.value)
  332. await nextTick()
  333. const rootTypeName = resolveRootType(type).name
  334. const target = document.getElementById(`type_${rootTypeName}`)
  335. if (target) {
  336. target.scrollIntoView({ block: "center", behavior: "smooth" })
  337. target.classList.add(
  338. "transition-all",
  339. "ring-inset",
  340. "ring-accentLight",
  341. "ring-4"
  342. )
  343. setTimeout(
  344. () =>
  345. target.classList.remove(
  346. "ring-inset",
  347. "ring-accentLight",
  348. "ring-4",
  349. "transition-all"
  350. ),
  351. 2000
  352. )
  353. }
  354. }
  355. const schemaString = useReadonlyStream(
  356. props.conn.schemaString$.pipe(map((x) => x ?? "")),
  357. ""
  358. )
  359. const schemaEditor = ref<any | null>(null)
  360. const linewrapEnabled = ref(true)
  361. useCodemirror(
  362. schemaEditor,
  363. schemaString,
  364. reactive({
  365. extendedEditorConfig: {
  366. mode: "graphql",
  367. readOnly: true,
  368. lineWrapping: linewrapEnabled,
  369. },
  370. linter: null,
  371. completer: null,
  372. })
  373. )
  374. const downloadSchema = () => {
  375. const dataToWrite = JSON.stringify(schemaString.value, null, 2)
  376. const file = new Blob([dataToWrite], { type: "application/graphql" })
  377. const a = document.createElement("a")
  378. const url = URL.createObjectURL(file)
  379. a.href = url
  380. a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}.graphql`
  381. document.body.appendChild(a)
  382. a.click()
  383. downloadSchemaIcon.value = "check"
  384. toast.success(`${t("state.download_started")}`)
  385. setTimeout(() => {
  386. document.body.removeChild(a)
  387. URL.revokeObjectURL(url)
  388. downloadSchemaIcon.value = "download"
  389. }, 1000)
  390. }
  391. const copySchema = () => {
  392. if (!schemaString.value) return
  393. copyToClipboard(schemaString.value)
  394. copySchemaIcon.value = "check"
  395. setTimeout(() => (copySchemaIcon.value = "copy"), 1000)
  396. }
  397. const handleUseHistory = (entry: GQLHistoryEntry) => {
  398. const url = entry.url
  399. const headers = entry.headers
  400. const gqlQueryString = entry.query
  401. const variableString = entry.variables
  402. const responseText = entry.response
  403. setGQLURL(url)
  404. setGQLHeaders(headers)
  405. setGQLQuery(gqlQueryString)
  406. setGQLVariables(variableString)
  407. setGQLResponse(responseText)
  408. props.conn.reset()
  409. }
  410. </script>