Skip to content

Commit 527e248

Browse files
release support for index queryField (#317)
* fix: include queryField when mapping index to key (#313) * fix: update codegen models to use v2 schema shapes (#316) Co-authored-by: Edward Foyle <[email protected]>
1 parent f3dd474 commit 527e248

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

packages/amplify-codegen-e2e-tests/schemas/modelgen/model_gen_schema_with_aws_scalars.graphql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ type User @model {
3131
type Post @model {
3232
title: String!
3333
content: String
34-
comments: [Comment] @connection(name: "PostComment")
34+
comments: [Comment] @hasMany
3535
}
3636

3737
type Comment @model {
3838
comment: String!
39-
post: Post @connection(name: "PostComment")
39+
post: Post @belongsTo
4040
}
4141

4242
# 1:1 Connection
4343

4444
type Person @model {
4545
id: ID!
4646
name: String!
47-
license: License @connection
47+
license: License @hasOne
4848
}
4949

5050
type License @model {
5151
id: ID!
5252
number: String!
53-
belongsTo: Person @connection
53+
belongsTo: Person @belongsTo
5454
}

packages/appsync-modelgen-plugin/src/__tests__/utils/process-index.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,49 @@ describe('processIndex', () => {
219219
processIndex(model);
220220
expect(model.directives.length).toBe(3);
221221
});
222+
223+
it('adds index with queryFields', () => {
224+
const model: CodeGenModel = {
225+
directives: [
226+
{
227+
name: 'model',
228+
arguments: {},
229+
},
230+
],
231+
name: 'testModel',
232+
type: 'model',
233+
fields: [
234+
{
235+
type: 'field',
236+
isList: false,
237+
isNullable: true,
238+
name: 'testField',
239+
directives: [
240+
{
241+
name: 'index',
242+
arguments: {
243+
queryField: 'myQuery',
244+
name: 'byItem',
245+
},
246+
},
247+
],
248+
},
249+
],
250+
};
251+
processIndex(model);
252+
expect(model.directives).toEqual([
253+
{
254+
name: 'model',
255+
arguments: {},
256+
},
257+
{
258+
name: 'key',
259+
arguments: {
260+
name: 'byItem',
261+
queryField: 'myQuery',
262+
fields: ['testField'],
263+
},
264+
},
265+
]);
266+
});
222267
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,26 @@ describe('AppSyncModelVisitor', () => {
411411
},
412412
});
413413
});
414+
415+
it('processes index with queryField', () => {
416+
const schema = /* GraphQL */ `
417+
type Project @model {
418+
id: ID!
419+
name: String @index(name: "nameIndex", queryField: "myQuery")
420+
}
421+
`;
422+
const visitor = createAndGenerateVisitor(schema, true);
423+
visitor.generate();
424+
const projectKeyDirective = visitor.models.Project.directives.find(directive => directive.name === 'key');
425+
expect(projectKeyDirective).toEqual({
426+
name: 'key',
427+
arguments: {
428+
name: 'nameIndex',
429+
queryField: 'myQuery',
430+
fields: ['name'],
431+
},
432+
});
433+
});
414434
});
415435

416436
describe('auth directive', () => {

packages/appsync-modelgen-plugin/src/utils/process-index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const processIndex = (model: CodeGenModel) => {
1818
name: 'key',
1919
arguments: {
2020
name: directive.arguments.name,
21+
queryField: directive.arguments.queryField,
2122
fields: [fieldName].concat((directive.arguments.sortKeyFields as string[]) ?? []),
2223
},
2324
}));

0 commit comments

Comments
 (0)