Sidebar.vue 12 KB

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