Skip to content

Commit

Permalink
Fixed addition of Exact type to output (#4832)
Browse files Browse the repository at this point in the history
* Fixed addition of Exact type to output

* Added changeset

* Moved FixDecorator to getWrapperDefinitions

Co-authored-by: Borre <borre.mosch@cubonacci.com>
  • Loading branch information
borremosch and Borre authored Oct 10, 2020
1 parent 9fcfc14 commit b412d85
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-starfishes-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-type-graphql': patch
---

Added missing Entry<> type
6 changes: 2 additions & 4 deletions packages/plugins/typescript/type-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { TypeGraphQLPluginConfig } from './config';

export * from './visitor';

const TYPE_GRAPHQL_IMPORT = `import * as TypeGraphQL from 'type-graphql';`;
const DECORATOR_FIX = `type FixDecorator<T> = T;`;
const TYPE_GRAPHQL_IMPORT = `import * as TypeGraphQL from 'type-graphql';\nexport { TypeGraphQL };`;
const isDefinitionInterface = (definition: string) => definition.includes('@TypeGraphQL.InterfaceType()');

export const plugin: PluginFunction<TypeGraphQLPluginConfig, Types.ComplexPluginOutput> = (
Expand All @@ -18,7 +17,6 @@ export const plugin: PluginFunction<TypeGraphQLPluginConfig, Types.ComplexPlugin
const visitor = new TypeGraphQLVisitor(schema, config);
const printedSchema = printSchema(schema);
const astNode = parse(printedSchema);
const maybeValue = `export type Maybe<T> = ${visitor.config.maybeValue};`;
const visitorResult = visit(astNode, { leave: visitor });
const introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config);
const scalars = visitor.scalarsDefinition;
Expand All @@ -30,7 +28,7 @@ export const plugin: PluginFunction<TypeGraphQLPluginConfig, Types.ComplexPlugin
);

return {
prepend: [...visitor.getEnumsImports(), maybeValue, TYPE_GRAPHQL_IMPORT, DECORATOR_FIX],
prepend: [...visitor.getEnumsImports(), ...visitor.getWrapperDefinitions(), TYPE_GRAPHQL_IMPORT],
content: [scalars, ...definitions, ...introspectionDefinitions].join('\n'),
};
};
Expand Down
10 changes: 10 additions & 0 deletions packages/plugins/typescript/type-graphql/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ interface Type {
isScalar: boolean;
}

const FIX_DECORATOR_SIGNATURE = `type FixDecorator<T> = T;`;

export class TypeGraphQLVisitor<
TRawConfig extends TypeGraphQLPluginConfig = TypeGraphQLPluginConfig,
TParsedConfig extends TypeGraphQLPluginParsedConfig = TypeGraphQLPluginParsedConfig
Expand Down Expand Up @@ -104,6 +106,14 @@ export class TypeGraphQLVisitor<
});
}

public getWrapperDefinitions(): string[] {
return [...super.getWrapperDefinitions(), this.getFixDecoratorDefinition()];
}

public getFixDecoratorDefinition(): string {
return `${this.getExportPrefix()}${FIX_DECORATOR_SIGNATURE}`;
}

ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string {
const typeDecorator = this.config.decoratorName.type;
const originalNode = parent[key] as ObjectTypeDefinitionNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@ import { buildSchema } from 'graphql';
import { plugin } from '../src/index';

describe('type-graphql', () => {
it('should generate type-graphql imports', async () => {
it('should expose Maybe', async () => {
const schema = buildSchema(/* GraphQL */ `
scalar A
`);
const result = await plugin(schema, [], {}, { outputFile: '' });
expect(result.prepend).toBeSimilarStringTo('export type Maybe<T> =');
});

it('should expose Exact', async () => {
const schema = buildSchema(/* GraphQL */ `
scalar A
`);
const result = await plugin(schema, [], {}, { outputFile: '' });
expect(result.prepend).toBeSimilarStringTo('export type Exact<');
});

it('should expose FixDecorator', async () => {
const schema = buildSchema(/* GraphQL */ `
scalar A
`);
const result = await plugin(schema, [], {}, { outputFile: '' });
expect(result.prepend).toBeSimilarStringTo('export type FixDecorator<T> = T;');
});

it('should generate type-graphql import/export', async () => {
const schema = buildSchema(/* GraphQL */ `
scalar A
`);
const result = await plugin(schema, [], {}, { outputFile: '' });

expect(result.prepend).toContainEqual(`import * as TypeGraphQL from 'type-graphql';`);
expect(result.prepend).toBeSimilarStringTo(`import * as TypeGraphQL from 'type-graphql';
export { TypeGraphQL };`);
});

it('should generate type-graphql enums', async () => {
Expand Down

0 comments on commit b412d85

Please # to comment.