refactor: update rest-gql import-export sync

This commit is contained in:
nivedin
2023-12-13 16:11:22 +05:30
committed by Andrew Bastin
parent 8bb628354e
commit e4eaa2cec9
10 changed files with 159 additions and 23 deletions

View File

@@ -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<{

View File

@@ -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))
}

View File

@@ -1,11 +1,14 @@
mutation CreateGQLChildUserCollection(
$title: String!
$parentUserCollectionID: ID!
$data: String
) {
createGQLChildUserCollection(
title: $title
parentUserCollectionID: $parentUserCollectionID
data: $data
) {
id
data
}
}

View File

@@ -1,5 +1,6 @@
mutation CreateGQLRootUserCollection($title: String!) {
createGQLRootUserCollection(title: $title) {
mutation CreateGQLRootUserCollection($title: String!, $data: String) {
createGQLRootUserCollection(title: $title, data: $data) {
id
data
}
}

View File

@@ -1,11 +1,14 @@
mutation CreateRESTChildUserCollection(
$title: String!
$parentUserCollectionID: ID!
$data: String
) {
createRESTChildUserCollection(
title: $title
parentUserCollectionID: $parentUserCollectionID
data: $data
) {
id
data
}
}

View File

@@ -1,5 +1,6 @@
mutation CreateRESTRootUserCollection($title: String!) {
createRESTRootUserCollection(title: $title) {
mutation CreateRESTRootUserCollection($title: String!, $data: String) {
createRESTRootUserCollection(title: $title, data: $data) {
id
data
}
}

View File

@@ -4,10 +4,12 @@ query GetGQLRootUserCollections {
id
title
type
data
childrenGQL {
id
title
type
data
}
}
}

View File

@@ -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) =>

View File

@@ -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,

View File

@@ -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,