Skip to content

Commit

Permalink
fix: get TS function call type params working
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed May 4, 2024
1 parent b912bc1 commit 708e6dc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/internal/graphqlTypegenCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import j, {
TSTypeParameterInstantiation,
TSQualifiedName,
TSTypeAliasDeclaration,
CallExpression,
} from 'jscodeshift'
import findImports from 'jscodeshift-find-imports'
import addImports from 'jscodeshift-add-imports'
Expand Down Expand Up @@ -178,7 +179,7 @@ export default function graphqlTypegenCore(
throw new Error('invalid typeAnnotation')
}

function makeFunctionTypeArguments(
function makeFunctionTypeParameterInstantiation(
data: TypeAlias | TSTypeAliasDeclaration,
variables?: TypeAlias | TSTypeAliasDeclaration | null | undefined
): TypeParameterInstantiation | TSTypeParameterInstantiation {
Expand Down Expand Up @@ -553,6 +554,14 @@ export default function graphqlTypegenCore(
})
}

const setTypeParameters = (
node: CallExpression,
params: TypeParameterInstantiation | TSTypeParameterInstantiation
) => {
if (isTS) (node as any).typeParameters = params
else (node as any).typeArguments = params
}

//////////////////////////////////////////////////
// Add types to useQuery hooks

Expand All @@ -572,9 +581,9 @@ export default function graphqlTypegenCore(
const { data, variables } = onlyValue(generatedTypes.query) || {}
if (!data || path.node.init?.type !== 'CallExpression') return
if (useFunctionTypeArguments) {
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
data,
variables
setTypeParameters(
path.node.init,
makeFunctionTypeParameterInstantiation(data, variables)
)
} else {
if (
Expand Down Expand Up @@ -636,9 +645,9 @@ export default function graphqlTypegenCore(
node: { id },
} = path
if (useFunctionTypeArguments) {
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
data,
variables
setTypeParameters(
path.node.init,
makeFunctionTypeParameterInstantiation(data, variables)
)
} else {
if (!mutationFunction) return
Expand Down Expand Up @@ -684,11 +693,11 @@ export default function graphqlTypegenCore(
.forEach((path: ASTPath<VariableDeclarator>): void => {
const { data, variables } =
onlyValue(generatedTypes.subscription) || {}
if (!data) return
if (!data || path.node.init?.type !== 'CallExpression') return
if (useFunctionTypeArguments) {
;(path.node.init as any).typeArguments = makeFunctionTypeArguments(
data,
variables
setTypeParameters(
path.node.init,
makeFunctionTypeParameterInstantiation(data, variables)
)
} else {
if (
Expand Down
76 changes: 76 additions & 0 deletions test/graphql-typegen-async/ts/useMutationReplaceTypeParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as path from 'path'

export const file = 'file.tsx'
export const normalize = false

export const input = `
import * as React from 'react'
import { useMutation } from '@apollo/react-hooks'
import gql from 'graphql-tag'
const mutation = gql\`
mutation createReview($episode: Episode!, $review: ReviewInput!) {
createReview(episode: $episode, review: $review) {
episode
stars
commentary
}
}
\`
const Comp = (props: {id: string}): React.Node => {
const [createReview] = useMutation<X>(mutation)
return <div />
}
`

export const options = {
addTypename: false,
schemaFile: path.resolve(__dirname, '../../../starwars.graphql'),
}

export const expected = `
import * as React from 'react'
import { useMutation } from '@apollo/react-hooks'
import gql from 'graphql-tag'
const mutation = gql\`
mutation createReview($episode: Episode!, $review: ReviewInput!) {
createReview(episode: $episode, review: $review) {
episode
stars
commentary
}
}
\`
// @graphql-typegen auto-generated
type CreateReviewMutationVariables = {
episode: "NEWHOPE" | "EMPIRE" | "JEDI",
review: {
stars: number,
commentary?: string | null,
favorite_color?: {
red: number,
green: number,
blue: number
} | null
}
};
// @graphql-typegen auto-generated
type CreateReviewMutationData = {
createReview: {
episode: "NEWHOPE" | "EMPIRE" | "JEDI" | null,
stars: number,
commentary: string | null
} | null
};
const Comp = (props: {id: string}): React.Node => {
const [createReview] = useMutation<CreateReviewMutationData, CreateReviewMutationVariables>(mutation)
return <div />
}
`

0 comments on commit 708e6dc

Please sign in to comment.