-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Count field definition #11
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- In general, if you have copied code here from the amplify codebase, try to import it wherever possible, instead of copying it
- Don't commit
handler.zip
- Please add some tests that assert the lambda behaviour and the CDK stack function correctly.
handler/tsconfig.json
Outdated
@@ -11,7 +11,7 @@ | |||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ | |||
|
|||
/* Language and Environment */ | |||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | |||
"target": "es2019", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why increase the target?
index.ts
Outdated
|
||
export default class CountTransformer | ||
extends TransformerPluginBase | ||
implements TransformerPluginProvider | ||
{ | ||
models: ObjectTypeDefinitionNode[]; | ||
fields: FieldCountDirectiveConfiguration[]; | ||
|
||
// directive @fieldCount(type: CountType!) on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
index.ts
Outdated
|
||
export default class CountTransformer | ||
extends TransformerPluginBase | ||
implements TransformerPluginProvider | ||
{ | ||
models: ObjectTypeDefinitionNode[]; | ||
fields: FieldCountDirectiveConfiguration[]; | ||
|
||
// directive @fieldCount(type: CountType!) on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
// Allow the lambda to access this table | ||
funcRole.addToPolicy( | ||
new iam.PolicyStatement({ | ||
actions: ["dynamodb:Scan"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, which bit in the policy needed to be query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well unless I'm mistaken, we need the lambda to be able to query the index. We could scan, but that wouldn't be efficient. So add dynamodb:Query
.
index.ts
Outdated
qref(`$ctx.stash.put("fieldName", "${config.resolverFieldName}")`), | ||
]; | ||
|
||
const authModes = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you import this code from the AuthTransformer
module? I assume it's copied from there.
…he count directive looks up the current object
The zip file is still in the pull request. |
Also, I believe more changes will be needed in the lambda resolver, to handle an index. We will want to |
Oh, I just realised what you're doing. You're not trying to add a count for secondary indexes like I thought (?). So the query stuff is not relevant. |
Yes sorry, I'm just creating a count field for list connections, I thought I had remove the .zip so i'll go back and do that now. |
I think we can get the name of the index generated by Or we could force an |
Hmm I have to admit I've never actually used |
You can use it with a custom index or let it generate one, both do the same thing effectively I'm just not sure how we could easy access the generated one from our directive. type Post @model {
id: ID!
title: String!
comments: [Comment] @hasMany
}
type Comment @model {
id: ID!
content: String!
} Or with a custom index: type Post @model {
id: ID!
title: String!
comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"])
}
type Comment @model {
id: ID!
postID: ID! @index(name: "byPost", sortKeyFields: ["content"])
content: String!
} We could then require them to pass the index name which in this case is |
So at a high level, you want to be able to For |
Yes that's what I'm thinking, the hope is because we are querying it directly too I can use a filter on it, to filter out certain comments from a total in this case. I'll look at using an index then and checkout their resolvers for the query, I've not used dynamo queries before. Should I make a new handler or just edit the current one to accept an optional index name? |
Definitely use the same handler. Most of the code will remain the same and we don't want to duplicate everything. You can pass the info like the index name in through a new key in the event (alongside |
Yeah so dynamo's |
This is how they do it for the posts one they have a #if( !$util.isNull($ctx.args. postID) )
#set( $modelQueryExpression.expression = "#postID = : postID" )
#set( $modelQueryExpression.expressionNames = {
"#postID": "postID"
} )
#set( $modelQueryExpression.expressionValues = {
": postID": {
"S": "$ctx.args. postID"
}
} ) do you know what's in the context, is there a way to access the primary key of the model that the field is in? I have found out how to use the indexes we simply pass in the index name |
Okay, so you'll have to write a resolver for the |
According to the docs we should be able to do something like |
What I would suggest is actually checking for the existence of the |
I have added a new direct called
fieldCount
that uses anhasMany
ormanyToMany
directive to access the other table instead, it also has a field argument like these connections so you can pass a field for it to return on, if none are provided it generates one similar to thehasOne
directive.I am not too sure how the resolvers work so not sure if it maps to the field properly or not just yet, any help and tips would be amazing!