From 05ad84f372d928d000e347d8ef65f17fd74fc8bd Mon Sep 17 00:00:00 2001 From: nivedin Date: Wed, 24 Apr 2024 20:46:27 +0530 Subject: [PATCH] chore: add fallback url and add reqIndex for graphql saving --- .../components/collections/graphql/index.vue | 19 +++++++------------ .../src/components/collections/index.vue | 4 ++++ .../src/components/graphql/RequestOptions.vue | 4 +++- .../src/newstore/collections.ts | 10 ++++++++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/hoppscotch-common/src/components/collections/graphql/index.vue b/packages/hoppscotch-common/src/components/collections/graphql/index.vue index 67fe145fc..42f60b18d 100644 --- a/packages/hoppscotch-common/src/components/collections/graphql/index.vue +++ b/packages/hoppscotch-common/src/components/collections/graphql/index.vue @@ -193,6 +193,7 @@ import { PersistedOAuthConfig } from "~/services/oauth/oauth.service" import { GQLOptionTabs } from "~/components/graphql/RequestOptions.vue" import { EditingProperties } from "../Properties.vue" import { defineActionHandler } from "~/helpers/actions" +import { getDefaultGQLRequest } from "~/helpers/graphql/default" const t = useI18n() const toast = useToast() @@ -380,32 +381,26 @@ const editCollection = ( displayModalEdit(true) } -const onAddRequest = ({ - name, - path, - index, -}: { - name: string - path: string - index: number -}) => { +const onAddRequest = ({ name, path }: { name: string; path: string }) => { const newRequest = { ...tabs.currentActiveTab.value.document.request, name, + url: + tabs.currentActiveTab.value.document.request.url || + getDefaultGQLRequest().url, } - saveGraphqlRequestAs(path, newRequest) + const insertionIndex = saveGraphqlRequestAs(path, newRequest) const { auth, headers } = cascadeParentCollectionForHeaderAuth( path, "graphql" ) - tabs.createNewTab({ saveContext: { originLocation: "user-collection", folderPath: path, - requestIndex: index, + requestIndex: insertionIndex, }, request: newRequest, isDirty: false, diff --git a/packages/hoppscotch-common/src/components/collections/index.vue b/packages/hoppscotch-common/src/components/collections/index.vue index 9e1fc4d66..f168878d1 100644 --- a/packages/hoppscotch-common/src/components/collections/index.vue +++ b/packages/hoppscotch-common/src/components/collections/index.vue @@ -254,6 +254,7 @@ import { PersistenceService } from "~/services/persistence" import { PersistedOAuthConfig } from "~/services/oauth/oauth.service" import { RESTOptionTabs } from "../http/RequestOptions.vue" import { EditingProperties } from "./Properties.vue" +import { getDefaultRESTRequest } from "~/helpers/rest/default" const t = useI18n() const toast = useToast() @@ -790,6 +791,9 @@ const onAddRequest = (requestName: string) => { const newRequest = { ...cloneDeep(tabs.currentActiveTab.value.document.request), name: requestName, + endpoint: + tabs.currentActiveTab.value.document.request.endpoint || + getDefaultRESTRequest().endpoint, } const path = editingFolderPath.value diff --git a/packages/hoppscotch-common/src/components/graphql/RequestOptions.vue b/packages/hoppscotch-common/src/components/graphql/RequestOptions.vue index cb7c88315..70b0a6374 100644 --- a/packages/hoppscotch-common/src/components/graphql/RequestOptions.vue +++ b/packages/hoppscotch-common/src/components/graphql/RequestOptions.vue @@ -120,7 +120,9 @@ const request = useVModel(props, "modelValue", emit) const url = computedWithControl( () => tabs.currentActiveTab.value, - () => tabs.currentActiveTab.value.document.request.url + () => + tabs.currentActiveTab.value.document.request.url || + getDefaultGQLRequest().url ) const activeGQLHeadersCount = computed( diff --git a/packages/hoppscotch-common/src/newstore/collections.ts b/packages/hoppscotch-common/src/newstore/collections.ts index 3c021809a..19c87b7d0 100644 --- a/packages/hoppscotch-common/src/newstore/collections.ts +++ b/packages/hoppscotch-common/src/newstore/collections.ts @@ -1393,6 +1393,14 @@ export function editGraphqlRequest( } export function saveGraphqlRequestAs(path: string, request: HoppGQLRequest) { + // For calculating the insertion request index + const targetLocation = navigateToFolderWithIndexPath( + graphqlCollectionStore.value.state, + path.split("/").map((x) => parseInt(x)) + ) + + const insertionIndex = targetLocation!.requests.length + graphqlCollectionStore.dispatch({ dispatcher: "saveRequestAs", payload: { @@ -1400,6 +1408,8 @@ export function saveGraphqlRequestAs(path: string, request: HoppGQLRequest) { request, }, }) + + return insertionIndex } export function removeGraphqlRequest(