Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(introspection): primary key info when pk is belongsTo field #570

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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