Skip to content

Commit 63183c5

Browse files
authored
fix(introspection): primary key info when pk is belongsTo field (#570)
* fix(introspection): primary key info when pk is belongsTo field * rename statement * update snapshot * fix lint error
1 parent a7a5bf0 commit 63183c5

File tree

3 files changed

+168
-8
lines changed

3 files changed

+168
-8
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-model-introspection-visitor.test.ts.snap

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,3 +2173,150 @@ exports[`Primary Key Info tests should generate correct primary key info for mod
21732173
\\"nonModels\\": {}
21742174
}"
21752175
`;
2176+
2177+
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`] = `
2178+
"{
2179+
\\"version\\": 1,
2180+
\\"models\\": {
2181+
\\"Post\\": {
2182+
\\"name\\": \\"Post\\",
2183+
\\"fields\\": {
2184+
\\"postId\\": {
2185+
\\"name\\": \\"postId\\",
2186+
\\"isArray\\": false,
2187+
\\"type\\": \\"ID\\",
2188+
\\"isRequired\\": true,
2189+
\\"attributes\\": []
2190+
},
2191+
\\"node\\": {
2192+
\\"name\\": \\"node\\",
2193+
\\"isArray\\": false,
2194+
\\"type\\": {
2195+
\\"model\\": \\"PostNode\\"
2196+
},
2197+
\\"isRequired\\": true,
2198+
\\"attributes\\": [],
2199+
\\"association\\": {
2200+
\\"connectionType\\": \\"BELONGS_TO\\",
2201+
\\"targetNames\\": [
2202+
\\"postId\\"
2203+
]
2204+
}
2205+
},
2206+
\\"title\\": {
2207+
\\"name\\": \\"title\\",
2208+
\\"isArray\\": false,
2209+
\\"type\\": \\"String\\",
2210+
\\"isRequired\\": true,
2211+
\\"attributes\\": []
2212+
},
2213+
\\"createdAt\\": {
2214+
\\"name\\": \\"createdAt\\",
2215+
\\"isArray\\": false,
2216+
\\"type\\": \\"AWSDateTime\\",
2217+
\\"isRequired\\": false,
2218+
\\"attributes\\": [],
2219+
\\"isReadOnly\\": true
2220+
},
2221+
\\"updatedAt\\": {
2222+
\\"name\\": \\"updatedAt\\",
2223+
\\"isArray\\": false,
2224+
\\"type\\": \\"AWSDateTime\\",
2225+
\\"isRequired\\": false,
2226+
\\"attributes\\": [],
2227+
\\"isReadOnly\\": true
2228+
}
2229+
},
2230+
\\"syncable\\": true,
2231+
\\"pluralName\\": \\"Posts\\",
2232+
\\"attributes\\": [
2233+
{
2234+
\\"type\\": \\"model\\",
2235+
\\"properties\\": {}
2236+
},
2237+
{
2238+
\\"type\\": \\"key\\",
2239+
\\"properties\\": {
2240+
\\"fields\\": [
2241+
\\"postId\\"
2242+
]
2243+
}
2244+
}
2245+
],
2246+
\\"primaryKeyInfo\\": {
2247+
\\"isCustomPrimaryKey\\": true,
2248+
\\"primaryKeyFieldName\\": \\"postId\\",
2249+
\\"sortKeyFieldNames\\": []
2250+
}
2251+
},
2252+
\\"PostNode\\": {
2253+
\\"name\\": \\"PostNode\\",
2254+
\\"fields\\": {
2255+
\\"id\\": {
2256+
\\"name\\": \\"id\\",
2257+
\\"isArray\\": false,
2258+
\\"type\\": \\"ID\\",
2259+
\\"isRequired\\": true,
2260+
\\"attributes\\": []
2261+
},
2262+
\\"post\\": {
2263+
\\"name\\": \\"post\\",
2264+
\\"isArray\\": false,
2265+
\\"type\\": {
2266+
\\"model\\": \\"Post\\"
2267+
},
2268+
\\"isRequired\\": true,
2269+
\\"attributes\\": [],
2270+
\\"association\\": {
2271+
\\"connectionType\\": \\"HAS_ONE\\",
2272+
\\"associatedWith\\": [
2273+
\\"node\\"
2274+
],
2275+
\\"targetNames\\": [
2276+
\\"postNodePostId\\"
2277+
]
2278+
}
2279+
},
2280+
\\"createdAt\\": {
2281+
\\"name\\": \\"createdAt\\",
2282+
\\"isArray\\": false,
2283+
\\"type\\": \\"AWSDateTime\\",
2284+
\\"isRequired\\": false,
2285+
\\"attributes\\": [],
2286+
\\"isReadOnly\\": true
2287+
},
2288+
\\"updatedAt\\": {
2289+
\\"name\\": \\"updatedAt\\",
2290+
\\"isArray\\": false,
2291+
\\"type\\": \\"AWSDateTime\\",
2292+
\\"isRequired\\": false,
2293+
\\"attributes\\": [],
2294+
\\"isReadOnly\\": true
2295+
},
2296+
\\"postNodePostId\\": {
2297+
\\"name\\": \\"postNodePostId\\",
2298+
\\"isArray\\": false,
2299+
\\"type\\": \\"ID\\",
2300+
\\"isRequired\\": true,
2301+
\\"attributes\\": []
2302+
}
2303+
},
2304+
\\"syncable\\": true,
2305+
\\"pluralName\\": \\"PostNodes\\",
2306+
\\"attributes\\": [
2307+
{
2308+
\\"type\\": \\"model\\",
2309+
\\"properties\\": {}
2310+
}
2311+
],
2312+
\\"primaryKeyInfo\\": {
2313+
\\"isCustomPrimaryKey\\": false,
2314+
\\"primaryKeyFieldName\\": \\"id\\",
2315+
\\"sortKeyFieldNames\\": []
2316+
}
2317+
}
2318+
},
2319+
\\"enums\\": {},
2320+
\\"nonModels\\": {}
2321+
}"
2322+
`;

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { buildSchema, GraphQLSchema, parse, visit } from 'graphql';
22
import { METADATA_SCALAR_MAP } from '../../scalars';
33
import { directives, scalars } from '../../scalars/supported-directives';
4-
import {
5-
CodeGenConnectionType,
6-
CodeGenFieldConnectionBelongsTo,
7-
CodeGenFieldConnectionHasMany,
8-
CodeGenFieldConnectionHasOne,
9-
} from '../../utils/process-connections';
104
import { AppSyncModelIntrospectionVisitor } from '../../visitors/appsync-model-introspection-visitor';
11-
import { CodeGenEnum, CodeGenField, CodeGenModel } from '../../visitors/appsync-visitor';
125

136
const defaultModelIntropectionVisitorSettings = {
147
isTimestampFieldsAdded: true,
@@ -217,4 +210,22 @@ describe('Primary Key Info tests', () => {
217210
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
218211
expect(visitor.generate()).toMatchSnapshot();
219212
});
213+
});
214+
215+
describe('Primary key info within a belongsTo model tests', () => {
216+
const schema = /* GraphQL */ `
217+
type Post @model {
218+
postId: ID! @primaryKey
219+
node: PostNode! @belongsTo(fields: ["postId"])
220+
title: String!
221+
}
222+
type PostNode @model {
223+
id: ID!
224+
post: Post! @hasOne
225+
}
226+
`;
227+
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', () => {
228+
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: false });
229+
expect(visitor.generate()).toMatchSnapshot();
230+
});
220231
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ export class AppSyncModelVisitor<
10281028
connectionInfo &&
10291029
connectionInfo.kind !== CodeGenConnectionType.HAS_MANY &&
10301030
connectionInfo.kind !== CodeGenConnectionType.HAS_ONE &&
1031-
connectionInfo.targetName !== 'id'
1031+
connectionInfo.targetName !== 'id' &&
1032+
!(this.config.target === 'introspection' &&
1033+
this.getFieldName(getModelPrimaryKeyComponentFields(model)[0]) === connectionInfo.targetName)
10321034
) {
10331035
// Need to remove the field that is targetName
10341036
removeFieldFromModel(model, connectionInfo.targetName);

0 commit comments

Comments
 (0)