Skip to content

Commit 4814b34

Browse files
committed
fix: fix issue with import type specifiers
1 parent d5f8300 commit 4814b34

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

.vscode/snippets.code-snippets

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,27 @@
3636
"}",
3737
""
3838
]
39+
},
40+
"testcase": {
41+
"prefix": "testcase",
42+
"body": [
43+
"import * as path from 'path'",
44+
"",
45+
"export const input = `",
46+
"import gql from 'graphql-tag'",
47+
"",
48+
"const query = gql\\`",
49+
"\\`",
50+
"`",
51+
"",
52+
"export const options = {",
53+
" addTypename: true,",
54+
" schemaFile: path.resolve(__dirname, '../../../starwars.graphql'),",
55+
"}",
56+
"",
57+
"export const expected = `",
58+
"`",
59+
""
60+
]
3961
}
4062
}

src/internal/graphqlTypegenCore.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import j, {
2222
TSQualifiedName,
2323
TSTypeAliasDeclaration,
2424
CallExpression,
25+
ImportDeclaration,
2526
} from 'jscodeshift'
2627
import findImports from 'jscodeshift-find-imports'
27-
import addImports from 'jscodeshift-add-imports'
28+
import _addImports from 'jscodeshift-add-imports'
2829
import generateFlowTypesFromDocument from './generateFlowTypesFromDocument'
2930
import generateTSTypesFromDocument from './generateTSTypesFromDocument'
3031
import * as graphql from 'graphql'
31-
import once from 'lodash/once'
32+
import { once, omit } from 'lodash'
3233
import {
3334
ExpressionKind,
3435
FlowTypeKind,
@@ -780,3 +781,34 @@ function getChildFunction(elementPath: ASTPath<any>): ASTPath<any> | null {
780781
}
781782
return null
782783
}
784+
785+
function addImports(
786+
root: Collection<any>,
787+
imports: ImportDeclaration | ImportDeclaration[]
788+
): Record<string, string> {
789+
return _addImports(
790+
root,
791+
(Array.isArray(imports) ? imports : [imports]).flatMap((statement) => {
792+
const specifiers = statement.specifiers?.filter((spec) => {
793+
if (
794+
spec.local?.name &&
795+
(statement.importKind === 'type' ||
796+
(spec?.type === 'ImportSpecifier' &&
797+
(spec as any).importKind === 'type'))
798+
) {
799+
const otherDecl = {
800+
...statement,
801+
importKind: 'value',
802+
specifiers: [omit(spec, 'importKind')],
803+
}
804+
if (findImports(root, otherDecl)[spec.local?.name]) {
805+
return false
806+
}
807+
}
808+
return true
809+
})
810+
811+
return specifiers?.length ? [{ ...statement, specifiers }] : []
812+
})
813+
)
814+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as path from 'path'
2+
3+
export const input = `
4+
import gql from 'graphql-tag'
5+
import { DateISOString } from 'date-iso-string'
6+
7+
const query = gql\`
8+
query Test($id: ID!) {
9+
human(id: $id) {
10+
id
11+
name
12+
# @graphql-typegen external as import { type Blah } from 'date-iso-string'
13+
appearsIn
14+
# @graphql-typegen external as import { type DateISOString } from 'date-iso-string'
15+
birthday
16+
}
17+
}
18+
\`
19+
`
20+
21+
export const options = {
22+
addTypename: false,
23+
schemaFile: path.resolve(__dirname, '../../../starwars.graphql'),
24+
}
25+
26+
export const expected = `
27+
import gql from 'graphql-tag'
28+
import { DateISOString, type Blah } from 'date-iso-string'
29+
30+
const query = gql\`
31+
query Test($id: ID!) {
32+
human(id: $id) {
33+
id
34+
name
35+
# @graphql-typegen external as import { type Blah } from 'date-iso-string'
36+
appearsIn
37+
# @graphql-typegen external as import { type DateISOString } from 'date-iso-string'
38+
birthday
39+
}
40+
}
41+
\`
42+
// @graphql-typegen auto-generated
43+
type TestQueryVariables = {
44+
id: string,
45+
}
46+
47+
// @graphql-typegen auto-generated
48+
type TestQueryData = {
49+
human: {
50+
id: string,
51+
name: string,
52+
appearsIn: Array<Blah | null>,
53+
birthday: DateISOString | null,
54+
} | null,
55+
}
56+
`

0 commit comments

Comments
 (0)