diff --git a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue index d611cb30e..db8123d94 100644 --- a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue @@ -25,13 +25,14 @@ import { useReadonlyStream } from "~/composables/stream" import { platform } from "~/platform" import { + appendGraphqlCollections, graphqlCollections$, - setGraphqlCollections, } from "~/newstore/collections" import { hoppGqlCollectionsImporter } from "~/helpers/import-export/import/hoppGql" import { gqlCollectionsExporter } from "~/helpers/import-export/export/gqlCollections" import { gqlCollectionsGistExporter } from "~/helpers/import-export/export/gqlCollectionsGistExporter" import { computed } from "vue" +import { hoppGQLImporter } from "~/helpers/import-export/import/hopp" const t = useI18n() const toast = useToast() @@ -60,15 +61,20 @@ const GqlCollectionsHoppImporter: ImporterOrExporter = { showImportFailedError() return } + const validatedCollection = await hoppGQLImporter( + JSON.stringify(res.right) + )() - handleImportToStore(res.right) + if (E.isRight(validatedCollection)) { + handleImportToStore(validatedCollection.right) - platform.analytics?.logEvent({ - type: "HOPP_IMPORT_COLLECTION", - platform: "gql", - workspaceType: "personal", - importer: "json", - }) + platform.analytics?.logEvent({ + type: "HOPP_IMPORT_COLLECTION", + platform: "gql", + workspaceType: "personal", + importer: "json", + }) + } emit("hide-modal") }, @@ -215,8 +221,8 @@ const showImportFailedError = () => { } const handleImportToStore = async (gqlCollections: HoppCollection[]) => { - setGraphqlCollections(gqlCollections) - toast.success(t("import.success")) + appendGraphqlCollections(gqlCollections) + toast.success(t("state.file_imported")) } const emit = defineEmits<{ diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts index 887e22e72..bdcb37518 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts @@ -7,6 +7,7 @@ import { isPlainObject as _isPlainObject } from "lodash-es" import { IMPORTER_INVALID_FILE_FORMAT } from "." import { safeParseJSON } from "~/helpers/functional/json" +import { translateToNewGQLCollection } from "@hoppscotch/data" export const hoppRESTImporter = (content: string) => pipe( @@ -50,3 +51,29 @@ const validateCollection = (collection: unknown) => { */ const makeCollectionsArray = (collections: unknown | unknown[]): unknown[] => Array.isArray(collections) ? collections : [collections] + +export const hoppGQLImporter = (content: string) => + pipe( + safeParseJSON(content), + O.chain( + flow( + makeCollectionsArray, + RA.map(validateGQLCollection), + O.sequenceArray, + O.map(RA.toArray) + ) + ), + TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT) + ) + +/** + * + * @param collection the collection to validate + * @returns the collection if it is valid, else a translated version of the collection + */ +export const validateGQLCollection = (collection: unknown) => { + if (isValidCollection(collection)) { + return O.some(collection) + } + return O.some(translateToNewGQLCollection(collection)) +} diff --git a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLChildUserCollection.graphql b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLChildUserCollection.graphql index 54dc80c48..736781a47 100644 --- a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLChildUserCollection.graphql +++ b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLChildUserCollection.graphql @@ -1,11 +1,14 @@ mutation CreateGQLChildUserCollection( $title: String! $parentUserCollectionID: ID! + $data: String ) { createGQLChildUserCollection( title: $title parentUserCollectionID: $parentUserCollectionID + data: $data ) { id + data } } diff --git a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLRootUserCollection.graphql b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLRootUserCollection.graphql index eecdebe17..bc30e96d2 100644 --- a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLRootUserCollection.graphql +++ b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateGQLRootUserCollection.graphql @@ -1,5 +1,6 @@ -mutation CreateGQLRootUserCollection($title: String!) { - createGQLRootUserCollection(title: $title) { +mutation CreateGQLRootUserCollection($title: String!, $data: String) { + createGQLRootUserCollection(title: $title, data: $data) { id + data } } diff --git a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTChildUserCollection.graphql b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTChildUserCollection.graphql index 40b38aca7..eea3cdf75 100644 --- a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTChildUserCollection.graphql +++ b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTChildUserCollection.graphql @@ -1,11 +1,14 @@ mutation CreateRESTChildUserCollection( $title: String! $parentUserCollectionID: ID! + $data: String ) { createRESTChildUserCollection( title: $title parentUserCollectionID: $parentUserCollectionID + data: $data ) { id + data } } diff --git a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTRootUserCollection.graphql b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTRootUserCollection.graphql index 8c54f0c2b..d0136c499 100644 --- a/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTRootUserCollection.graphql +++ b/packages/hoppscotch-selfhost-web/src/api/mutations/CreateRESTRootUserCollection.graphql @@ -1,5 +1,6 @@ -mutation CreateRESTRootUserCollection($title: String!) { - createRESTRootUserCollection(title: $title) { +mutation CreateRESTRootUserCollection($title: String!, $data: String) { + createRESTRootUserCollection(title: $title, data: $data) { id + data } } diff --git a/packages/hoppscotch-selfhost-web/src/api/queries/GetRootGQLUserCollections.graphql b/packages/hoppscotch-selfhost-web/src/api/queries/GetRootGQLUserCollections.graphql index fcc80d993..2eaa1cf7f 100644 --- a/packages/hoppscotch-selfhost-web/src/api/queries/GetRootGQLUserCollections.graphql +++ b/packages/hoppscotch-selfhost-web/src/api/queries/GetRootGQLUserCollections.graphql @@ -4,10 +4,12 @@ query GetGQLRootUserCollections { id title type + data childrenGQL { id title type + data } } } diff --git a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.api.ts b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.api.ts index 3faecbda7..10a7cb249 100644 --- a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.api.ts +++ b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.api.ts @@ -70,22 +70,24 @@ import { UpdateUserCollectionDocument, } from "../../api/generated/graphql" -export const createRESTRootUserCollection = (title: string) => +export const createRESTRootUserCollection = (title: string, data?: string) => runMutation< CreateRestRootUserCollectionMutation, CreateRestRootUserCollectionMutationVariables, "" >(CreateRestRootUserCollectionDocument, { title, + data, })() -export const createGQLRootUserCollection = (title: string) => +export const createGQLRootUserCollection = (title: string, data?: string) => runMutation< CreateGqlRootUserCollectionMutation, CreateGqlRootUserCollectionMutationVariables, "" >(CreateGqlRootUserCollectionDocument, { title, + data, })() export const createRESTUserRequest = ( @@ -120,7 +122,8 @@ export const createGQLUserRequest = ( export const createRESTChildUserCollection = ( title: string, - parentUserCollectionID: string + parentUserCollectionID: string, + data?: string ) => runMutation< CreateRestChildUserCollectionMutation, @@ -129,11 +132,13 @@ export const createRESTChildUserCollection = ( >(CreateRestChildUserCollectionDocument, { title, parentUserCollectionID, + data, })() export const createGQLChildUserCollection = ( title: string, - parentUserCollectionID: string + parentUserCollectionID: string, + data?: string ) => runMutation< CreateGqlChildUserCollectionMutation, @@ -142,6 +147,7 @@ export const createGQLChildUserCollection = ( >(CreateGqlChildUserCollectionDocument, { title, parentUserCollectionID, + data, })() export const deleteUserCollection = (userCollectionID: string) => diff --git a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts index dc9e23df7..bbdec917b 100644 --- a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts +++ b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts @@ -47,27 +47,69 @@ const recursivelySyncCollections = async ( // if parentUserCollectionID does not exist, create the collection as a root collection if (!parentUserCollectionID) { - const res = await createRESTRootUserCollection(collection.name) - + const data = { + auth: collection.auth ?? { + authType: "inherit", + authActive: true, + }, + headers: collection.headers ?? [], + } + const res = await createRESTRootUserCollection( + collection.name, + JSON.stringify(data) + ) if (E.isRight(res)) { parentCollectionID = res.right.createRESTRootUserCollection.id + const returnedData = res.right.createRESTRootUserCollection.data + ? JSON.parse(res.right.createRESTRootUserCollection.data) + : { + auth: { + authType: "inherit", + authActive: true, + }, + headers: [], + } + collection.id = parentCollectionID + collection.auth = returnedData.auth + collection.headers = returnedData.headers removeDuplicateRESTCollectionOrFolder(parentCollectionID, collectionPath) } else { parentCollectionID = undefined } } else { // if parentUserCollectionID exists, create the collection as a child collection + const data = { + auth: collection.auth ?? { + authType: "inherit", + authActive: true, + }, + headers: collection.headers ?? [], + } + const res = await createRESTChildUserCollection( collection.name, - parentUserCollectionID + parentUserCollectionID, + JSON.stringify(data) ) if (E.isRight(res)) { const childCollectionId = res.right.createRESTChildUserCollection.id + const returnedData = res.right.createRESTChildUserCollection.data + ? JSON.parse(res.right.createRESTChildUserCollection.data) + : { + auth: { + authType: "inherit", + authActive: true, + }, + headers: [], + } + collection.id = childCollectionId + collection.auth = returnedData.auth + collection.headers = returnedData.headers removeDuplicateRESTCollectionOrFolder( childCollectionId, diff --git a/packages/hoppscotch-selfhost-web/src/platform/collections/gqlCollections.sync.ts b/packages/hoppscotch-selfhost-web/src/platform/collections/gqlCollections.sync.ts index ac168d0c7..3c6e5afab 100644 --- a/packages/hoppscotch-selfhost-web/src/platform/collections/gqlCollections.sync.ts +++ b/packages/hoppscotch-selfhost-web/src/platform/collections/gqlCollections.sync.ts @@ -44,12 +44,35 @@ const recursivelySyncCollections = async ( // if parentUserCollectionID does not exist, create the collection as a root collection if (!parentUserCollectionID) { - const res = await createGQLRootUserCollection(collection.name) + const data = { + auth: collection.auth ?? { + authType: "inherit", + authActive: true, + }, + headers: collection.headers ?? [], + } + const res = await createGQLRootUserCollection( + collection.name, + JSON.stringify(data) + ) if (E.isRight(res)) { parentCollectionID = res.right.createGQLRootUserCollection.id + const returnedData = res.right.createGQLRootUserCollection.data + ? JSON.parse(res.right.createGQLRootUserCollection.data) + : { + auth: { + authType: "inherit", + authActive: true, + }, + headers: [], + } + collection.id = parentCollectionID + collection.auth = returnedData.auth + collection.headers = returnedData.headers + removeDuplicateGraphqlCollectionOrFolder( parentCollectionID, collectionPath @@ -59,15 +82,37 @@ const recursivelySyncCollections = async ( } } else { // if parentUserCollectionID exists, create the collection as a child collection + + const data = { + auth: collection.auth ?? { + authType: "inherit", + authActive: true, + }, + headers: collection.headers ?? [], + } + const res = await createGQLChildUserCollection( collection.name, - parentUserCollectionID + parentUserCollectionID, + JSON.stringify(data) ) if (E.isRight(res)) { const childCollectionId = res.right.createGQLChildUserCollection.id + const returnedData = res.right.createGQLChildUserCollection.data + ? JSON.parse(res.right.createGQLChildUserCollection.data) + : { + auth: { + authType: "inherit", + authActive: true, + }, + headers: [], + } + collection.id = childCollectionId + collection.auth = returnedData.auth + collection.headers = returnedData.headers removeDuplicateGraphqlCollectionOrFolder( childCollectionId,