Skip to content

Commit

Permalink
fix: use correct path separators on windows enviroment in generated r…
Browse files Browse the repository at this point in the history
…esources
  • Loading branch information
dpilch committed Oct 18, 2023
1 parent f74aaa7 commit 343d298
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as path from 'path';
import { GraphQLStatementsFormatter } from '../../utils';

jest.mock('path', () => ({
...jest.requireActual('path'),
join: jest.fn(jest.requireActual('path').join),
}));

describe('GraphQL statements Formatter', () => {
const statements = new Map();

Expand Down Expand Up @@ -46,6 +52,13 @@ describe('GraphQL statements Formatter', () => {
expect(formattedOutput).toMatchSnapshot();
});

// simulate windows path.join functionality until tests are run on windows
it('Generates formatted output for TS frontend with windows simulated', () => {
path.join.mockReturnValueOnce('..\\types\\API.ts');
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../types/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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,29 @@ export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
>;
"
`;

exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows simulated 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten
import * as APITypes from \\"../types/API.ts\\";
type GeneratedQuery<InputType, OutputType> = 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
>;
"
`;
3 changes: 2 additions & 1 deletion packages/graphql-generator/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import { parse } from 'graphql';
import * as appSyncDataStoreCodeGen from '@aws-amplify/appsync-modelgen-plugin';
import { codegen } from '@graphql-codegen/core';
Expand Down Expand Up @@ -61,7 +62,7 @@ export async function generateModels(options: GenerateModelsOptions): Promise<Ge
return Promise.all(
appsyncLocalConfig.map(async config => {
const content = await codegen(config);
return { [config.filename]: content };
return { [config.filename.split(path.win32.sep).join(path.posix.sep)]: content };
}),
).then((outputs: GeneratedOutput[]) => outputs.reduce((curr, next) => ({ ...curr, ...next }), {}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export class GraphQLStatementsFormatter {
this.lintOverrides = [];
this.headerComments = [];
if (typesPath) {
const { dir, name } = path.parse(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);
const typesPathWithoutExtension = path.join(dir, name).split(path.win32.sep).join(path.posix.sep);
if (!typesPathWithoutExtension.startsWith('.')) {
// path.join will strip prefixed ./
this.typesPath = `./${typesPathWithoutExtension}`;
Expand Down

0 comments on commit 343d298

Please sign in to comment.