Skip to content

Commit

Permalink
Merge pull request #94 from mizdra/follow-up-non-local-schema
Browse files Browse the repository at this point in the history
Fix invalid syntax code generated from an empty schema
  • Loading branch information
mizdra authored Sep 19, 2024
2 parents 58ceb54 + 84c75d3 commit d2dd479
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/__snapshots__/code-generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ export type OptionalNode = OptionalBook | OptionalAuthor;
"
`;

exports[`generateCode > generates code when empty typeInfos is passed 1`] = `
"import {
type DefineTypeFactoryInterface,
defineTypeFactory,
} from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
import type {
Maybe,
} from './types';
export * from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
"
`;
9 changes: 9 additions & 0 deletions src/code-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ describe('generateCode', () => {
const actual = generateCode(config, typeInfos);
expect(actual).toMatchSnapshot();
});
it('generates code when empty typeInfos is passed', () => {
const config = fakeConfig({
typesFile: './types',
skipTypename: oneOf([true, false]),
});
const typeInfos: TypeInfo[] = [];
const actual = generateCode(config, typeInfos);
expect(actual).toMatchSnapshot();
});
});
7 changes: 3 additions & 4 deletions src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import type { ObjectTypeInfo, TypeInfo } from './schema-scanner.js';
function generatePreludeCode(config: Config, typeInfos: TypeInfo[]): string {
const joinedTypeNames = typeInfos
.filter(({ type }) => type === 'object')
.map(({ name }) => ` ${name}`)
.join(',\n');
.map(({ name }) => ` ${name},\n`)
.join('');
const code = `
import {
type DefineTypeFactoryInterface${config.nonOptionalDefaultFields ? 'Required' : ''},
defineTypeFactory,
} from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
import type {
Maybe,
${joinedTypeNames},
} from '${config.typesFile}';
${joinedTypeNames}} from '${config.typesFile}';
export * from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
`.trim();
Expand Down
7 changes: 7 additions & 0 deletions src/schema-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ describe('getTypeInfos', () => {
]
`);
});
it('returns empty array when schema without type is passed', () => {
const schema = buildSchema(`
scalar Date
`);
const config: Config = fakeConfig();
expect(getTypeInfos(config, schema)).toMatchInlineSnapshot(`[]`);
});
it('includes description comment', () => {
const schema = buildSchema(`
"The book"
Expand Down

0 comments on commit d2dd479

Please sign in to comment.