Sidebar.vue 13 KB

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