Skip to content

Commit

Permalink
fix(introspection): primary key info when pk is belongsTo field (#570)
Browse files Browse the repository at this point in the history
* fix(introspection): primary key info when pk is belongsTo field

* rename statement

* update snapshot

* fix lint error
  • Loading branch information
AaronZyLee authored Apr 12, 2023
1 parent a7a5bf0 commit 63183c5
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2173,3 +2173,150 @@ exports[`Primary Key Info tests should generate correct primary key info for mod
\\"nonModels\\": {}
}"
`;

exports[`Primary key info within a belongsTo model tests should generate correct primary key info for model when the primary key field is part of belongsTo connection field and custom PK is disabled 1`] = `
"{
\\"version\\": 1,
\\"models\\": {
\\"Post\\": {
\\"name\\": \\"Post\\",
\\"fields\\": {
\\"postId\\": {
\\"name\\": \\"postId\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"node\\": {
\\"name\\": \\"node\\",
\\"isArray\\": false,
\\"type\\": {
\\"model\\": \\"PostNode\\"
},
\\"isRequired\\": true,
\\"attributes\\": [],
\\"association\\": {
\\"connectionType\\": \\"BELONGS_TO\\",
\\"targetNames\\": [
\\"postId\\"
]
}
},
\\"title\\": {
\\"name\\": \\"title\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"createdAt\\": {
\\"name\\": \\"createdAt\\",
\\"isArray\\": false,
\\"type\\": \\"AWSDateTime\\",
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isReadOnly\\": true
},
\\"updatedAt\\": {
\\"name\\": \\"updatedAt\\",
\\"isArray\\": false,
\\"type\\": \\"AWSDateTime\\",
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isReadOnly\\": true
}
},
\\"syncable\\": true,
\\"pluralName\\": \\"Posts\\",
\\"attributes\\": [
{
\\"type\\": \\"model\\",
\\"properties\\": {}
},
{
\\"type\\": \\"key\\",
\\"properties\\": {
\\"fields\\": [
\\"postId\\"
]
}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": true,
\\"primaryKeyFieldName\\": \\"postId\\",
\\"sortKeyFieldNames\\": []
}
},
\\"PostNode\\": {
\\"name\\": \\"PostNode\\",
\\"fields\\": {
\\"id\\": {
\\"name\\": \\"id\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"post\\": {
\\"name\\": \\"post\\",
\\"isArray\\": false,
\\"type\\": {
\\"model\\": \\"Post\\"
},
\\"isRequired\\": true,
\\"attributes\\": [],
\\"association\\": {
\\"connectionType\\": \\"HAS_ONE\\",
\\"associatedWith\\": [
\\"node\\"
],
\\"targetNames\\": [
\\"postNodePostId\\"
]
}
},
\\"createdAt\\": {
\\"name\\": \\"createdAt\\",
\\"isArray\\": false,
\\"type\\": \\"AWSDateTime\\",
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isReadOnly\\": true
},
\\"updatedAt\\": {
\\"name\\": \\"updatedAt\\",
\\"isArray\\": false,
\\"type\\": \\"AWSDateTime\\",
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isReadOnly\\": true
},
\\"postNodePostId\\": {
\\"name\\": \\"postNodePostId\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
}
},
\\"syncable\\": true,
\\"pluralName\\": \\"PostNodes\\",
\\"attributes\\": [
{
\\"type\\": \\"model\\",
\\"properties\\": {}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": false,
\\"primaryKeyFieldName\\": \\"id\\",
\\"sortKeyFieldNames\\": []
}
}
},
\\"enums\\": {},
\\"nonModels\\": {}
}"
`;
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { buildSchema, GraphQLSchema, parse, visit } from 'graphql';
import { METADATA_SCALAR_MAP } from '../../scalars';
import { directives, scalars } from '../../scalars/supported-directives';
import {
CodeGenConnectionType,
CodeGenFieldConnectionBelongsTo,
CodeGenFieldConnectionHasMany,
CodeGenFieldConnectionHasOne,
} from '../../utils/process-connections';
import { AppSyncModelIntrospectionVisitor } from '../../visitors/appsync-model-introspection-visitor';
import { CodeGenEnum, CodeGenField, CodeGenModel } from '../../visitors/appsync-visitor';

const defaultModelIntropectionVisitorSettings = {
isTimestampFieldsAdded: true,
Expand Down Expand Up @@ -217,4 +210,22 @@ describe('Primary Key Info tests', () => {
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
expect(visitor.generate()).toMatchSnapshot();
});
});

describe('Primary key info within a belongsTo model tests', () => {
const schema = /* GraphQL */ `
type Post @model {
postId: ID! @primaryKey
node: PostNode! @belongsTo(fields: ["postId"])
title: String!
}
type PostNode @model {
id: ID!
post: Post! @hasOne
}
`;
it('should generate correct primary key info for model when the primary key field is part of belongsTo connection field and custom PK is disabled', () => {
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: false });
expect(visitor.generate()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,9 @@ export class AppSyncModelVisitor<
connectionInfo &&
connectionInfo.kind !== CodeGenConnectionType.HAS_MANY &&
connectionInfo.kind !== CodeGenConnectionType.HAS_ONE &&
connectionInfo.targetName !== 'id'
connectionInfo.targetName !== 'id' &&
!(this.config.target === 'introspection' &&
this.getFieldName(getModelPrimaryKeyComponentFields(model)[0]) === connectionInfo.targetName)
) {
// Need to remove the field that is targetName
removeFieldFromModel(model, connectionInfo.targetName);
Expand Down

0 comments on commit 63183c5

Please sign in to comment.