Skip to content

Commit

Permalink
fix: fix issue with import type specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed May 10, 2024
1 parent d5f8300 commit 4814b34
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .vscode/snippets.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,27 @@
"}",
""
]
},
"testcase": {
"prefix": "testcase",
"body": [
"import * as path from 'path'",
"",
"export const input = `",
"import gql from 'graphql-tag'",
"",
"const query = gql\\`",
"\\`",
"`",
"",
"export const options = {",
" addTypename: true,",
" schemaFile: path.resolve(__dirname, '../../../starwars.graphql'),",
"}",
"",
"export const expected = `",
"`",
""
]
}
}
36 changes: 34 additions & 2 deletions src/internal/graphqlTypegenCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import j, {
TSQualifiedName,
TSTypeAliasDeclaration,
CallExpression,
ImportDeclaration,
} from 'jscodeshift'
import findImports from 'jscodeshift-find-imports'
import addImports from 'jscodeshift-add-imports'
import _addImports from 'jscodeshift-add-imports'
import generateFlowTypesFromDocument from './generateFlowTypesFromDocument'
import generateTSTypesFromDocument from './generateTSTypesFromDocument'
import * as graphql from 'graphql'
import once from 'lodash/once'
import { once, omit } from 'lodash'
import {
ExpressionKind,
FlowTypeKind,
Expand Down Expand Up @@ -780,3 +781,34 @@ function getChildFunction(elementPath: ASTPath<any>): ASTPath<any> | null {
}
return null
}

function addImports(
root: Collection<any>,
imports: ImportDeclaration | ImportDeclaration[]
): Record<string, string> {
return _addImports(
root,
(Array.isArray(imports) ? imports : [imports]).flatMap((statement) => {
const specifiers = statement.specifiers?.filter((spec) => {
if (
spec.local?.name &&
(statement.importKind === 'type' ||
(spec?.type === 'ImportSpecifier' &&
(spec as any).importKind === 'type'))
) {
const otherDecl = {
...statement,
importKind: 'value',
specifiers: [omit(spec, 'importKind')],
}
if (findImports(root, otherDecl)[spec.local?.name]) {
return false
}
}
return true
})

return specifiers?.length ? [{ ...statement, specifiers }] : []
})
)
}
56 changes: 56 additions & 0 deletions test/graphql-typegen-async/ts/typeAndValueImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as path from 'path'

export const input = `
import gql from 'graphql-tag'
import { DateISOString } from 'date-iso-string'
const query = gql\`
query Test($id: ID!) {
human(id: $id) {
id
name
# @graphql-typegen external as import { type Blah } from 'date-iso-string'
appearsIn
# @graphql-typegen external as import { type DateISOString } from 'date-iso-string'
birthday
}
}
\`
`

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

export const expected = `
import gql from 'graphql-tag'
import { DateISOString, type Blah } from 'date-iso-string'
const query = gql\`
query Test($id: ID!) {
human(id: $id) {
id
name
# @graphql-typegen external as import { type Blah } from 'date-iso-string'
appearsIn
# @graphql-typegen external as import { type DateISOString } from 'date-iso-string'
birthday
}
}
\`
// @graphql-typegen auto-generated
type TestQueryVariables = {
id: string,
}
// @graphql-typegen auto-generated
type TestQueryData = {
human: {
id: string,
name: string,
appearsIn: Array<Blah | null>,
birthday: DateISOString | null,
} | null,
}
`

0 comments on commit 4814b34

Please sign in to comment.