Skip to content

Commit 15c0ee9

Browse files
author
Dane Pilcher
authored
test: ensure order of targetNames and primaryKeyInfo.sortKeyFieldNames (#800)
1 parent 025644d commit 15c0ee9

File tree

2 files changed

+353
-0
lines changed

2 files changed

+353
-0
lines changed

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

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,318 @@ exports[`Primary Key Info tests should generate correct primary key info for mod
23472347
}"
23482348
`;
23492349

2350+
exports[`Primary Key Info tests should retain order of targetNames and primaryKeyInfo.sortKeyFieldNames 1`] = `
2351+
Object {
2352+
"enums": Object {},
2353+
"models": Object {
2354+
"Enthusiast": Object {
2355+
"attributes": Array [
2356+
Object {
2357+
"properties": Object {},
2358+
"type": "model",
2359+
},
2360+
Object {
2361+
"properties": Object {
2362+
"fields": Array [
2363+
"id",
2364+
],
2365+
},
2366+
"type": "key",
2367+
},
2368+
],
2369+
"fields": Object {
2370+
"createdAt": Object {
2371+
"attributes": Array [],
2372+
"isArray": false,
2373+
"isReadOnly": true,
2374+
"isRequired": false,
2375+
"name": "createdAt",
2376+
"type": "AWSDateTime",
2377+
},
2378+
"id": Object {
2379+
"attributes": Array [],
2380+
"isArray": false,
2381+
"isRequired": true,
2382+
"name": "id",
2383+
"type": "ID",
2384+
},
2385+
"likes": Object {
2386+
"association": Object {
2387+
"associatedWith": Array [
2388+
"enthusiast",
2389+
],
2390+
"connectionType": "HAS_MANY",
2391+
},
2392+
"attributes": Array [],
2393+
"isArray": true,
2394+
"isArrayNullable": true,
2395+
"isRequired": false,
2396+
"name": "likes",
2397+
"type": Object {
2398+
"model": "EnthusiastLikes",
2399+
},
2400+
},
2401+
"name": Object {
2402+
"attributes": Array [],
2403+
"isArray": false,
2404+
"isRequired": true,
2405+
"name": "name",
2406+
"type": "String",
2407+
},
2408+
"updatedAt": Object {
2409+
"attributes": Array [],
2410+
"isArray": false,
2411+
"isReadOnly": true,
2412+
"isRequired": false,
2413+
"name": "updatedAt",
2414+
"type": "AWSDateTime",
2415+
},
2416+
},
2417+
"name": "Enthusiast",
2418+
"pluralName": "Enthusiasts",
2419+
"primaryKeyInfo": Object {
2420+
"isCustomPrimaryKey": false,
2421+
"primaryKeyFieldName": "id",
2422+
"sortKeyFieldNames": Array [],
2423+
},
2424+
"syncable": true,
2425+
},
2426+
"EnthusiastLikes": Object {
2427+
"attributes": Array [
2428+
Object {
2429+
"properties": Object {},
2430+
"type": "model",
2431+
},
2432+
Object {
2433+
"properties": Object {
2434+
"fields": Array [
2435+
"enthusiastId",
2436+
],
2437+
"name": "byEnthusiast",
2438+
},
2439+
"type": "key",
2440+
},
2441+
Object {
2442+
"properties": Object {
2443+
"fields": Array [
2444+
"likeName",
2445+
"likesortKeyFieldOne",
2446+
"likesortKeyFieldTwo",
2447+
"likesortKeyFieldThree",
2448+
],
2449+
"name": "byLike",
2450+
},
2451+
"type": "key",
2452+
},
2453+
],
2454+
"fields": Object {
2455+
"createdAt": Object {
2456+
"attributes": Array [],
2457+
"isArray": false,
2458+
"isReadOnly": true,
2459+
"isRequired": false,
2460+
"name": "createdAt",
2461+
"type": "AWSDateTime",
2462+
},
2463+
"enthusiast": Object {
2464+
"association": Object {
2465+
"connectionType": "BELONGS_TO",
2466+
"targetNames": Array [
2467+
"enthusiastId",
2468+
],
2469+
},
2470+
"attributes": Array [],
2471+
"isArray": false,
2472+
"isRequired": true,
2473+
"name": "enthusiast",
2474+
"type": Object {
2475+
"model": "Enthusiast",
2476+
},
2477+
},
2478+
"enthusiastId": Object {
2479+
"attributes": Array [],
2480+
"isArray": false,
2481+
"isRequired": false,
2482+
"name": "enthusiastId",
2483+
"type": "ID",
2484+
},
2485+
"id": Object {
2486+
"attributes": Array [],
2487+
"isArray": false,
2488+
"isRequired": true,
2489+
"name": "id",
2490+
"type": "ID",
2491+
},
2492+
"like": Object {
2493+
"association": Object {
2494+
"connectionType": "BELONGS_TO",
2495+
"targetNames": Array [
2496+
"likeName",
2497+
"likesortKeyFieldOne",
2498+
"likesortKeyFieldTwo",
2499+
"likesortKeyFieldThree",
2500+
],
2501+
},
2502+
"attributes": Array [],
2503+
"isArray": false,
2504+
"isRequired": true,
2505+
"name": "like",
2506+
"type": Object {
2507+
"model": "Like",
2508+
},
2509+
},
2510+
"likeName": Object {
2511+
"attributes": Array [],
2512+
"isArray": false,
2513+
"isRequired": false,
2514+
"name": "likeName",
2515+
"type": "ID",
2516+
},
2517+
"likesortKeyFieldOne": Object {
2518+
"attributes": Array [],
2519+
"isArray": false,
2520+
"isRequired": false,
2521+
"name": "likesortKeyFieldOne",
2522+
"type": "String",
2523+
},
2524+
"likesortKeyFieldThree": Object {
2525+
"attributes": Array [],
2526+
"isArray": false,
2527+
"isRequired": false,
2528+
"name": "likesortKeyFieldThree",
2529+
"type": "String",
2530+
},
2531+
"likesortKeyFieldTwo": Object {
2532+
"attributes": Array [],
2533+
"isArray": false,
2534+
"isRequired": false,
2535+
"name": "likesortKeyFieldTwo",
2536+
"type": "String",
2537+
},
2538+
"updatedAt": Object {
2539+
"attributes": Array [],
2540+
"isArray": false,
2541+
"isReadOnly": true,
2542+
"isRequired": false,
2543+
"name": "updatedAt",
2544+
"type": "AWSDateTime",
2545+
},
2546+
},
2547+
"name": "EnthusiastLikes",
2548+
"pluralName": "EnthusiastLikes",
2549+
"primaryKeyInfo": Object {
2550+
"isCustomPrimaryKey": false,
2551+
"primaryKeyFieldName": "id",
2552+
"sortKeyFieldNames": Array [],
2553+
},
2554+
"syncable": true,
2555+
},
2556+
"Like": Object {
2557+
"attributes": Array [
2558+
Object {
2559+
"properties": Object {},
2560+
"type": "model",
2561+
},
2562+
Object {
2563+
"properties": Object {
2564+
"fields": Array [
2565+
"name",
2566+
"sortKeyFieldOne",
2567+
"sortKeyFieldTwo",
2568+
"sortKeyFieldThree",
2569+
],
2570+
},
2571+
"type": "key",
2572+
},
2573+
],
2574+
"fields": Object {
2575+
"createdAt": Object {
2576+
"attributes": Array [],
2577+
"isArray": false,
2578+
"isReadOnly": true,
2579+
"isRequired": false,
2580+
"name": "createdAt",
2581+
"type": "AWSDateTime",
2582+
},
2583+
"enthusiasts": Object {
2584+
"association": Object {
2585+
"associatedWith": Array [
2586+
"like",
2587+
],
2588+
"connectionType": "HAS_MANY",
2589+
},
2590+
"attributes": Array [],
2591+
"isArray": true,
2592+
"isArrayNullable": true,
2593+
"isRequired": false,
2594+
"name": "enthusiasts",
2595+
"type": Object {
2596+
"model": "EnthusiastLikes",
2597+
},
2598+
},
2599+
"name": Object {
2600+
"attributes": Array [],
2601+
"isArray": false,
2602+
"isRequired": true,
2603+
"name": "name",
2604+
"type": "String",
2605+
},
2606+
"sortKeyFieldOne": Object {
2607+
"attributes": Array [],
2608+
"isArray": false,
2609+
"isRequired": true,
2610+
"name": "sortKeyFieldOne",
2611+
"type": "String",
2612+
},
2613+
"sortKeyFieldThree": Object {
2614+
"attributes": Array [],
2615+
"isArray": false,
2616+
"isRequired": true,
2617+
"name": "sortKeyFieldThree",
2618+
"type": "String",
2619+
},
2620+
"sortKeyFieldTwo": Object {
2621+
"attributes": Array [],
2622+
"isArray": false,
2623+
"isRequired": true,
2624+
"name": "sortKeyFieldTwo",
2625+
"type": "String",
2626+
},
2627+
"updatedAt": Object {
2628+
"attributes": Array [],
2629+
"isArray": false,
2630+
"isReadOnly": true,
2631+
"isRequired": false,
2632+
"name": "updatedAt",
2633+
"type": "AWSDateTime",
2634+
},
2635+
"value": Object {
2636+
"attributes": Array [],
2637+
"isArray": false,
2638+
"isRequired": true,
2639+
"name": "value",
2640+
"type": "String",
2641+
},
2642+
},
2643+
"name": "Like",
2644+
"pluralName": "Likes",
2645+
"primaryKeyInfo": Object {
2646+
"isCustomPrimaryKey": true,
2647+
"primaryKeyFieldName": "name",
2648+
"sortKeyFieldNames": Array [
2649+
"sortKeyFieldOne",
2650+
"sortKeyFieldTwo",
2651+
"sortKeyFieldThree",
2652+
],
2653+
},
2654+
"syncable": true,
2655+
},
2656+
},
2657+
"nonModels": Object {},
2658+
"version": 1,
2659+
}
2660+
`;
2661+
23502662
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`] = `
23512663
"{
23522664
\\"version\\": 1,

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,47 @@ describe('Primary Key Info tests', () => {
210210
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
211211
expect(visitor.generate()).toMatchSnapshot();
212212
});
213+
214+
it('should retain order of targetNames and primaryKeyInfo.sortKeyFieldNames', () => {
215+
// Data Manager relies on this order matching
216+
const schema = /* GraphQL */ `
217+
type Enthusiast @model {
218+
id: ID! @primaryKey
219+
name: String!
220+
likes: [Like] @manyToMany(relationName: "EnthusiastLikes")
221+
}
222+
223+
type Like @model {
224+
sortKeyFieldThree: String!
225+
value: String!
226+
name: String! @primaryKey(sortKeyFields: ["sortKeyFieldOne", "sortKeyFieldTwo", "sortKeyFieldThree"])
227+
sortKeyFieldOne: String!
228+
enthusiasts: [Enthusiast] @manyToMany(relationName: "EnthusiastLikes")
229+
sortKeyFieldTwo: String!
230+
}
231+
`;
232+
const result = JSON.parse(getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true }).generate());
233+
const { models: { Like, EnthusiastLikes } } = result;
234+
expect(result).toMatchSnapshot();
235+
236+
// name
237+
expect(Like.primaryKeyInfo.primaryKeyFieldName).toEqual('name');
238+
expect(EnthusiastLikes.fields.like.association.targetNames[0]).toEqual('likeName');
239+
240+
241+
// sortKeyFieldOne
242+
expect(Like.primaryKeyInfo.sortKeyFieldNames[0]).toEqual('sortKeyFieldOne');
243+
expect(EnthusiastLikes.fields.like.association.targetNames[1]).toEqual('likesortKeyFieldOne');
244+
245+
// sortKeyFieldTwo
246+
expect(Like.primaryKeyInfo.sortKeyFieldNames[1]).toEqual('sortKeyFieldTwo');
247+
expect(EnthusiastLikes.fields.like.association.targetNames[2]).toEqual('likesortKeyFieldTwo');
248+
249+
// sortKeyFieldThree
250+
expect(Like.primaryKeyInfo.sortKeyFieldNames[2]).toEqual('sortKeyFieldThree');
251+
expect(EnthusiastLikes.fields.like.association.targetNames[3]).toEqual('likesortKeyFieldThree');
252+
});
253+
213254
});
214255

215256
describe('Primary key info within a belongsTo model tests', () => {

0 commit comments

Comments
 (0)