8000 feat: support await and nested functions · wheatjs/vite-plugin-vue-gql@2bd12ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bd12ee

Browse files
committed
feat: support await and nested functions
1 parent 5382b0c commit 2bd12ee

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

examples/spa/src/App.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import TestComponent from './components/TestComponent.vue'
44

55
<template>
66
<div>
7-
<TestComponent />
7+
<Suspense>
8+
<template #default>
9+
<TestComponent />
10+
</template>
11+
<template #fallback>
12+
<div>
13+
Loading...
14+
</div>
15+
</template>
16+
</Suspense>
817
</div>
918
</template>

examples/spa/src/components/TestComponent.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const name = ref('RADWIMPS')
77
const throttled = useThrottle(name, 2000)
88
99
const { fetching, error, data } = useQuery<any, any>({ name: throttled })
10-
1110
</script>
1211

1312
<template>
@@ -53,3 +52,16 @@ query($name: String!) {
5352
}
5453
}
5554
</gql>
55+
56+
<gql name='test'>
57+
query($name: String!) {
58+
queryArtists(byName: $name) {
59+
name
60+
image
61+
albums {
62+
name
63+
image
64+
}
65+
}
66+
}
67+
</gql>

src/util.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface QueryMetadata {
1010
interface NodeMetadata {
1111
callName: string
1212
callType: string
13+
isAsync?: boolean
1314
start: number
1415
end: number
1516
args: (Expression | SpreadElement | JSXNamespacedName | ArgumentPlaceholder)[]
@@ -38,11 +39,17 @@ export function collectNodes(statements: Statement[], calls: Record<string, stri
3839
const nodes: NodeMetadata[] = []
3940

4041
statements.forEach((x) => {
42+
if (x.type === 'FunctionDeclaration' && x.body.type === 'BlockStatement')
43+
nodes.push(...collectNodes(x.body.body, calls))
44+
4145
if (x.type === 'VariableDeclaration') {
4246
x.declarations.forEach((y) => {
4347
const z = y.init
4448

45-
if (z && z.type === 'CallExpression' && isOneOfCall(z, Object.values(calls))) {
49+
if (z && z.type === 'ArrowFunctionExpression' && z.body.type === 'BlockStatement') {
50+
nodes.push(...collectNodes(z.body.body, calls))
51+
}
52+
else if (z && z.type === 'CallExpression' && isOneOfCall(z, Object.values(calls))) {
4653
const { start, end, arguments: args } = z
4754

4855
nodes.push({
@@ -53,6 +60,21 @@ export functio 8000 n collectNodes(statements: Statement[], calls: Record<string, stri
5360
args,
5461
})
5562
}
63+
else if (z && z.type === 'AwaitExpression' && isOneOfCall(z.argument, Object.values(calls))) {
64+
if (z.argument.type === 'CallExpression') {
65+
const { start, end, argument: { arguments: args } } = z
66+
const cName = callName(z.argument)
67+
68+
nodes.push({
69+
callName: cName,
70+
isAsync: true,
71+
callType: Object.entries(calls).find(([_, value]) => value === cName)![0],
72+
start: start!,
73+
end: end!,
74+
args,
75+
})
76+
}
77+
}
5678
})
5779
}
5880
else if (x.type === 'ExpressionStatement' && x.expression.type === 'CallExpression' && isOneOfCall(x.expression, Object.values(calls))) {
@@ -66,6 +88,21 @@ export function collectNodes(statements: Statement[], calls: Record<string, stri
6688
args,
6789
})
6890
}
91+
else if (x.type === 'ExpressionStatement' && x.expression.type === 'AwaitExpression' && isOneOfCall(x.expression.argument, Object.values(calls))) {
92+
if (x.expression.argument.type === 'CallExpression') {
93+
const { start, end, expression: { argument: { arguments: args } } } = x
94+
const cName = callName(x.expression.argument)
95+
96+
nodes.push({
97+
callName: cName,
98+
isAsync: true,
99+
callType: Object.entries(calls).find(([_, value]) => value === cName)![0],
100+
start: start!,
101+
end: end!,
102+
args,
103+
})
104+
}
105+
}
69106
})
70107

71108
return nodes
@@ -208,7 +245,7 @@ export function convertQueryToFunctionCall(node: NodeReplacement): string {
208245
}
209246
}
210247

211-
return `${name}(${arg})`.trim()
248+
return `${node.isAsync ? 'await ' : ''}${name}(${arg})`.trim()
212249
}
213250

214251
/**

0 commit comments

Comments
 (0)
0