Skip to content

Commit 24e1c31

Browse files
authored
Merge pull request #281 from lazpavel/has-one-directive-revert
fix(amplify-codegen): revert for #276
2 parents ff9d1a9 + 770777e commit 24e1c31

File tree

2 files changed

+8
-34
lines changed

2 files changed

+8
-34
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,6 @@ describe('AppSyncModelVisitor', () => {
317317
expect(commentsField).not.toContain('post');
318318
expect(commentsField).toContain('postCommentsId'); // because of connection from Post.comments
319319
});
320-
321-
it('should not generate projectTeamId connection field for hasOne directive', () => {
322-
const schema = /* GraphQL */ `
323-
type Project @model {
324-
id: ID!
325-
name: String
326-
team: Team @hasOne
327-
}
328-
329-
type Team @model {
330-
id: ID!
331-
name: String!
332-
}
333-
`;
334-
const ast = parse(schema);
335-
const builtSchema = buildSchemaWithDirectives(schema);
336-
const visitor = new AppSyncModelVisitor(
337-
builtSchema,
338-
{ directives, target: 'typescript', generate: CodeGenGenerateEnum.code, usePipelinedTransformer: true },
339-
{},
340-
);
341-
visit(ast, { leave: visitor });
342-
visitor.generate();
343-
const teamFields = visitor.models.Team.fields.map(field => field.name);
344-
expect(teamFields).not.toContain('projectTeamId');
345-
});
346320
});
347321

348322
describe('auth directive', () => {

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@ export class AppSyncModelVisitor<
294294
}
295295
processDirectives() {
296296
if (this.config.usePipelinedTransformer || this.config.transformerVersion === 2) {
297-
this.processConnectionDirectivesV2()
298-
}
299-
else {
297+
this.processConnectionDirectivesV2();
298+
} else {
300299
this.processConnectionDirective();
301300
}
302301
this.processAuthDirectives();
@@ -441,9 +440,10 @@ export class AppSyncModelVisitor<
441440
const typeArr: any[] = [];
442441
Object.values({ ...this.modelMap, ...this.nonModelMap }).forEach((obj: CodeGenModel) => {
443442
// include only key directive as we don't care about others for versioning
444-
const directives = (this.config.usePipelinedTransformer || this.config.transformerVersion === 2)
445-
? obj.directives.filter(dir => dir.name === 'primaryKey' || dir.name === 'index')
446-
: obj.directives.filter(dir => dir.name === 'key');
443+
const directives =
444+
this.config.usePipelinedTransformer || this.config.transformerVersion === 2
445+
? obj.directives.filter(dir => dir.name === 'primaryKey' || dir.name === 'index')
446+
: obj.directives.filter(dir => dir.name === 'key');
447447
const fields = obj.fields
448448
.map((field: CodeGenField) => {
449449
// include only connection field and type
@@ -633,7 +633,7 @@ export class AppSyncModelVisitor<
633633
Object.values(this.modelMap).forEach(model => {
634634
model.fields.forEach(field => {
635635
field.directives.forEach(dir => {
636-
if(dir.name === 'manyToMany') {
636+
if (dir.name === 'manyToMany') {
637637
let relationName = graphqlName(toUpper(dir.arguments.relationName));
638638
let existingRelation = manyDirectiveMap.get(relationName);
639639
if (existingRelation) {
@@ -679,7 +679,7 @@ export class AppSyncModelVisitor<
679679
model.fields.forEach(field => {
680680
const connectionInfo = processConnectionsV2(field, model, this.modelMap);
681681
if (connectionInfo) {
682-
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
682+
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY || connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
683683
// Need to update the other side of the connection even if there is no connection directive
684684
addFieldToModel(connectionInfo.connectedModel, connectionInfo.associatedWith);
685685
} else if (connectionInfo.targetName !== 'id') {

0 commit comments

Comments
 (0)