api-client/pages/graphql.vue

720 lines
22 KiB
Vue
Raw Normal View History

2019-11-18 00:48:29 +00:00
<template>
<div class="page">
<div class="content">
<div class="page-columns inner-left">
<pw-section class="blue" :label="$t('endpoint')" ref="endpoint">
<ul>
<li>
2019-11-28 12:27:52 +00:00
<label for="url">{{ $t("url") }}</label>
<input
id="url"
type="url"
v-model="url"
2020-02-25 06:43:55 +00:00
spellcheck="false"
@keyup.enter="getSchema()"
/>
</li>
2019-11-21 01:25:37 +00:00
<div>
<li>
<label for="get" class="hide-on-small-screen">&nbsp;</label>
<button id="get" name="get" @click="getSchema">
2019-11-28 12:27:52 +00:00
{{ $t("get_schema") }}
2019-11-21 01:25:37 +00:00
<span><i class="material-icons">send</i></span>
</button>
</li>
</div>
</ul>
</pw-section>
2019-11-19 14:59:18 +00:00
<pw-section class="orange" :label="$t('headers')" ref="headers">
2019-11-26 03:48:36 +00:00
<ul>
<li>
<div class="flex-wrap">
2019-11-28 12:27:52 +00:00
<label for="headerList">{{ $t("header_list") }}</label>
2019-11-26 03:48:36 +00:00
<div>
2020-02-25 16:10:40 +00:00
<button class="icon" @click="headers = []" v-tooltip.bottom="$t('clear')">
2019-11-26 03:48:36 +00:00
<i class="material-icons">clear_all</i>
</button>
</div>
</div>
<textarea
id="headerList"
readonly
v-textarea-auto-height="headerString"
v-model="headerString"
:placeholder="$t('add_one_header')"
2019-11-26 03:48:36 +00:00
rows="1"
></textarea>
</li>
</ul>
<ul v-for="(header, index) in headers" :key="index">
<li>
<autocomplete
:placeholder="$t('header_count', { count: index + 1 })"
:source="commonHeaders"
:spellcheck="false"
2019-11-26 03:48:36 +00:00
:value="header.key"
@input="
2019-11-26 03:48:36 +00:00
$store.commit('setGQLHeaderKey', {
index,
2020-02-25 16:10:40 +00:00
value: $event,
2019-11-26 03:48:36 +00:00
})
"
autofocus
/>
</li>
<li>
<input
:placeholder="$t('value_count', { count: index + 1 })"
2019-11-26 03:48:36 +00:00
:name="'value' + index"
:value="header.value"
@change="
$store.commit('setGQLHeaderValue', {
index,
2020-02-25 16:10:40 +00:00
value: $event.target.value,
2019-11-26 03:48:36 +00:00
})
"
autofocus
/>
</li>
<div>
<li>
<button
class="icon"
@click="removeRequestHeader(index)"
v-tooltip.bottom="$t('delete')"
2019-11-26 03:48:36 +00:00
id="header"
>
2019-11-26 14:31:48 +00:00
<i class="material-icons">delete</i>
2019-11-26 03:48:36 +00:00
</button>
</li>
</div>
</ul>
<ul>
<li>
<button class="icon" @click="addRequestHeader">
<i class="material-icons">add</i>
2019-11-28 12:27:52 +00:00
<span>{{ $t("add_new") }}</span>
2019-11-26 03:48:36 +00:00
</button>
</li>
</ul>
</pw-section>
<pw-section class="green" :label="$t('schema')" ref="schema">
<div class="flex-wrap">
2020-02-29 16:42:12 +00:00
<label>{{ $t("schema") }}</label>
<div v-if="schema">
<button
class="icon"
@click="ToggleExpandResponse"
ref="ToggleExpandResponse"
v-tooltip="{
2020-02-25 16:10:40 +00:00
content: !expandResponse ? $t('expand_response') : $t('collapse_response'),
}"
>
2019-12-02 15:20:20 +00:00
<i class="material-icons">
{{ !expandResponse ? "unfold_more" : "unfold_less" }}
</i>
</button>
<button
class="icon"
@click="downloadResponse"
ref="downloadResponse"
v-tooltip="$t('download_file')"
>
<i class="material-icons">get_app</i>
</button>
<button
class="icon"
ref="copySchemaCode"
@click="copySchema"
v-tooltip="$t('copy_schema')"
>
<i class="material-icons">file_copy</i>
</button>
</div>
</div>
<Editor
2020-02-29 16:42:12 +00:00
v-if="schema"
:value="schema"
:lang="'graphqlschema'"
:options="{
maxLines: responseBodyMaxLines,
2020-02-29 16:42:12 +00:00
minLines: 16,
fontSize: '16px',
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
2020-02-25 16:10:40 +00:00
useWorker: false,
}"
/>
2020-02-29 16:42:12 +00:00
<input
v-else
class="missing-data-response"
:value="$t('waiting_receive_schema')"
ref="status"
id="status"
name="status"
readonly
type="text"
/>
</pw-section>
2019-12-29 00:47:00 +00:00
<pw-section class="cyan" :label="$t('query')" ref="query">
<div class="flex-wrap gqlRunQuery">
2019-11-28 12:27:52 +00:00
<label for="gqlQuery">{{ $t("query") }}</label>
2019-11-26 20:37:41 +00:00
<div>
<button
@click="runQuery()"
v-tooltip.bottom="`${$t('run_query')} (${getSpecialKey()}-Enter)`"
>
2019-11-26 20:37:41 +00:00
<i class="material-icons">play_arrow</i>
</button>
2019-11-26 21:48:13 +00:00
<button
class="icon"
@click="copyQuery"
ref="copyQueryButton"
v-tooltip="$t('copy_query')"
2019-11-26 21:48:13 +00:00
>
<i class="material-icons">file_copy</i>
</button>
<button
class="icon"
@click="doPrettifyQuery"
v-tooltip="`${$t('prettify_query')} (${getSpecialKey()}-P)`"
>
<i class="material-icons">photo_filter</i>
</button>
2019-11-26 20:37:41 +00:00
</div>
</div>
<QueryEditor
ref="queryEditor"
v-model="gqlQueryString"
2020-02-28 02:33:43 +00:00
:onRunGQLQuery="runQuery"
:options="{
maxLines: responseBodyMaxLines,
2020-02-29 16:42:12 +00:00
minLines: 10,
fontSize: '16px',
autoScrollEditorIntoView: true,
showPrintMargin: false,
2020-02-25 16:10:40 +00:00
useWorker: false,
}"
/>
</pw-section>
<pw-section class="yellow" label="Variables" ref="variables">
<Editor
v-model="variableString"
:lang="'json'"
:options="{
2020-02-29 16:42:12 +00:00
maxLines: 10,
minLines: 5,
fontSize: '16px',
autoScrollEditorIntoView: true,
showPrintMargin: false,
2020-02-25 16:10:40 +00:00
useWorker: false,
}"
/>
2019-11-26 20:37:41 +00:00
</pw-section>
2019-12-29 00:47:00 +00:00
2019-11-26 20:37:41 +00:00
<pw-section class="purple" label="Response" ref="response">
2019-11-26 21:55:43 +00:00
<div class="flex-wrap">
2019-11-28 12:27:52 +00:00
<label for="responseField">{{ $t("response") }}</label>
2019-11-26 21:55:43 +00:00
<div>
<button
class="icon"
@click="copyResponse"
ref="copyResponseButton"
v-tooltip="$t('copy_response')"
2019-11-26 21:55:43 +00:00
>
<i class="material-icons">file_copy</i>
</button>
</div>
</div>
2019-11-26 20:37:41 +00:00
<Editor
2020-02-29 16:42:12 +00:00
v-if="response"
:value="response"
2019-11-26 20:37:41 +00:00
:lang="'json'"
:lint="false"
2019-11-26 20:37:41 +00:00
:options="{
maxLines: responseBodyMaxLines,
2020-02-29 16:42:12 +00:00
minLines: 10,
2019-11-26 20:37:41 +00:00
fontSize: '16px',
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
2020-02-25 16:10:40 +00:00
useWorker: false,
2019-11-26 20:37:41 +00:00
}"
/>
2020-02-29 16:42:12 +00:00
<input
v-else
class="missing-data-response"
:value="$t('waiting_receive_response')"
ref="status"
id="status"
name="status"
readonly
type="text"
/>
2019-11-26 20:37:41 +00:00
</pw-section>
</div>
<aside class="sticky-inner inner-right">
<pw-section class="purple" :label="$t('docs')" ref="docs">
<section>
2020-03-08 03:41:25 +00:00
<tabs ref="gqlTabs">
<div class="gqlTabs">
2020-03-08 16:22:04 +00:00
<tab
v-if="queryFields.length > 0"
:id="'queries'"
:label="$t('queries')"
:selected="true"
>
2020-03-08 03:41:25 +00:00
<div v-for="field in queryFields" :key="field.name">
<gql-field :gqlField="field" :jumpTypeCallback="handleJumpToType" />
</div>
</tab>
2020-03-08 16:22:04 +00:00
<tab v-if="mutationFields.length > 0" :id="'mutations'" :label="$t('mutations')">
2020-03-08 03:41:25 +00:00
<div v-for="field in mutationFields" :key="field.name">
<gql-field :gqlField="field" :jumpTypeCallback="handleJumpToType" />
</div>
</tab>
2020-03-08 16:22:04 +00:00
<tab
v-if="subscriptionFields.length > 0"
:id="'subscriptions'"
:label="$t('subscriptions')"
>
2020-03-08 03:41:25 +00:00
<div v-for="field in subscriptionFields" :key="field.name">
<gql-field :gqlField="field" :jumpTypeCallback="handleJumpToType" />
</div>
</tab>
2020-03-08 16:22:04 +00:00
<tab v-if="gqlTypes.length > 0" :id="'types'" :label="$t('types')" ref="typesTab">
2020-03-08 03:41:25 +00:00
<div v-for="type in gqlTypes" :key="type.name" :id="`type_${type.name}`">
<gql-type :gqlType="type" :jumpTypeCallback="handleJumpToType" />
</div>
</tab>
</div>
2020-03-07 13:10:37 +00:00
</tabs>
</section>
2019-12-29 00:47:00 +00:00
<p
v-if="
queryFields.length === 0 &&
mutationFields.length === 0 &&
subscriptionFields.length === 0 &&
gqlTypes.length === 0
2019-12-29 00:47:00 +00:00
"
class="info"
>
{{ $t("send_request_first") }}
</p>
</pw-section>
</aside>
</div>
2019-11-18 00:48:29 +00:00
</div>
</template>
<style scoped lang="scss">
2020-03-08 03:41:25 +00:00
.gqlTabs {
2019-11-27 08:45:11 +00:00
max-height: calc(100vh - 186px);
overflow: auto;
}
.gqlRunQuery {
margin-bottom: 12px;
}
</style>
2019-11-18 00:48:29 +00:00
<script>
2020-02-25 16:10:40 +00:00
import axios from "axios"
import * as gql from "graphql"
import textareaAutoHeight from "../directives/textareaAutoHeight"
import { commonHeaders } from "../functions/headers"
2020-03-03 11:57:53 +00:00
import AceEditor from "../components/ui/ace-editor"
2020-02-25 16:10:40 +00:00
import QueryEditor from "../components/graphql/queryeditor"
import { getPlatformSpecialKey } from "~/functions/platformutils"
2020-02-25 16:10:40 +00:00
import { sendNetworkRequest } from "../functions/network"
2019-11-18 00:48:29 +00:00
export default {
2019-11-26 03:48:36 +00:00
directives: {
2020-02-25 16:10:40 +00:00
textareaAutoHeight,
2019-11-26 03:48:36 +00:00
},
2019-11-18 00:48:29 +00:00
components: {
2020-03-03 11:57:53 +00:00
"pw-section": () => import("../components/layout/section"),
"gql-field": () => import("../components/graphql/field"),
2019-11-18 20:17:33 +00:00
"gql-type": () => import("../components/graphql/type"),
2020-03-03 11:57:53 +00:00
autocomplete: () => import("../components/ui/autocomplete"),
Editor: AceEditor,
2020-02-25 16:10:40 +00:00
QueryEditor: QueryEditor,
2020-03-07 13:10:37 +00:00
tabs: () => import("../components/ui/tabs"),
tab: () => import("../components/ui/tab"),
2019-11-18 00:48:29 +00:00
},
data() {
return {
2020-02-11 09:44:03 +00:00
commonHeaders,
queryFields: [],
mutationFields: [],
2019-11-18 20:17:33 +00:00
subscriptionFields: [],
gqlTypes: [],
copyButton: '<i class="material-icons">file_copy</i>',
downloadButton: '<i class="material-icons">get_app</i>',
doneButton: '<i class="material-icons">done</i>',
expandResponse: false,
2020-02-24 18:44:50 +00:00
responseBodyMaxLines: 16,
settings: {
SCROLL_INTO_ENABLED:
2020-02-25 16:10:40 +00:00
typeof this.$store.state.postwoman.settings.SCROLL_INTO_ENABLED !== "undefined"
? this.$store.state.postwoman.settings.SCROLL_INTO_ENABLED
2020-02-25 16:10:40 +00:00
: true,
},
}
2019-11-18 00:48:29 +00:00
},
2019-11-21 05:02:30 +00:00
computed: {
url: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.url
2019-11-21 05:02:30 +00:00
},
set(value) {
2020-02-25 16:10:40 +00:00
this.$store.commit("setGQLState", { value, attribute: "url" })
},
2019-11-26 03:48:36 +00:00
},
headers: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.headers
2019-11-26 03:48:36 +00:00
},
set(value) {
2020-02-25 16:10:40 +00:00
this.$store.commit("setGQLState", { value, attribute: "headers" })
},
2019-11-26 03:48:36 +00:00
},
2019-11-26 20:44:15 +00:00
gqlQueryString: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.query
2019-11-26 20:44:15 +00:00
},
set(value) {
2020-02-25 16:10:40 +00:00
this.$store.commit("setGQLState", { value, attribute: "query" })
},
2019-11-26 20:44:15 +00:00
},
response: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.response
},
set(value) {
2020-02-25 16:10:40 +00:00
this.$store.commit("setGQLState", { value, attribute: "response" })
},
},
schema: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.schema
},
set(value) {
2020-02-25 16:10:40 +00:00
this.$store.commit("setGQLState", { value, attribute: "schema" })
},
2019-11-26 20:44:15 +00:00
},
variableString: {
get() {
2020-02-25 16:10:40 +00:00
return this.$store.state.gql.variablesJSONString
},
set(value) {
2020-01-30 18:48:20 +00:00
this.$store.commit("setGQLState", {
value,
2020-02-25 16:10:40 +00:00
attribute: "variablesJSONString",
})
},
},
2019-11-26 03:48:36 +00:00
headerString() {
const result = this.headers
.filter(({ key }) => !!key)
.map(({ key, value }) => `${key}: ${value}`)
2020-02-25 16:10:40 +00:00
.join(",\n")
return result === "" ? "" : `${result}`
},
2019-11-21 05:02:30 +00:00
},
mounted() {
if (this.$store.state.gql.schemaIntrospection && this.$store.state.gql.schema) {
const gqlSchema = gql.buildClientSchema(JSON.parse(this.$store.state.gql.schemaIntrospection))
this.getDocsFromSchema(gqlSchema)
}
},
2019-11-18 00:48:29 +00:00
methods: {
getSpecialKey: getPlatformSpecialKey,
doPrettifyQuery() {
this.$refs.queryEditor.prettifyQuery()
},
handleJumpToType(type) {
2020-03-08 03:41:25 +00:00
this.$refs.gqlTabs.selectTab(this.$refs.typesTab)
2020-02-25 16:10:40 +00:00
const rootTypeName = this.resolveRootType(type).name
2020-02-25 16:10:40 +00:00
const target = document.getElementById(`type_${rootTypeName}`)
if (target && this.settings.SCROLL_INTO_ENABLED) {
target.scrollIntoView({
2020-02-25 16:10:40 +00:00
behavior: "smooth",
})
}
},
resolveRootType(type) {
2020-02-25 16:10:40 +00:00
let t = type
while (t.ofType != null) t = t.ofType
return t
},
copySchema() {
2020-02-25 16:10:40 +00:00
this.$refs.copySchemaCode.innerHTML = this.doneButton
const aux = document.createElement("textarea")
aux.innerText = this.schema
document.body.appendChild(aux)
aux.select()
document.execCommand("copy")
document.body.removeChild(aux)
this.$toast.success(this.$t("copied_to_clipboard"), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
setTimeout(() => (this.$refs.copySchemaCode.innerHTML = this.copyButton), 1000)
},
2019-11-26 21:48:13 +00:00
copyQuery() {
2020-02-25 16:10:40 +00:00
this.$refs.copyQueryButton.innerHTML = this.doneButton
const aux = document.createElement("textarea")
aux.innerText = this.gqlQueryString
document.body.appendChild(aux)
aux.select()
document.execCommand("copy")
document.body.removeChild(aux)
this.$toast.success(this.$t("copied_to_clipboard"), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
setTimeout(() => (this.$refs.copyQueryButton.innerHTML = this.copyButton), 1000)
2019-11-26 21:48:13 +00:00
},
2019-11-26 21:55:43 +00:00
copyResponse() {
2020-02-25 16:10:40 +00:00
this.$refs.copyResponseButton.innerHTML = this.doneButton
const aux = document.createElement("textarea")
aux.innerText = this.response
document.body.appendChild(aux)
aux.select()
document.execCommand("copy")
document.body.removeChild(aux)
this.$toast.success(this.$t("copied_to_clipboard"), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
setTimeout(() => (this.$refs.copyResponseButton.innerHTML = this.copyButton), 1000)
2019-11-26 21:55:43 +00:00
},
2019-11-26 20:37:41 +00:00
async runQuery() {
2020-02-25 16:10:40 +00:00
const startTime = Date.now()
2019-11-26 20:37:41 +00:00
2020-02-25 06:43:55 +00:00
// Start showing the loading bar as soon as possible.
// The nuxt axios module will hide it when the request is made.
2020-02-25 16:10:40 +00:00
this.$nuxt.$loading.start()
2019-11-26 20:37:41 +00:00
2020-02-25 16:10:40 +00:00
this.response = this.$t("loading")
if (this.settings.SCROLL_INTO_ENABLED) this.scrollInto("response")
2019-11-26 20:37:41 +00:00
try {
2020-02-25 16:10:40 +00:00
let headers = {}
this.headers.forEach((header) => {
2020-02-25 16:10:40 +00:00
headers[header.key] = header.value
})
2020-01-30 18:48:20 +00:00
let variables = JSON.parse(this.variableString || "{}")
2019-11-26 20:37:41 +00:00
2020-02-25 16:10:40 +00:00
const gqlQueryString = this.gqlQueryString
2019-11-26 20:37:41 +00:00
const reqOptions = {
method: "post",
url: this.url,
headers: {
...headers,
2020-02-25 16:10:40 +00:00
"content-type": "application/json",
2019-11-26 20:37:41 +00:00
},
2020-02-25 16:10:40 +00:00
data: JSON.stringify({ query: gqlQueryString, variables }),
}
2019-11-26 20:37:41 +00:00
2020-02-25 16:10:40 +00:00
const data = await sendNetworkRequest(reqOptions, this.$store)
this.response = JSON.stringify(data.data, null, 2)
2019-11-29 05:55:13 +00:00
2020-02-25 16:10:40 +00:00
this.$nuxt.$loading.finish()
const duration = Date.now() - startTime
this.$toast.info(this.$t("finished_in", { duration }), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
2019-11-26 20:37:41 +00:00
} catch (error) {
this.response = `${error}. ${this.$t("check_console_details")}`
2020-02-25 16:10:40 +00:00
this.$nuxt.$loading.finish()
2019-11-26 20:37:41 +00:00
this.$toast.error(`${error} ${this.$t("f12_details")}`, {
2020-02-25 16:10:40 +00:00
icon: "error",
})
console.log("Error", error)
2019-11-26 20:37:41 +00:00
}
},
// NOTE : schema required here is the GQL Schema document object, not the schema string
getDocsFromSchema(schema) {
if (schema.getQueryType()) {
const fields = schema.getQueryType().getFields()
const qFields = []
for (const field in fields) {
qFields.push(fields[field])
}
this.queryFields = qFields
}
if (schema.getMutationType()) {
const fields = schema.getMutationType().getFields()
const mFields = []
for (const field in fields) {
mFields.push(fields[field])
}
this.mutationFields = mFields
}
if (schema.getSubscriptionType()) {
const fields = schema.getSubscriptionType().getFields()
const sFields = []
for (const field in fields) {
sFields.push(fields[field])
}
this.subscriptionFields = sFields
}
const typeMap = schema.getTypeMap()
const types = []
const queryTypeName = schema.getQueryType() ? schema.getQueryType().name : ""
const mutationTypeName = schema.getMutationType() ? schema.getMutationType().name : ""
const subscriptionTypeName = schema.getSubscriptionType()
? schema.getSubscriptionType().name
: ""
for (const type in typeMap) {
if (
!typeMap[type].name.startsWith("__") &&
![queryTypeName, mutationTypeName, subscriptionTypeName].includes(typeMap[type].name) &&
typeMap[type] instanceof gql.GraphQLObjectType
) {
types.push(typeMap[type])
}
}
this.gqlTypes = types
},
2019-11-20 12:41:07 +00:00
async getSchema() {
2020-02-25 16:10:40 +00:00
const startTime = Date.now()
// Start showing the loading bar as soon as possible.
// The nuxt axios module will hide it when the request is made.
2020-02-25 16:10:40 +00:00
this.$nuxt.$loading.start()
2020-02-25 06:43:55 +00:00
2020-02-25 16:10:40 +00:00
this.schema = this.$t("loading")
if (this.settings.SCROLL_INTO_ENABLED) this.scrollInto("schema")
2019-11-20 12:30:17 +00:00
try {
const query = JSON.stringify({
2020-02-25 16:10:40 +00:00
query: gql.getIntrospectionQuery(),
})
2019-11-20 12:35:46 +00:00
2020-02-25 16:10:40 +00:00
let headers = {}
this.headers.forEach((header) => {
2020-02-25 16:10:40 +00:00
headers[header.key] = header.value
})
2019-11-26 04:42:41 +00:00
const reqOptions = {
method: "post",
url: this.url,
headers: {
2019-11-26 04:42:41 +00:00
...headers,
2020-02-25 16:10:40 +00:00
"content-type": "application/json",
},
2020-02-25 16:10:40 +00:00
data: query,
}
2019-11-26 04:42:41 +00:00
const data = await sendNetworkRequest(reqOptions, this.$store)
2020-02-25 16:10:40 +00:00
const schema = gql.buildClientSchema(data.data.data)
this.$store.commit("setGQLState", {
value: JSON.stringify(data.data.data),
attribute: "schemaIntrospection",
})
this.schema = gql.printSchema(schema, {
2020-02-25 16:10:40 +00:00
commentDescriptions: true,
})
this.getDocsFromSchema(schema)
2020-02-25 16:10:40 +00:00
this.$refs.queryEditor.setValidationSchema(schema)
this.$nuxt.$loading.finish()
const duration = Date.now() - startTime
this.$toast.info(this.$t("finished_in", { duration }), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
2019-11-21 03:41:50 +00:00
} catch (error) {
2020-02-25 16:10:40 +00:00
this.$nuxt.$loading.finish()
2020-02-26 03:41:08 +00:00
2020-02-25 16:10:40 +00:00
this.schema = `${error}. ${this.$t("check_console_details")}`
this.$toast.error(
`${this.$t("graphql_introspect_failed")} ${this.$t("check_graphql_valid")}`,
{
icon: "error",
}
)
2020-02-25 16:10:40 +00:00
console.log("Error", error)
2019-11-21 03:41:50 +00:00
}
},
ToggleExpandResponse() {
2020-02-25 16:10:40 +00:00
this.expandResponse = !this.expandResponse
this.responseBodyMaxLines = this.responseBodyMaxLines == Infinity ? 16 : Infinity
},
downloadResponse() {
2020-02-25 16:10:40 +00:00
const dataToWrite = JSON.stringify(this.schema, null, 2)
const file = new Blob([dataToWrite], { type: "application/json" })
const a = document.createElement("a")
const url = URL.createObjectURL(file)
a.href = url
a.download = `${this.url} on ${Date()}.graphql`.replace(/\./g, "[dot]")
document.body.appendChild(a)
a.click()
this.$refs.downloadResponse.innerHTML = this.doneButton
this.$toast.success(this.$t("download_started"), {
2020-02-25 16:10:40 +00:00
icon: "done",
})
setTimeout(() => {
2020-02-25 16:10:40 +00:00
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
this.$refs.downloadResponse.innerHTML = this.downloadButton
}, 1000)
2019-11-26 03:48:36 +00:00
},
addRequestHeader(index) {
this.$store.commit("addGQLHeader", {
key: "",
2020-02-25 16:10:40 +00:00
value: "",
})
return false
2019-11-26 03:48:36 +00:00
},
removeRequestHeader(index) {
// .slice() is used so we get a separate array, rather than just a reference
2020-02-25 16:10:40 +00:00
const oldHeaders = this.headers.slice()
2020-02-25 16:10:40 +00:00
this.$store.commit("removeGQLHeader", index)
this.$toast.error(this.$t("deleted"), {
icon: "delete",
action: {
text: this.$t("undo"),
duration: 4000,
onClick: (e, toastObject) => {
2020-02-25 16:10:40 +00:00
this.headers = oldHeaders
toastObject.remove()
},
},
})
},
scrollInto(view) {
this.$refs[view].$el.scrollIntoView({
2020-02-25 16:10:40 +00:00
behavior: "smooth",
})
},
},
}
2019-11-18 00:48:29 +00:00
</script>