Skip to content

Commit 708e6dc

Browse files
committed
fix: get TS function call type params working
1 parent b912bc1 commit 708e6dc

2 files changed

Lines changed: 96 additions & 11 deletions

File tree

src/internal/graphqlTypegenCore.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import j, {
2121
TSTypeParameterInstantiation,
2222
TSQualifiedName,
2323
TSTypeAliasDeclaration,
24+
CallExpression,
2425
} from 'jscodeshift'
2526
import findImports from 'jscodeshift-find-imports'
2627
import addImports from 'jscodeshift-add-imports'
@@ -178,7 +179,7 @@ export default function graphqlTypegenCore(
178179
throw new Error('invalid typeAnnotation')
179180
}
180181

181-
function makeFunctionTypeArguments(
182+
function makeFunctionTypeParameterInstantiation(
182183
data: TypeAlias | TSTypeAliasDeclaration,
183184
variables?: TypeAlias | TSTypeAliasDeclaration | null | undefined
184185
): TypeParameterInstantiation | TSTypeParameterInstantiation {
@@ -553,6 +554,14 @@ export default function graphqlTypegenCore(
553554
})
554555
}
555556

557+
const setTypeParameters = (
558+
node: CallExpression,
559+
params: TypeParameterInstantiation | TSTypeParameterInstantiation
560+
) => {
561+
if (isTS) (node as any).typeParameters = params
562+
else (node as any).typeArguments = params
563+
}
564+
556565
//////////////////////////////////////////////////
557566
// Add types to useQuery hooks
558567

@@ -572,9 +581,9 @@ export default function graphqlTypegenCore(
572581
const { data, variables } = onlyValue(generatedTypes.query) || {}
573582
if (!data || path.node.init?.type !== 'CallExpression') return
574583
if (useFunctionTypeArguments) {
575-
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
576-
data,
577-
variables
584+
setTypeParameters(
585+
path.node.init,
586+
makeFunctionTypeParameterInstantiation(data, variables)
578587
)
579588
} else {
580589
if (
@@ -636,9 +645,9 @@ export default function graphqlTypegenCore(
636645
node: { id },
637646
} = path
638647
if (useFunctionTypeArguments) {
639-
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
640-
data,
641-
variables
648+
setTypeParameters(
649+
path.node.init,
650+
makeFunctionTypeParameterInstantiation(data, variables)
642651
)
643652
} else {
644653
if (!mutationFunction) return
@@ -684,11 +693,11 @@ export default function graphqlTypegenCore(
684693
.forEach((path: ASTPath<VariableDeclarator>): void => {
685694
const { data, variables } =
686695
onlyValue(generatedTypes.subscription) || {}
687-
if (!data) return
696+
if (!data || path.node.init?.type !== 'CallExpression') return
688697
if (useFunctionTypeArguments) {
689-
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
690-
data,
691-
variables
698+
setTypeParameters(
699+
path.node.init,
700+
makeFunctionTypeParameterInstantiation(data, variables)
692701
)
693702
} else {
694703
if (
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as path from 'path'
2+
3+
export const file = 'file.tsx'
4+
export const normalize = false
5+
6+
export const input = `
7+
8+
import * as React from 'react'
9+
import { useMutation } from '@apollo/react-hooks'
10+
import gql from 'graphql-tag'
11+
12+
const mutation = gql\`
13+
mutation createReview($episode: Episode!, $review: ReviewInput!) {
14+
createReview(episode: $episode, review: $review) {
15+
episode
16+
stars
17+
commentary
18+
}
19+
}
20+
\`
21+
22+
const Comp = (props: {id: string}): React.Node => {
23+
const [createReview] = useMutation<X>(mutation)
24+
return <div />
25+
}
26+
`
27+
28+
export const options = {
29+
addTypename: false,
30+
schemaFile: path.resolve(__dirname, '../../../starwars.graphql'),
31+
}
32+
33+
export const expected = `
34+
35+
import * as React from 'react'
36+
import { useMutation } from '@apollo/react-hooks'
37+
import gql from 'graphql-tag'
38+
39+
const mutation = gql\`
40+
mutation createReview($episode: Episode!, $review: ReviewInput!) {
41+
createReview(episode: $episode, review: $review) {
42+
episode
43+
stars
44+
commentary
45+
}
46+
}
47+
\`
48+
49+
// @graphql-typegen auto-generated
50+
type CreateReviewMutationVariables = {
51+
episode: "NEWHOPE" | "EMPIRE" | "JEDI",
52+
review: {
53+
stars: number,
54+
commentary?: string | null,
55+
favorite_color?: {
56+
red: number,
57+
green: number,
58+
blue: number
59+
} | null
60+
}
61+
};
62+
63+
// @graphql-typegen auto-generated
64+
type CreateReviewMutationData = {
65+
createReview: {
66+
episode: "NEWHOPE" | "EMPIRE" | "JEDI" | null,
67+
stars: number,
68+
commentary: string | null
69+
} | null
70+
};
71+
72+
const Comp = (props: {id: string}): React.Node => {
73+
const [createReview] = useMutation<CreateReviewMutationData, CreateReviewMutationVariables>(mutation)
74+
return <div />
75+
}
76+
`

0 commit comments

Comments
 (0)