Skip to content

Commit f74aaa7

Browse files
author
Dane Pilcher
authored
fix: correctly remove file extension on types import path (#742)
1 parent 899af2f commit f74aaa7

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ describe('GraphQL statements Formatter', () => {
3636
expect(formattedOutput).toMatchSnapshot();
3737
});
3838

39+
it('Generates formatted output for TS frontend with posix path in same dir', () => {
40+
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', './API.ts').format(statements);
41+
expect(formattedOutput).toMatchSnapshot();
42+
});
43+
44+
it('Generates formatted output for TS frontend with windows path in same dir', () => {
45+
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '.\\API.ts').format(statements);
46+
expect(formattedOutput).toMatchSnapshot();
47+
});
48+
49+
it('Generates formatted output and only remove file extension', () => {
50+
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../Components/Data/API.tsx').format(statements);
51+
expect(formattedOutput).toMatchSnapshot();
52+
});
53+
3954
it('Generates formatted output for Flow frontend', () => {
4055
const formattedOutput = new GraphQLStatementsFormatter('flow').format(statements);
4156
expect(formattedOutput).toMatchSnapshot();

packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`GraphQL statements Formatter Generates formatted output and only remove file extension 1`] = `
4+
"/* tslint:disable */
5+
/* eslint-disable */
6+
// this is an auto generated file. This will be overwritten
7+
8+
import * as APITypes from \\"../Components/Data/API\\";
9+
type GeneratedQuery<InputType, OutputType> = string & {
10+
__generatedQueryInput: InputType;
11+
__generatedQueryOutput: OutputType;
12+
};
13+
14+
export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
15+
getProject(id: $id) {
16+
id
17+
name
18+
createdAt
19+
updatedAt
20+
}
21+
}
22+
\` as GeneratedQuery<
23+
APITypes.GetProjectQueryVariables,
24+
APITypes.GetProjectQuery
25+
>;
26+
"
27+
`;
28+
329
exports[`GraphQL statements Formatter Generates formatted output for Angular frontend 1`] = `
430
"# this is an auto generated file. This will be overwritten
531
@@ -88,6 +114,32 @@ export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
88114
"
89115
`;
90116

117+
exports[`GraphQL statements Formatter Generates formatted output for TS frontend with posix path in same dir 1`] = `
118+
"/* tslint:disable */
119+
/* eslint-disable */
120+
// this is an auto generated file. This will be overwritten
121+
122+
import * as APITypes from \\"./API\\";
123+
type GeneratedQuery<InputType, OutputType> = string & {
124+
__generatedQueryInput: InputType;
125+
__generatedQueryOutput: OutputType;
126+
};
127+
128+
export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
129+
getProject(id: $id) {
130+
id
131+
name
132+
createdAt
133+
updatedAt
134+
}
135+
}
136+
\` as GeneratedQuery<
137+
APITypes.GetProjectQueryVariables,
138+
APITypes.GetProjectQuery
139+
>;
140+
"
141+
`;
142+
91143
exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path 1`] = `
92144
"/* tslint:disable */
93145
/* eslint-disable */
@@ -113,3 +165,29 @@ export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
113165
>;
114166
"
115167
`;
168+
169+
exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path in same dir 1`] = `
170+
"/* tslint:disable */
171+
/* eslint-disable */
172+
// this is an auto generated file. This will be overwritten
173+
174+
import * as APITypes from \\"./API\\";
175+
type GeneratedQuery<InputType, OutputType> = string & {
176+
__generatedQueryInput: InputType;
177+
__generatedQueryOutput: OutputType;
178+
};
179+
180+
export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
181+
getProject(id: $id) {
182+
id
183+
name
184+
createdAt
185+
updatedAt
186+
}
187+
}
188+
\` as GeneratedQuery<
189+
APITypes.GetProjectQueryVariables,
190+
APITypes.GetProjectQuery
191+
>;
192+
"
193+
`;

packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ export class GraphQLStatementsFormatter {
3535
}[operation];
3636
this.lintOverrides = [];
3737
this.headerComments = [];
38-
this.typesPath = typesPath
39-
? typesPath.replace(/.ts/i, '')
40-
// ensure posix path separators are used
41-
.split(path.win32.sep)
42-
.join(path.posix.sep)
43-
: null;
38+
if (typesPath) {
39+
// ensure posix path separators are used
40+
const typesPathWithPosixSep = typesPath.split(path.win32.sep).join(path.posix.sep)
41+
const { dir, name } = path.parse(typesPathWithPosixSep);
42+
const typesPathWithoutExtension = path.join(dir, name);
43+
if (!typesPathWithoutExtension.startsWith('.')) {
44+
// path.join will strip prefixed ./
45+
this.typesPath = `./${typesPathWithoutExtension}`;
46+
} else {
47+
this.typesPath = typesPathWithoutExtension;
48+
}
49+
} else {
50+
this.typesPath = null;
51+
}
4452
this.includeTypeScriptTypes = !!(this.language === 'typescript' && this.opTypeName && this.typesPath);
4553
}
4654

0 commit comments

Comments
 (0)