Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fragment-matcher-indentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/fragment-matcher': patch
---

Fix indentation of generated TypeScript and JavaScript output. The plugin previously emitted declarations indented by six spaces while the `JSON.stringify` content was flush left, producing misaligned code (for example an empty `possibleTypes` map). The generated output is now emitted flush left with consistent indentation.
41 changes: 19 additions & 22 deletions packages/plugins/other/fragment-matcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ export const plugin: PluginFunction = async (
const defaultExportStatement =
config.module === 'es2015' ? `export default` : 'module.exports =';

return `
${defaultExportStatement} ${content}
`;
return `${defaultExportStatement} ${content}\n`;
}

if (extensions.ts.includes(ext)) {
Expand All @@ -228,29 +226,28 @@ export const plugin: PluginFunction = async (
type = `export type ${typename} = ${content};`;
} else if (apolloClientVersion === 2) {
type = `export interface ${typename} {
__schema: {
types: {
kind: string;
name: string;
possibleTypes: {
name: string;
}[];
}[];
};
}`;
__schema: {
types: {
kind: string;
name: string;
possibleTypes: {
name: string;
}[];
}[];
};
}`;
} else if (apolloClientVersion === 3) {
type = `export interface ${typename} {
possibleTypes: {
[key: string]: string[]
}
}`;
possibleTypes: {
[key: string]: string[]
}
}`;
}

return `
${type}
const result: ${typename} = ${content};
export default result;
`;
return `${type}
const result: ${typename} = ${content};
export default result;
`;
}

throw new Error(`Extension ${ext} is not supported`);
Expand Down
37 changes: 37 additions & 0 deletions packages/plugins/other/fragment-matcher/tests/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ describe('Fragment Matcher Plugin', () => {
expect(jsxContent).toBeSimilarStringTo(output);
});

it('should generate correctly indented code', async () => {
const jsContent = await plugin(
schema,
[],
{
apolloClientVersion: 2,
},
{
outputFile: 'foo.js',
},
);

expect(jsContent).toEqual(`export default ${introspection}\n`);
});

it('should be able to use commonjs', async () => {
const jsContent = await plugin(
schema,
Expand Down Expand Up @@ -254,6 +269,28 @@ describe('Fragment Matcher Plugin', () => {
expect(tsxContent).toBeSimilarStringTo(output);
});

it('should generate correctly indented code', async () => {
const tsContent = await plugin(
schema,
[],
{
apolloClientVersion: 3,
},
{
outputFile: 'foo.ts',
},
);

expect(tsContent).toEqual(`export interface PossibleTypesResultData {
possibleTypes: {
[key: string]: string[]
}
}
const result: PossibleTypesResultData = ${apolloClient3Result};
export default result;
`);
});

it('should use es2015 even though commonjs is requested', async () => {
const tsContent = await plugin(
schema,
Expand Down
Loading