Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
feat: codemirror for graphql query, scheme and response
liyasthomas457b6b982cfe
diff --git a/components/graphql/RequestOptions.vue b/components/graphql/RequestOptions.vueindex e8c7147f8..31da91239 100644--- a/components/graphql/RequestOptions.vue+++ b/components/graphql/RequestOptions.vue@@ -55,20 +55,7 @@/></div></div>- <GraphqlQueryEditor- ref="queryEditor"- v-model="gqlQueryString"- :on-run-g-q-l-query="runQuery"- :options="{- maxLines: Infinity,- minLines: 16,- autoScrollEditorIntoView: true,- showPrintMargin: false,- useWorker: false,- }"- styles="border-b border-dividerLight"- @update-query="updateQuery"- />+ <div ref="queryEditor" class="w-full block"></div></AppSection></SmartTab>@@ -284,6 +271,7 @@<script setup lang="ts">import { onMounted, ref, useContext, watch } from "@nuxtjs/composition-api"import clone from "lodash/clone"+import * as gql from "graphql"import { copyToClipboard } from "~/helpers/utils/clipboard"import {useNuxt,@@ -312,6 +300,9 @@ import { getCurrentStrategyID } from "~/helpers/network"import { makeGQLRequest } from "~/helpers/types/HoppGQLRequest"import { useCodemirror } from "~/helpers/editor/codemirror"import "codemirror/mode/javascript/javascript"+import jsonLinter from "~/helpers/editor/linting/json"+import { createGQLQueryLinter } from "~/helpers/editor/linting/gqlQuery"+import queryCompleter from "~/helpers/editor/completion/gqlQuery"const props = defineProps<{conn: GQLConnection@@ -363,13 +354,22 @@ const variableEditor = ref<any | null>(null)useCodemirror(variableEditor, variableString, {extendedEditorConfig: {- mode: "javascript",+ mode: "application/ld+json",},- linter: null,+ linter: jsonLinter,completer: null,})const queryEditor = ref<any | null>(null)+const schemaString = useReadonlyStream(props.conn.schema$, null)++useCodemirror(queryEditor, gqlQueryString, {+ extendedEditorConfig: {+ mode: "application/ld+json",+ },+ linter: createGQLQueryLinter(schemaString),+ completer: queryCompleter(schemaString),+})const copyQueryIcon = ref("copy")const prettifyQueryIcon = ref("align-left")@@ -466,7 +466,13 @@ const hideRequestModal = () => {}const prettifyQuery = () => {- queryEditor.value.prettifyQuery()+ try {+ gqlQueryString.value = gql.print(gql.parse(gqlQueryString.value))+ } catch (e) {+ $toast.error(t("error.gql_prettify_invalid_query").toString(), {+ icon: "error_outline",+ })+ }prettifyQueryIcon.value = "check"setTimeout(() => (prettifyQueryIcon.value = "align-left"), 1000)}@@ -475,11 +481,6 @@ const saveRequest = () => {showSaveRequestModal.value = true}-// Why ?-const updateQuery = (updatedQuery: string) => {- gqlQueryString.value = updatedQuery-}-const copyVariables = () => {copyToClipboard(variableString.value)copyVariablesIcon.value = "check"diff --git a/components/graphql/Response.vue b/components/graphql/Response.vueindex 09c6be4e1..ea7d86fe1 100644--- a/components/graphql/Response.vue+++ b/components/graphql/Response.vue@@ -18,6 +18,13 @@{{ $t("response.title") }}</label><div class="flex">+ <ButtonSecondary+ v-tippy="{ theme: 'tooltip' }"+ :title="$t('state.linewrap')"+ :class="{ '!text-accent': linewrapEnabled }"+ svg="corner-down-left"+ @click.native.prevent="linewrapEnabled = !linewrapEnabled"+ /><ButtonSecondaryref="downloadResponse"v-tippy="{ theme: 'tooltip' }"@@ -34,21 +41,7 @@/></div></div>- <SmartAceEditor- v-if="responseString"- :value="responseString"- :lang="'json'"- :lint="false"- :options="{- maxLines: Infinity,- minLines: 16,- autoScrollEditorIntoView: true,- readOnly: true,- showPrintMargin: false,- useWorker: false,- }"- styles="border-b border-dividerLight"- />+ <div v-if="responseString" ref="schemaEditor" class="w-full block"></div><divv-elseclass="@@ -72,19 +65,12 @@</template><script setup lang="ts">-import { PropType, ref, useContext } from "@nuxtjs/composition-api"-import { GQLConnection } from "~/helpers/GQLConnection"+import { reactive, ref, useContext } from "@nuxtjs/composition-api"+import { useCodemirror } from "~/helpers/editor/codemirror"import { copyToClipboard } from "~/helpers/utils/clipboard"import { useReadonlyStream } from "~/helpers/utils/composables"import { gqlResponse$ } from "~/newstore/GQLSession"-defineProps({- conn: {- type: Object as PropType<GQLConnection>,- required: true,- },-})-const {$toast,app: { i18n },@@ -93,6 +79,23 @@ const t = i18n.t.bind(i18n)const responseString = useReadonlyStream(gqlResponse$, "")+const schemaEditor = ref<any | null>(null)+const linewrapEnabled = ref(true)++useCodemirror(+ schemaEditor,+ responseString,+ reactive({+ extendedEditorConfig: {+ mode: "application/ld+json",+ readOnly: true,+ lineWrapping: linewrapEnabled,+ },+ linter: null,+ completer: null,+ })+)+const downloadResponseIcon = ref("download")const copyResponseIcon = ref("copy")diff --git a/components/graphql/Sidebar.vue b/components/graphql/Sidebar.vueindex bfc2c69c2..e1b0a0c1b 100644--- a/components/graphql/Sidebar.vue+++ b/components/graphql/Sidebar.vue@@ -149,6 +149,13 @@:title="$t('app.wiki')"svg="help-circle"/>+ <ButtonSecondary+ v-tippy="{ theme: 'tooltip' }"+ :title="$t('state.linewrap')"+ :class="{ '!text-accent': linewrapEnabled }"+ svg="corner-down-left"+ @click.native.prevent="linewrapEnabled = !linewrapEnabled"+ /><ButtonSecondaryref="downloadSchema"v-tippy="{ theme: 'tooltip' }"@@ -165,20 +172,11 @@/></div></div>- <SmartAceEditor+ <divv-if="schemaString"- v-model="schemaString"- :lang="'graphqlschema'"- :options="{- maxLines: Infinity,- minLines: 16,- autoScrollEditorIntoView: true,- readOnly: true,- showPrintMargin: false,- useWorker: false,- }"- styles="border-b border-dividerLight"- />+ ref="schemaEditor"+ class="w-full block"+ ></div><divv-elseclass="@@ -200,17 +198,17 @@</aside></template>-<script lang="ts">+<script setup lang="ts">import {computed,- defineComponent,nextTick,- PropType,+ reactive,ref,useContext,} from "@nuxtjs/composition-api"import { GraphQLField, GraphQLType } from "graphql"import { map } from "rxjs/operators"+import { useCodemirror } from "~/helpers/editor/codemirror"import { GQLConnection } from "~/helpers/GQLConnection"import { GQLHeader } from "~/helpers/types/HoppGQLRequest"import { copyToClipboard } from "~/helpers/utils/clipboard"@@ -285,186 +283,168 @@ type GQLHistoryEntry = {variables: string}-export default defineComponent({- props: {- conn: {- type: Object as PropType<GQLConnection>,- required: true,- },- },- setup(props) {- const {- $toast,- app: { i18n },- } = useContext()- const t = i18n.t.bind(i18n)-- const queryFields = useReadonlyStream(- props.conn.queryFields$.pipe(map((x) => x ?? [])),- []- )- const mutationFields = useReadonlyStream(- props.conn.mutationFields$.pipe(map((x) => x ?? [])),- []- )- const subscriptionFields = useReadonlyStream(- props.conn.subscriptionFields$.pipe(map((x) => x ?? [])),- []- )- const graphqlTypes = useReadonlyStream(- props.conn.graphqlTypes$.pipe(map((x) => x ?? [])),- []- )+const props = defineProps<{+ conn: GQLConnection+}>()- const downloadSchemaIcon = ref("download")- const copySchemaIcon = ref("copy")-- const graphqlFieldsFilterText = ref("")-- const gqlTabs = ref<any | null>(null)- const typesTab = ref<any | null>(null)-- const filteredQueryFields = computed(() => {- return getFilteredGraphqlFields(- graphqlFieldsFilterText.value,- queryFields.value as any- )- })-- const filteredMutationFields = computed(() => {- return getFilteredGraphqlFields(- graphqlFieldsFilterText.value,- mutationFields.value as any- )- })-- const filteredSubscriptionFields = computed(() => {- return getFilteredGraphqlFields(- graphqlFieldsFilterText.value,- subscriptionFields.value as any- )- })-- const filteredGraphqlTypes = computed(() => {- return getFilteredGraphqlTypes(- graphqlFieldsFilterText.value,- graphqlTypes.value as any- )- })-- const isGqlTypeHighlighted = (gqlType: GraphQLType) => {- if (!graphqlFieldsFilterText.value) return false-- return isTextFoundInGraphqlFieldObject(- graphqlFieldsFilterText.value,- gqlType as any- )- }+const {+ $toast,+ app: { i18n },+} = useContext()+const t = i18n.t.bind(i18n)- const getGqlTypeHighlightedFields = (gqlType: GraphQLType) => {- if (!graphqlFieldsFilterText.value) return []+const queryFields = useReadonlyStream(+ props.conn.queryFields$.pipe(map((x) => x ?? [])),+ []+)- const fields = Object.values((gqlType as any)._fields || {})- if (!fields || fields.length === 0) return []+const mutationFields = useReadonlyStream(+ props.conn.mutationFields$.pipe(map((x) => x ?? [])),+ []+)- return fields.filter((field) =>- isTextFoundInGraphqlFieldObject(- graphqlFieldsFilterText.value,- field as any- )- )- }+const subscriptionFields = useReadonlyStream(+ props.conn.subscriptionFields$.pipe(map((x) => x ?? [])),+ []+)- const handleJumpToType = async (type: GraphQLType) => {- gqlTabs.value.selectTab(typesTab.value)- await nextTick()+const graphqlTypes = useReadonlyStream(+ props.conn.graphqlTypes$.pipe(map((x) => x ?? [])),+ []+)- const rootTypeName = resolveRootType(type).name+const downloadSchemaIcon = ref("download")+const copySchemaIcon = ref("copy")- const target = document.getElementById(`type_${rootTypeName}`)- if (target) {- gqlTabs.value.$el- .querySelector(".gqlTabs")- .scrollTo({ top: target.offsetTop, behavior: "smooth" })- }- }- const schemaString = useReadonlyStream(- props.conn.schemaString$.pipe(map((x) => x ?? "")),- ""- )+const graphqlFieldsFilterText = ref("")- const downloadSchema = () => {- const dataToWrite = JSON.stringify(schemaString.value, null, 2)- const file = new Blob([dataToWrite], { type: "application/graphql" })- const a = document.createElement("a")- const url = URL.createObjectURL(file)- a.href = url- a.download = `${- url.split("/").pop()!.split("#")[0].split("?")[0]- }.graphql`- document.body.appendChild(a)- a.click()- downloadSchemaIcon.value = "check"- $toast.success(t("state.download_started").toString(), {- icon: "downloading",- })- setTimeout(() => {- document.body.removeChild(a)- URL.revokeObjectURL(url)- downloadSchemaIcon.value = "download"- }, 1000)- }+const gqlTabs = ref<any | null>(null)+const typesTab = ref<any | null>(null)- const copySchema = () => {- if (!schemaString.value) return+const filteredQueryFields = computed(() => {+ return getFilteredGraphqlFields(+ graphqlFieldsFilterText.value,+ queryFields.value as any+ )+})- copyToClipboard(schemaString.value)- copySchemaIcon.value = "check"- setTimeout(() => (copySchemaIcon.value = "copy"), 1000)- }+const filteredMutationFields = computed(() => {+ return getFilteredGraphqlFields(+ graphqlFieldsFilterText.value,+ mutationFields.value as any+ )+})- const handleUseHistory = (entry: GQLHistoryEntry) => {- const url = entry.url- const headers = entry.headers- const gqlQueryString = entry.query- const variableString = entry.variables- const responseText = entry.response-- setGQLURL(url)- setGQLHeaders(headers)- setGQLQuery(gqlQueryString)- setGQLVariables(variableString)- setGQLResponse(responseText)- props.conn.reset()- }+const filteredSubscriptionFields = computed(() => {+ return getFilteredGraphqlFields(+ graphqlFieldsFilterText.value,+ subscriptionFields.value as any+ )+})- return {- queryFields,- mutationFields,- subscriptionFields,- graphqlTypes,- schemaString,+const filteredGraphqlTypes = computed(() => {+ return getFilteredGraphqlTypes(+ graphqlFieldsFilterText.value,+ graphqlTypes.value as any+ )+})- graphqlFieldsFilterText,+const isGqlTypeHighlighted = (gqlType: GraphQLType) => {+ if (!graphqlFieldsFilterText.value) return false- filteredQueryFields,- filteredMutationFields,- filteredSubscriptionFields,- filteredGraphqlTypes,+ return isTextFoundInGraphqlFieldObject(+ graphqlFieldsFilterText.value,+ gqlType as any+ )+}- isGqlTypeHighlighted,- getGqlTypeHighlightedFields,+const getGqlTypeHighlightedFields = (gqlType: GraphQLType) => {+ if (!graphqlFieldsFilterText.value) return []- gqlTabs,- typesTab,- handleJumpToType,+ const fields = Object.values((gqlType as any)._fields || {})+ if (!fields || fields.length === 0) return []- downloadSchema,- downloadSchemaIcon,- copySchemaIcon,- copySchema,- handleUseHistory,- }- },-})+ return fields.filter((field) =>+ isTextFoundInGraphqlFieldObject(graphqlFieldsFilterText.value, field as any)+ )+}++const handleJumpToType = async (type: GraphQLType) => {+ gqlTabs.value.selectTab(typesTab.value)+ await nextTick()++ const rootTypeName = resolveRootType(type).name++ const target = document.getElementById(`type_${rootTypeName}`)+ if (target) {+ gqlTabs.value.$el+ .querySelector(".gqlTabs")+ .scrollTo({ top: target.offsetTop, behavior: "smooth" })+ }+}++const schemaString = useReadonlyStream(+ props.conn.schemaString$.pipe(map((x) => x ?? "")),+ ""+)++const schemaEditor = ref<any | null>(null)+const linewrapEnabled = ref(true)++useCodemirror(+ schemaEditor,+ schemaString,+ reactive({+ extendedEditorConfig: {+ mode: "application/ld+json",+ readOnly: true,+ lineWrapping: linewrapEnabled,+ },+ linter: null,+ completer: null,+ })+)++const downloadSchema = () => {+ const dataToWrite = JSON.stringify(schemaString.value, null, 2)+ const file = new Blob([dataToWrite], { type: "application/graphql" })+ const a = document.createElement("a")+ const url = URL.createObjectURL(file)+ a.href = url+ a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}.graphql`+ document.body.appendChild(a)+ a.click()+ downloadSchemaIcon.value = "check"+ $toast.success(t("state.download_started").toString(), {+ icon: "downloading",+ })+ setTimeout(() => {+ document.body.removeChild(a)+ URL.revokeObjectURL(url)+ downloadSchemaIcon.value = "download"+ }, 1000)+}++const copySchema = () => {+ if (!schemaString.value) return++ copyToClipboard(schemaString.value)+ copySchemaIcon.value = "check"+ setTimeout(() => (copySchemaIcon.value = "copy"), 1000)+}++const handleUseHistory = (entry: GQLHistoryEntry) => {+ const url = entry.url+ const headers = entry.headers+ const gqlQueryString = entry.query+ const variableString = entry.variables+ const responseText = entry.response++ setGQLURL(url)+ setGQLHeaders(headers)+ setGQLQuery(gqlQueryString)+ setGQLVariables(variableString)+ setGQLResponse(responseText)+ props.conn.reset()+}</script>diff --git a/layouts/default.vue b/layouts/default.vueindex 5bc5bfdcd..544568052 100644--- a/layouts/default.vue+++ b/layouts/default.vue@@ -24,7 +24,7 @@><Pane class="flex flex-1 hide-scrollbar !overflow-auto"><main class="flex flex-1 w-full">- <nuxt class="flex flex-1" />+ <nuxt class="flex overflow-y-auto flex-1" /></main></Pane></Splitpanes>