diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-model-introspection-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-model-introspection-visitor.test.ts.snap index f94d6486f..b12f44e55 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-model-introspection-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-model-introspection-visitor.test.ts.snap @@ -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\\": {} +}" +`; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-model-introspection-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-model-introspection-visitor.test.ts index 42698d282..01b8b2188 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-model-introspection-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-model-introspection-visitor.test.ts @@ -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, @@ -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(); + }); }); \ No newline at end of file diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts index 6ec29dc33..8b5479002 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts @@ -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);