Skip to content

Commit 7397ab9

Browse files
authored
fix: support key directive in model introspection schema gen (#628)
1 parent ffd7cf7 commit 7397ab9

File tree

4 files changed

+380
-8
lines changed

4 files changed

+380
-8
lines changed

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

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,3 +2320,327 @@ exports[`Primary key info within a belongsTo model tests should generate correct
23202320
\\"nonModels\\": {}
23212321
}"
23222322
`;
2323+
2324+
exports[`schemas with pk on a belongsTo fk works for v1 1`] = `
2325+
"{
2326+
\\"version\\": 1,
2327+
\\"models\\": {
2328+
\\"Blog\\": {
2329+
\\"name\\": \\"Blog\\",
2330+
\\"fields\\": {
2331+
\\"id\\": {
2332+
\\"name\\": \\"id\\",
2333+
\\"isArray\\": false,
2334+
\\"type\\": \\"ID\\",
2335+
\\"isRequired\\": true,
2336+
\\"attributes\\": []
2337+
},
2338+
\\"title\\": {
2339+
\\"name\\": \\"title\\",
2340+
\\"isArray\\": false,
2341+
\\"type\\": \\"String\\",
2342+
\\"isRequired\\": false,
2343+
\\"attributes\\": []
2344+
},
2345+
\\"posts\\": {
2346+
\\"name\\": \\"posts\\",
2347+
\\"isArray\\": true,
2348+
\\"type\\": {
2349+
\\"model\\": \\"Post\\"
2350+
},
2351+
\\"isRequired\\": false,
2352+
\\"attributes\\": [],
2353+
\\"isArrayNullable\\": true,
2354+
\\"association\\": {
2355+
\\"connectionType\\": \\"HAS_MANY\\",
2356+
\\"associatedWith\\": []
2357+
}
2358+
},
2359+
\\"createdAt\\": {
2360+
\\"name\\": \\"createdAt\\",
2361+
\\"isArray\\": false,
2362+
\\"type\\": \\"AWSDateTime\\",
2363+
\\"isRequired\\": false,
2364+
\\"attributes\\": [],
2365+
\\"isReadOnly\\": true
2366+
},
2367+
\\"updatedAt\\": {
2368+
\\"name\\": \\"updatedAt\\",
2369+
\\"isArray\\": false,
2370+
\\"type\\": \\"AWSDateTime\\",
2371+
\\"isRequired\\": false,
2372+
\\"attributes\\": [],
2373+
\\"isReadOnly\\": true
2374+
}
2375+
},
2376+
\\"syncable\\": true,
2377+
\\"pluralName\\": \\"Blogs\\",
2378+
\\"attributes\\": [
2379+
{
2380+
\\"type\\": \\"model\\",
2381+
\\"properties\\": {}
2382+
}
2383+
],
2384+
\\"primaryKeyInfo\\": {
2385+
\\"isCustomPrimaryKey\\": false,
2386+
\\"primaryKeyFieldName\\": \\"id\\",
2387+
\\"sortKeyFieldNames\\": []
2388+
}
2389+
},
2390+
\\"Post\\": {
2391+
\\"name\\": \\"Post\\",
2392+
\\"fields\\": {
2393+
\\"id\\": {
2394+
\\"name\\": \\"id\\",
2395+
\\"isArray\\": false,
2396+
\\"type\\": \\"ID\\",
2397+
\\"isRequired\\": true,
2398+
\\"attributes\\": []
2399+
},
2400+
\\"blogId\\": {
2401+
\\"name\\": \\"blogId\\",
2402+
\\"isArray\\": false,
2403+
\\"type\\": \\"ID\\",
2404+
\\"isRequired\\": true,
2405+
\\"attributes\\": []
2406+
},
2407+
\\"title\\": {
2408+
\\"name\\": \\"title\\",
2409+
\\"isArray\\": false,
2410+
\\"type\\": \\"String\\",
2411+
\\"isRequired\\": true,
2412+
\\"attributes\\": []
2413+
},
2414+
\\"description\\": {
2415+
\\"name\\": \\"description\\",
2416+
\\"isArray\\": false,
2417+
\\"type\\": \\"String\\",
2418+
\\"isRequired\\": true,
2419+
\\"attributes\\": []
2420+
},
2421+
\\"blog\\": {
2422+
\\"name\\": \\"blog\\",
2423+
\\"isArray\\": false,
2424+
\\"type\\": {
2425+
\\"model\\": \\"Blog\\"
2426+
},
2427+
\\"isRequired\\": false,
2428+
\\"attributes\\": [],
2429+
\\"association\\": {
2430+
\\"connectionType\\": \\"BELONGS_TO\\",
2431+
\\"targetNames\\": []
2432+
}
2433+
},
2434+
\\"createdAt\\": {
2435+
\\"name\\": \\"createdAt\\",
2436+
\\"isArray\\": false,
2437+
\\"type\\": \\"AWSDateTime\\",
2438+
\\"isRequired\\": false,
2439+
\\"attributes\\": [],
2440+
\\"isReadOnly\\": true
2441+
},
2442+
\\"updatedAt\\": {
2443+
\\"name\\": \\"updatedAt\\",
2444+
\\"isArray\\": false,
2445+
\\"type\\": \\"AWSDateTime\\",
2446+
\\"isRequired\\": false,
2447+
\\"attributes\\": [],
2448+
\\"isReadOnly\\": true
2449+
}
2450+
},
2451+
\\"syncable\\": true,
2452+
\\"pluralName\\": \\"Posts\\",
2453+
\\"attributes\\": [
2454+
{
2455+
\\"type\\": \\"model\\",
2456+
\\"properties\\": {}
2457+
},
2458+
{
2459+
\\"type\\": \\"key\\",
2460+
\\"properties\\": {
2461+
\\"fields\\": [
2462+
\\"blogId\\",
2463+
\\"title\\",
2464+
\\"description\\"
2465+
]
2466+
}
2467+
}
2468+
],
2469+
\\"primaryKeyInfo\\": {
2470+
\\"isCustomPrimaryKey\\": true,
2471+
\\"primaryKeyFieldName\\": \\"blogId\\",
2472+
\\"sortKeyFieldNames\\": [
2473+
\\"title\\",
2474+
\\"description\\"
2475+
]
2476+
}
2477+
}
2478+
},
2479+
\\"enums\\": {},
2480+
\\"nonModels\\": {}
2481+
}"
2482+
`;
2483+
2484+
exports[`schemas with pk on a belongsTo fk works for v2 1`] = `
2485+
"{
2486+
\\"version\\": 1,
2487+
\\"models\\": {
2488+
\\"Blog\\": {
2489+
\\"name\\": \\"Blog\\",
2490+
\\"fields\\": {
2491+
\\"id\\": {
2492+
\\"name\\": \\"id\\",
2493+
\\"isArray\\": false,
2494+
\\"type\\": \\"ID\\",
2495+
\\"isRequired\\": true,
2496+
\\"attributes\\": []
2497+
},
2498+
\\"title\\": {
2499+
\\"name\\": \\"title\\",
2500+
\\"isArray\\": false,
2501+
\\"type\\": \\"String\\",
2502+
\\"isRequired\\": false,
2503+
\\"attributes\\": []
2504+
},
2505+
\\"posts\\": {
2506+
\\"name\\": \\"posts\\",
2507+
\\"isArray\\": true,
2508+
\\"type\\": {
2509+
\\"model\\": \\"Post\\"
2510+
},
2511+
\\"isRequired\\": false,
2512+
\\"attributes\\": [],
2513+
\\"isArrayNullable\\": true,
2514+
\\"association\\": {
2515+
\\"connectionType\\": \\"HAS_MANY\\",
2516+
\\"associatedWith\\": [
2517+
\\"blog\\"
2518+
]
2519+
}
2520+
},
2521+
\\"createdAt\\": {
2522+
\\"name\\": \\"createdAt\\",
2523+
\\"isArray\\": false,
2524+
\\"type\\": \\"AWSDateTime\\",
2525+
\\"isRequired\\": false,
2526+
\\"attributes\\": [],
2527+
\\"isReadOnly\\": true
2528+
},
2529+
\\"updatedAt\\": {
2530+
\\"name\\": \\"updatedAt\\",
2531+
\\"isArray\\": false,
2532+
\\"type\\": \\"AWSDateTime\\",
2533+
\\"isRequired\\": false,
2534+
\\"attributes\\": [],
2535+
\\"isReadOnly\\": true
2536+
}
2537+
},
2538+
\\"syncable\\": true,
2539+
\\"pluralName\\": \\"Blogs\\",
2540+
\\"attributes\\": [
2541+
{
2542+
\\"type\\": \\"model\\",
2543+
\\"properties\\": {}
2544+
}
2545+
],
2546+
\\"primaryKeyInfo\\": {
2547+
\\"isCustomPrimaryKey\\": false,
2548+
\\"primaryKeyFieldName\\": \\"id\\",
2549+
\\"sortKeyFieldNames\\": []
2550+
}
2551+
},
2552+
\\"Post\\": {
2553+
\\"name\\": \\"Post\\",
2554+
\\"fields\\": {
2555+
\\"id\\": {
2556+
\\"name\\": \\"id\\",
2557+
\\"isArray\\": false,
2558+
\\"type\\": \\"ID\\",
2559+
\\"isRequired\\": true,
2560+
\\"attributes\\": []
2561+
},
2562+
\\"blogId\\": {
2563+
\\"name\\": \\"blogId\\",
2564+
\\"isArray\\": false,
2565+
\\"type\\": \\"ID\\",
2566+
\\"isRequired\\": true,
2567+
\\"attributes\\": []
2568+
},
2569+
\\"title\\": {
2570+
\\"name\\": \\"title\\",
2571+
\\"isArray\\": false,
2572+
\\"type\\": \\"String\\",
2573+
\\"isRequired\\": true,
2574+
\\"attributes\\": []
2575+
},
2576+
\\"description\\": {
2577+
\\"name\\": \\"description\\",
2578+
\\"isArray\\": false,
2579+
\\"type\\": \\"String\\",
2580+
\\"isRequired\\": true,
2581+
\\"attributes\\": []
2582+
},
2583+
\\"blog\\": {
2584+
\\"name\\": \\"blog\\",
2585+
\\"isArray\\": false,
2586+
\\"type\\": {
2587+
\\"model\\": \\"Blog\\"
2588+
},
2589+
\\"isRequired\\": false,
2590+
\\"attributes\\": [],
2591+
\\"association\\": {
2592+
\\"connectionType\\": \\"BELONGS_TO\\",
2593+
\\"targetNames\\": [
2594+
\\"blogId\\"
2595+
]
2596+
}
2597+
},
2598+
\\"createdAt\\": {
2599+
\\"name\\": \\"createdAt\\",
2600+
\\"isArray\\": false,
2601+
\\"type\\": \\"AWSDateTime\\",
2602+
\\"isRequired\\": false,
2603+
\\"attributes\\": [],
2604+
\\"isReadOnly\\": true
2605+
},
2606+
\\"updatedAt\\": {
2607+
\\"name\\": \\"updatedAt\\",
2608+
\\"isArray\\": false,
2609+
\\"type\\": \\"AWSDateTime\\",
2610+
\\"isRequired\\": false,
2611+
\\"attributes\\": [],
2612+
\\"isReadOnly\\": true
2613+
}
2614+
},
2615+
\\"syncable\\": true,
2616+
\\"pluralName\\": \\"Posts\\",
2617+
\\"attributes\\": [
2618+
{
2619+
\\"type\\": \\"model\\",
2620+
\\"properties\\": {}
2621+
},
2622+
{
2623+
\\"type\\": \\"key\\",
2624+
\\"properties\\": {
2625+
\\"fields\\": [
2626+
\\"blogId\\",
2627+
\\"title\\",
2628+
\\"description\\"
2629+
]
2630+
}
2631+
}
2632+
],
2633+
\\"primaryKeyInfo\\": {
2634+
\\"isCustomPrimaryKey\\": true,
2635+
\\"primaryKeyFieldName\\": \\"blogId\\",
2636+
\\"sortKeyFieldNames\\": [
2637+
\\"title\\",
2638+
\\"description\\"
2639+
]
2640+
}
2641+
}
2642+
},
2643+
\\"enums\\": {},
2644+
\\"nonModels\\": {}
2645+
}"
2646+
`;

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,48 @@ describe('Primary key info within a belongsTo model tests', () => {
228228
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: false });
229229
expect(visitor.generate()).toMatchSnapshot();
230230
});
231-
});
231+
});
232+
233+
describe('schemas with pk on a belongsTo fk', () => {
234+
it('works for v1', () => {
235+
expect(getVisitor(/* GraphQL */ `
236+
type Blog @model {
237+
id: ID!
238+
title: String
239+
posts: [Post] @connection(fields: ["id"])
240+
}
241+
242+
type Post @model @key(fields: ["blogId", "title", "description"]) {
243+
id: ID!
244+
blogId: ID!
245+
title: String!
246+
description: String!
247+
blog: Blog @connection(fields: ["blogId"])
248+
}
249+
`, {
250+
transformerVersion: 1,
251+
usePipelinedTransformer: false,
252+
}).generate()).toMatchSnapshot();
253+
});
254+
255+
it('works for v2', () => {
256+
expect(getVisitor(/* GraphQL */ `
257+
type Blog @model {
258+
id: ID!
259+
title: String
260+
posts: [Post] @hasMany(fields: ["id"])
261+
}
262+
263+
type Post @model {
264+
id: ID!
265+
blogId: ID! @primaryKey(sortKeyFields: ["title", "description"])
266+
title: String!
267+
description: String!
268+
blog: Blog @belongsTo(fields: ["blogId"])
269+
}
270+
`, {
271+
transformerVersion: 2,
272+
usePipelinedTransformer: true,
273+
}).generate()).toMatchSnapshot();
274+
});
275+
});

0 commit comments

Comments
 (0)