Skip to content

Commit

Permalink
fix: support key directive in model introspection schema gen (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
alharris-at authored Jul 14, 2023
1 parent ffd7cf7 commit 7397ab9
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2320,3 +2320,327 @@ exports[`Primary key info within a belongsTo model tests should generate correct
\\"nonModels\\": {}
}"
`;

exports[`schemas with pk on a belongsTo fk works for v1 1`] = `
"{
\\"version\\": 1,
\\"models\\": {
\\"Blog\\": {
\\"name\\": \\"Blog\\",
\\"fields\\": {
\\"id\\": {
\\"name\\": \\"id\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"title\\": {
\\"name\\": \\"title\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
},
\\"posts\\": {
\\"name\\": \\"posts\\",
\\"isArray\\": true,
\\"type\\": {
\\"model\\": \\"Post\\"
},
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isArrayNullable\\": true,
\\"association\\": {
\\"connectionType\\": \\"HAS_MANY\\",
\\"associatedWith\\": []
}
},
\\"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\\": \\"Blogs\\",
\\"attributes\\": [
{
\\"type\\": \\"model\\",
\\"properties\\": {}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": false,
\\"primaryKeyFieldName\\": \\"id\\",
\\"sortKeyFieldNames\\": []
}
},
\\"Post\\": {
\\"name\\": \\"Post\\",
\\"fields\\": {
\\"id\\": {
\\"name\\": \\"id\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"blogId\\": {
\\"name\\": \\"blogId\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"title\\": {
\\"name\\": \\"title\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"description\\": {
\\"name\\": \\"description\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"blog\\": {
\\"name\\": \\"blog\\",
\\"isArray\\": false,
\\"type\\": {
\\"model\\": \\"Blog\\"
},
\\"isRequired\\": false,
\\"attributes\\": [],
\\"association\\": {
\\"connectionType\\": \\"BELONGS_TO\\",
\\"targetNames\\": []
}
},
\\"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\\": [
\\"blogId\\",
\\"title\\",
\\"description\\"
]
}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": true,
\\"primaryKeyFieldName\\": \\"blogId\\",
\\"sortKeyFieldNames\\": [
\\"title\\",
\\"description\\"
]
}
}
},
\\"enums\\": {},
\\"nonModels\\": {}
}"
`;

exports[`schemas with pk on a belongsTo fk works for v2 1`] = `
"{
\\"version\\": 1,
\\"models\\": {
\\"Blog\\": {
\\"name\\": \\"Blog\\",
\\"fields\\": {
\\"id\\": {
\\"name\\": \\"id\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"title\\": {
\\"name\\": \\"title\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
},
\\"posts\\": {
\\"name\\": \\"posts\\",
\\"isArray\\": true,
\\"type\\": {
\\"model\\": \\"Post\\"
},
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isArrayNullable\\": true,
\\"association\\": {
\\"connectionType\\": \\"HAS_MANY\\",
\\"associatedWith\\": [
\\"blog\\"
]
}
},
\\"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\\": \\"Blogs\\",
\\"attributes\\": [
{
\\"type\\": \\"model\\",
\\"properties\\": {}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": false,
\\"primaryKeyFieldName\\": \\"id\\",
\\"sortKeyFieldNames\\": []
}
},
\\"Post\\": {
\\"name\\": \\"Post\\",
\\"fields\\": {
\\"id\\": {
\\"name\\": \\"id\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"blogId\\": {
\\"name\\": \\"blogId\\",
\\"isArray\\": false,
\\"type\\": \\"ID\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"title\\": {
\\"name\\": \\"title\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"description\\": {
\\"name\\": \\"description\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": true,
\\"attributes\\": []
},
\\"blog\\": {
\\"name\\": \\"blog\\",
\\"isArray\\": false,
\\"type\\": {
\\"model\\": \\"Blog\\"
},
\\"isRequired\\": false,
\\"attributes\\": [],
\\"association\\": {
\\"connectionType\\": \\"BELONGS_TO\\",
\\"targetNames\\": [
\\"blogId\\"
]
}
},
\\"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\\": [
\\"blogId\\",
\\"title\\",
\\"description\\"
]
}
}
],
\\"primaryKeyInfo\\": {
\\"isCustomPrimaryKey\\": true,
\\"primaryKeyFieldName\\": \\"blogId\\",
\\"sortKeyFieldNames\\": [
\\"title\\",
\\"description\\"
]
}
}
},
\\"enums\\": {},
\\"nonModels\\": {}
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,48 @@ describe('Primary key info within a belongsTo model tests', () => {
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: false });
expect(visitor.generate()).toMatchSnapshot();
});
});
});

describe('schemas with pk on a belongsTo fk', () => {
it('works for v1', () => {
expect(getVisitor(/* GraphQL */ `
type Blog @model {
id: ID!
title: String
posts: [Post] @connection(fields: ["id"])
}
type Post @model @key(fields: ["blogId", "title", "description"]) {
id: ID!
blogId: ID!
title: String!
description: String!
blog: Blog @connection(fields: ["blogId"])
}
`, {
transformerVersion: 1,
usePipelinedTransformer: false,
}).generate()).toMatchSnapshot();
});

it('works for v2', () => {
expect(getVisitor(/* GraphQL */ `
type Blog @model {
id: ID!
title: String
posts: [Post] @hasMany(fields: ["id"])
}
type Post @model {
id: ID!
blogId: ID! @primaryKey(sortKeyFields: ["title", "description"])
title: String!
description: String!
blog: Blog @belongsTo(fields: ["blogId"])
}
`, {
transformerVersion: 2,
usePipelinedTransformer: true,
}).generate()).toMatchSnapshot();
});
});
Loading

0 comments on commit 7397ab9

Please sign in to comment.