Skip to content

Commit 57d3c89

Browse files
fix(graphql): merge amended metadata to serialized metadata
1 parent 54ff266 commit 57d3c89

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

packages/graphql/lib/plugin/visitors/model-class.visitor.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class ModelClassVisitor {
9393
}
9494
return;
9595
}
96-
const members = this.amendFieldsDecorators(
96+
const [members, amendedMetadata] = this.amendFieldsDecorators(
9797
factory,
9898
node.members,
9999
pluginOptions,
@@ -126,7 +126,11 @@ export class ModelClassVisitor {
126126
this._collectedMetadata[filePath] = {};
127127
}
128128
const attributeKey = node.name.getText();
129-
this._collectedMetadata[filePath][attributeKey] = metadata;
129+
this._collectedMetadata[filePath][attributeKey] = safelyMergeObjects(
130+
factory,
131+
metadata,
132+
amendedMetadata,
133+
);
130134
return;
131135
}
132136
} else if (ts.isSourceFile(node) && !pluginOptions.readonly) {
@@ -210,8 +214,9 @@ export class ModelClassVisitor {
210214
pluginOptions: PluginOptions,
211215
hostFilename: string, // sourceFile.fileName,
212216
typeChecker: ts.TypeChecker | undefined,
213-
): ts.ClassElement[] {
214-
return members.map((member) => {
217+
): [ts.ClassElement[], ts.ObjectLiteralExpression] {
218+
const propertyAssignments: ts.PropertyAssignment[] = [];
219+
const updatedClassElements = members.map((member) => {
215220
const decorators = getDecorators(member);
216221
if (
217222
(ts.isPropertyDeclaration(member) || ts.isGetAccessor(member)) &&
@@ -241,6 +246,13 @@ export class ModelClassVisitor {
241246
f,
242247
metadata as any,
243248
);
249+
250+
propertyAssignments.push(
251+
f.createPropertyAssignment(
252+
f.createIdentifier(member.name.getText()),
253+
serializedMetadata,
254+
),
255+
);
244256
return [
245257
type,
246258
options
@@ -256,6 +268,11 @@ export class ModelClassVisitor {
256268

257269
return member;
258270
});
271+
272+
return [
273+
updatedClassElements,
274+
f.createObjectLiteralExpression(propertyAssignments),
275+
];
259276
}
260277

261278
private collectMetadataFromClassMembers(

packages/graphql/tests/plugin/fixtures/project/recipes/models/recipe.model.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectType } from '../../../../../../lib/decorators';
1+
import { Field, ObjectType } from '../../../../../../lib/decorators';
22
import { Ingredient } from './ingredient.model';
33

44
@ObjectType({ description: 'recipe ' })
@@ -13,6 +13,10 @@ export class Recipe {
1313

1414
description?: string;
1515

16+
/**
17+
* Creation date of the recipe
18+
*/
19+
@Field((type) => Date)
1620
creationDate: Date;
1721

1822
ingredients: Ingredient[];

packages/graphql/tests/plugin/fixtures/serialized-meta.fixture.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export default async () => {
4949
description: 'The title of the recipe'
5050
},
5151
description: { nullable: true, type: () => String },
52-
creationDate: { type: () => Date },
5352
ingredients: {
5453
type: () => [t['./recipes/models/ingredient.model'].Ingredient]
5554
},
5655
primary: {
5756
type: () => t['./recipes/models/ingredient.model'].Ingredient
58-
}
57+
},
58+
creationDate: { description: 'Creation date of the recipe' }
5959
}
6060
}
6161
]

0 commit comments

Comments
 (0)