From af4b4204b985696dd369b0bb68704f2c1e13a5d5 Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 13 Oct 2023 08:30:52 -0600 Subject: [PATCH 1/2] fix: correctly remove file extension on types import path --- .../utils/GraphQLStatementsFormatter.test.ts | 5 ++++ .../GraphQLStatementsFormatter.test.ts.snap | 26 +++++++++++++++++++ .../src/utils/GraphQLStatementsFormatter.ts | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts b/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts index a9a049b7d..260533db3 100644 --- a/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts +++ b/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts @@ -36,6 +36,11 @@ describe('GraphQL statements Formatter', () => { expect(formattedOutput).toMatchSnapshot(); }); + it('Generates formatted output and only remove file extension', () => { + const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../Components/Data/API.tsx').format(statements); + expect(formattedOutput).toMatchSnapshot(); + }); + it('Generates formatted output for Flow frontend', () => { const formattedOutput = new GraphQLStatementsFormatter('flow').format(statements); expect(formattedOutput).toMatchSnapshot(); diff --git a/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap b/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap index 72114f01c..73740c14f 100644 --- a/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap +++ b/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap @@ -1,5 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`GraphQL statements Formatter Generates formatted output and only remove file extension 1`] = ` +"/* tslint:disable */ +/* eslint-disable */ +// this is an auto generated file. This will be overwritten + +import * as APITypes from \\"../Components/Data/API\\"; +type GeneratedQuery = string & { + __generatedQueryInput: InputType; + __generatedQueryOutput: OutputType; +}; + +export const getProject = /* GraphQL */ \`query GetProject($id: ID!) { + getProject(id: $id) { + id + name + createdAt + updatedAt + } +} +\` as GeneratedQuery< + APITypes.GetProjectQueryVariables, + APITypes.GetProjectQuery +>; +" +`; + exports[`GraphQL statements Formatter Generates formatted output for Angular frontend 1`] = ` "# this is an auto generated file. This will be overwritten diff --git a/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts b/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts index 0958df584..ffa49e299 100644 --- a/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts +++ b/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts @@ -36,7 +36,7 @@ export class GraphQLStatementsFormatter { this.lintOverrides = []; this.headerComments = []; this.typesPath = typesPath - ? typesPath.replace(/.ts/i, '') + ? typesPath.replace(/\.[^.]+$/, '') // remove file extensions // ensure posix path separators are used .split(path.win32.sep) .join(path.posix.sep) From 81dfb43353b130c2876b7b8fc97a29546d9d2dad Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 13 Oct 2023 13:11:37 -0600 Subject: [PATCH 2/2] fix: use path.parse to remove file extension --- .../utils/GraphQLStatementsFormatter.test.ts | 10 ++++ .../GraphQLStatementsFormatter.test.ts.snap | 52 +++++++++++++++++++ .../src/utils/GraphQLStatementsFormatter.ts | 20 ++++--- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts b/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts index 260533db3..644e9a8eb 100644 --- a/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts +++ b/packages/graphql-generator/src/__tests__/utils/GraphQLStatementsFormatter.test.ts @@ -36,6 +36,16 @@ describe('GraphQL statements Formatter', () => { expect(formattedOutput).toMatchSnapshot(); }); + it('Generates formatted output for TS frontend with posix path in same dir', () => { + const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', './API.ts').format(statements); + expect(formattedOutput).toMatchSnapshot(); + }); + + it('Generates formatted output for TS frontend with windows path in same dir', () => { + const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '.\\API.ts').format(statements); + expect(formattedOutput).toMatchSnapshot(); + }); + it('Generates formatted output and only remove file extension', () => { const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../Components/Data/API.tsx').format(statements); expect(formattedOutput).toMatchSnapshot(); diff --git a/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap b/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap index 73740c14f..871c73f3f 100644 --- a/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap +++ b/packages/graphql-generator/src/__tests__/utils/__snapshots__/GraphQLStatementsFormatter.test.ts.snap @@ -114,6 +114,32 @@ export const getProject = /* GraphQL */ \`query GetProject($id: ID!) { " `; +exports[`GraphQL statements Formatter Generates formatted output for TS frontend with posix path in same dir 1`] = ` +"/* tslint:disable */ +/* eslint-disable */ +// this is an auto generated file. This will be overwritten + +import * as APITypes from \\"./API\\"; +type GeneratedQuery = string & { + __generatedQueryInput: InputType; + __generatedQueryOutput: OutputType; +}; + +export const getProject = /* GraphQL */ \`query GetProject($id: ID!) { + getProject(id: $id) { + id + name + createdAt + updatedAt + } +} +\` as GeneratedQuery< + APITypes.GetProjectQueryVariables, + APITypes.GetProjectQuery +>; +" +`; + exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path 1`] = ` "/* tslint:disable */ /* eslint-disable */ @@ -139,3 +165,29 @@ export const getProject = /* GraphQL */ \`query GetProject($id: ID!) { >; " `; + +exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path in same dir 1`] = ` +"/* tslint:disable */ +/* eslint-disable */ +// this is an auto generated file. This will be overwritten + +import * as APITypes from \\"./API\\"; +type GeneratedQuery = string & { + __generatedQueryInput: InputType; + __generatedQueryOutput: OutputType; +}; + +export const getProject = /* GraphQL */ \`query GetProject($id: ID!) { + getProject(id: $id) { + id + name + createdAt + updatedAt + } +} +\` as GeneratedQuery< + APITypes.GetProjectQueryVariables, + APITypes.GetProjectQuery +>; +" +`; diff --git a/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts b/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts index ffa49e299..2c894af0e 100644 --- a/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts +++ b/packages/graphql-generator/src/utils/GraphQLStatementsFormatter.ts @@ -35,12 +35,20 @@ export class GraphQLStatementsFormatter { }[operation]; this.lintOverrides = []; this.headerComments = []; - this.typesPath = typesPath - ? typesPath.replace(/\.[^.]+$/, '') // remove file extensions - // ensure posix path separators are used - .split(path.win32.sep) - .join(path.posix.sep) - : null; + if (typesPath) { + // ensure posix path separators are used + const typesPathWithPosixSep = typesPath.split(path.win32.sep).join(path.posix.sep) + const { dir, name } = path.parse(typesPathWithPosixSep); + const typesPathWithoutExtension = path.join(dir, name); + if (!typesPathWithoutExtension.startsWith('.')) { + // path.join will strip prefixed ./ + this.typesPath = `./${typesPathWithoutExtension}`; + } else { + this.typesPath = typesPathWithoutExtension; + } + } else { + this.typesPath = null; + } this.includeTypeScriptTypes = !!(this.language === 'typescript' && this.opTypeName && this.typesPath); }