Skip to content

Commit

Permalink
fix: Custom type for output types
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 7, 2021
1 parent d72d390 commit c9ae9e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,20 @@ describe('custom decorators field custom type namespace', () => {

// it('^', () => console.log(sourceFile.getText()));
});

describe('custom type user model', () => {
before(() => {
setSourceFile('user.model.ts');
});

it('custom type user model email field type', () => {
const decorator = p('email')?.decorators?.find(d => d.name === 'Field');
const typeArgument = decorator?.arguments?.[0];
expect(typeArgument).toEqual('() => Scalars.EmailAddress');
});

// it('^', () => console.log(sourceFile.getText()));
});
});

// it('^', () => console.log(sourceFile.getText()));
42 changes: 25 additions & 17 deletions src/handlers/model-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,35 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
type: outputTypeName,
});

const graphqlType =
customType?.graphqlType ??
getGraphqlType({
let graphqlType: string;
const fieldType = settings?.getFieldType();

if (fieldType) {
graphqlType = fieldType.name;
importDeclarations.create({ ...fieldType });
} else {
graphqlType =
customType?.graphqlType ??
getGraphqlType({
location,
type: outputTypeName,
isId: modelField?.isId,
});

const graphqlImport = getGraphqlImport({
sourceFile,
fileType,
location,
type: outputTypeName,
isId: modelField?.isId,
name: graphqlType,
customType,
getSourceFile,
});

const graphqlImport = getGraphqlImport({
sourceFile,
fileType,
location,
isId: modelField?.isId,
name: graphqlType,
customType,
getSourceFile,
});
if (graphqlImport.name !== outputType.name && graphqlImport.specifier) {
importDeclarations.add(graphqlImport.name, graphqlImport.specifier);
}
}

// console.log({
// 'field.outputType': field.outputType,
Expand All @@ -145,10 +157,6 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {

classStructure.properties?.push(property);

if (graphqlImport.name !== outputType.name && graphqlImport.specifier) {
importDeclarations.add(graphqlImport.name, graphqlImport.specifier);
}

// Create import for typescript field/property type
if (customType && customType.fieldType && customType.fieldModule) {
importDeclarations.add(customType.fieldType, customType.fieldModule);
Expand Down

0 comments on commit c9ae9e9

Please sign in to comment.