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 { platform } from "~/platform"
import { import {
appendGraphqlCollections,
graphqlCollections$, graphqlCollections$,
setGraphqlCollections,
} from "~/newstore/collections" } from "~/newstore/collections"
import { hoppGqlCollectionsImporter } from "~/helpers/import-export/import/hoppGql" import { hoppGqlCollectionsImporter } from "~/helpers/import-export/import/hoppGql"
import { gqlCollectionsExporter } from "~/helpers/import-export/export/gqlCollections" import { gqlCollectionsExporter } from "~/helpers/import-export/export/gqlCollections"
import { gqlCollectionsGistExporter } from "~/helpers/import-export/export/gqlCollectionsGistExporter" import { gqlCollectionsGistExporter } from "~/helpers/import-export/export/gqlCollectionsGistExporter"
import { computed } from "vue" import { computed } from "vue"
import { hoppGQLImporter } from "~/helpers/import-export/import/hopp"
const t = useI18n() const t = useI18n()
const toast = useToast() const toast = useToast()
@@ -60,15 +61,20 @@ const GqlCollectionsHoppImporter: ImporterOrExporter = {
showImportFailedError() showImportFailedError()
return return
} }
const validatedCollection = await hoppGQLImporter(
JSON.stringify(res.right)
)()
handleImportToStore(res.right) if (E.isRight(validatedCollection)) {
handleImportToStore(validatedCollection.right)
platform.analytics?.logEvent({ platform.analytics?.logEvent({
type: "HOPP_IMPORT_COLLECTION", type: "HOPP_IMPORT_COLLECTION",
platform: "gql", platform: "gql",
workspaceType: "personal", workspaceType: "personal",
importer: "json", importer: "json",
}) })
}
emit("hide-modal") emit("hide-modal")
}, },
@@ -215,8 +221,8 @@ const showImportFailedError = () => {
} }
const handleImportToStore = async (gqlCollections: HoppCollection[]) => { const handleImportToStore = async (gqlCollections: HoppCollection[]) => {
setGraphqlCollections(gqlCollections) appendGraphqlCollections(gqlCollections)
toast.success(t("import.success")) toast.success(t("state.file_imported"))
} }
const emit = defineEmits<{ const emit = defineEmits<{

View File

@@ -7,6 +7,7 @@ import { isPlainObject as _isPlainObject } from "lodash-es"
import { IMPORTER_INVALID_FILE_FORMAT } from "." import { IMPORTER_INVALID_FILE_FORMAT } from "."
import { safeParseJSON } from "~/helpers/functional/json" import { safeParseJSON } from "~/helpers/functional/json"
import { translateToNewGQLCollection } from "@hoppscotch/data"
export const hoppRESTImporter = (content: string) => export const hoppRESTImporter = (content: string) =>
pipe( pipe(
@@ -50,3 +51,29 @@ const validateCollection = (collection: unknown) => {
*/ */
const makeCollectionsArray = (collections: unknown | unknown[]): unknown[] => const makeCollectionsArray = (collections: unknown | unknown[]): unknown[] =>
Array.isArray(collections) ? collections : [collections] 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( mutation CreateGQLChildUserCollection(
$title: String! $title: String!
$parentUserCollectionID: ID! $parentUserCollectionID: ID!
$data: String
) { ) {
createGQLChildUserCollection( createGQLChildUserCollection(
title: $title title: $title
parentUserCollectionID: $parentUserCollectionID parentUserCollectionID: $parentUserCollectionID
data: $data
) { ) {
id id
data
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@@ -70,22 +70,24 @@ import {
UpdateUserCollectionDocument, UpdateUserCollectionDocument,
} from "../../api/generated/graphql" } from "../../api/generated/graphql"
export const createRESTRootUserCollection = (title: string) => export const createRESTRootUserCollection = (title: string, data?: string) =>
runMutation< runMutation<
CreateRestRootUserCollectionMutation, CreateRestRootUserCollectionMutation,
CreateRestRootUserCollectionMutationVariables, CreateRestRootUserCollectionMutationVariables,
"" ""
>(CreateRestRootUserCollectionDocument, { >(CreateRestRootUserCollectionDocument, {
title, title,
data,
})() })()
export const createGQLRootUserCollection = (title: string) => export const createGQLRootUserCollection = (title: string, data?: string) =>
runMutation< runMutation<
CreateGqlRootUserCollectionMutation, CreateGqlRootUserCollectionMutation,
CreateGqlRootUserCollectionMutationVariables, CreateGqlRootUserCollectionMutationVariables,
"" ""
>(CreateGqlRootUserCollectionDocument, { >(CreateGqlRootUserCollectionDocument, {
title, title,
data,
})() })()
export const createRESTUserRequest = ( export const createRESTUserRequest = (
@@ -120,7 +122,8 @@ export const createGQLUserRequest = (
export const createRESTChildUserCollection = ( export const createRESTChildUserCollection = (
title: string, title: string,
parentUserCollectionID: string parentUserCollectionID: string,
data?: string
) => ) =>
runMutation< runMutation<
CreateRestChildUserCollectionMutation, CreateRestChildUserCollectionMutation,
@@ -129,11 +132,13 @@ export const createRESTChildUserCollection = (
>(CreateRestChildUserCollectionDocument, { >(CreateRestChildUserCollectionDocument, {
title, title,
parentUserCollectionID, parentUserCollectionID,
data,
})() })()
export const createGQLChildUserCollection = ( export const createGQLChildUserCollection = (
title: string, title: string,
parentUserCollectionID: string parentUserCollectionID: string,
data?: string
) => ) =>
runMutation< runMutation<
CreateGqlChildUserCollectionMutation, CreateGqlChildUserCollectionMutation,
@@ -142,6 +147,7 @@ export const createGQLChildUserCollection = (
>(CreateGqlChildUserCollectionDocument, { >(CreateGqlChildUserCollectionDocument, {
title, title,
parentUserCollectionID, parentUserCollectionID,
data,
})() })()
export const deleteUserCollection = (userCollectionID: string) => 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 does not exist, create the collection as a root collection
if (!parentUserCollectionID) { 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)) { if (E.isRight(res)) {
parentCollectionID = res.right.createRESTRootUserCollection.id 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.id = parentCollectionID
collection.auth = returnedData.auth
collection.headers = returnedData.headers
removeDuplicateRESTCollectionOrFolder(parentCollectionID, collectionPath) removeDuplicateRESTCollectionOrFolder(parentCollectionID, collectionPath)
} else { } else {
parentCollectionID = undefined parentCollectionID = undefined
} }
} else { } else {
// if parentUserCollectionID exists, create the collection as a child collection // 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( const res = await createRESTChildUserCollection(
collection.name, collection.name,
parentUserCollectionID parentUserCollectionID,
JSON.stringify(data)
) )
if (E.isRight(res)) { if (E.isRight(res)) {
const childCollectionId = res.right.createRESTChildUserCollection.id 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.id = childCollectionId
collection.auth = returnedData.auth
collection.headers = returnedData.headers
removeDuplicateRESTCollectionOrFolder( removeDuplicateRESTCollectionOrFolder(
childCollectionId, childCollectionId,

View File

@@ -44,12 +44,35 @@ const recursivelySyncCollections = async (
// if parentUserCollectionID does not exist, create the collection as a root collection // if parentUserCollectionID does not exist, create the collection as a root collection
if (!parentUserCollectionID) { 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)) { if (E.isRight(res)) {
parentCollectionID = res.right.createGQLRootUserCollection.id 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.id = parentCollectionID
collection.auth = returnedData.auth
collection.headers = returnedData.headers
removeDuplicateGraphqlCollectionOrFolder( removeDuplicateGraphqlCollectionOrFolder(
parentCollectionID, parentCollectionID,
collectionPath collectionPath
@@ -59,15 +82,37 @@ const recursivelySyncCollections = async (
} }
} else { } else {
// if parentUserCollectionID exists, create the collection as a child collection // 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( const res = await createGQLChildUserCollection(
collection.name, collection.name,
parentUserCollectionID parentUserCollectionID,
JSON.stringify(data)
) )
if (E.isRight(res)) { if (E.isRight(res)) {
const childCollectionId = res.right.createGQLChildUserCollection.id 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.id = childCollectionId
collection.auth = returnedData.auth
collection.headers = returnedData.headers
removeDuplicateGraphqlCollectionOrFolder( removeDuplicateGraphqlCollectionOrFolder(
childCollectionId, childCollectionId,