add union support for operation parser

This commit is contained in:
DrakeTDL 2023-10-12 11:23:06 -07:00
parent 5d0d3c33fd
commit 5d1c1bb92d
No known key found for this signature in database
2 changed files with 9 additions and 6 deletions

View File

@ -90,7 +90,7 @@ const replaceLast = (str: string, pattern: string | RegExp, replacement: string)
}
const updateOperation: UpdateOperation = (
{ query, variables, fields, level, hasSubField },
{ query, variables, fields, level, hasSubField, isUnion },
) => {
if (!query) query = `${fields[level]} {\n\t%${fields[level]}\n}`
const convertedType: [string[], string[]] = [[], []]
@ -103,20 +103,22 @@ const updateOperation: UpdateOperation = (
query = replaceLast(
query,
`%${fields[level - 1]}`,
`${fields[level]}${variables ? ` (${convertedType[0].join(", ")})` : ""}${
hasSubField ? `${` {\n${t(level + 1)}%${fields.at(-1)}\n${t(level)}}`}` : ""
}\n${t(level)}%${fields[level - 1]}`,
`${isUnion ? "... on " : ""}${fields[level]}${
variables ? ` (${convertedType[0].join(", ")})` : ""
}${hasSubField ? `${` {\n${t(level + 1)}%${fields.at(-1)}\n${t(level)}}`}` : ""}\n${t(level)}%${
fields[level - 1]
}`,
)
return {
get() {
// return query
return query!.replace(/(\n|,)?(\t+| )%\w+\b/g, "")
},
set({ subField, variables, hasSubField, level }) {
set({ subField, variables, hasSubField, level, isUnion }) {
fields.splice(level, 0, subField)
while (fields.length - 1 > level) fields.pop()
return updateOperation({ query, variables, level, fields, hasSubField })
return updateOperation({ query, variables, level, fields, hasSubField, isUnion })
},
}
}

View File

@ -18,6 +18,7 @@ export type UpdateOperation = {
fields: string[]
level: number
hasSubField?: true
isUnion?: true
}): {
get(): string
set(