-
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
Open
Tedsterh
wants to merge
12
commits into
multimeric:master
Choose a base branch
from
Tedsterh:count-field-definition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8a9406
Started count directive on field
Tedsterh 3f2a406
Added filter fields on type
Tedsterh 52db825
Added resolver
Tedsterh 8c18671
Changed index check
Tedsterh 08fce83
Removed handler.zip and fixed up requested changes, changed the way t…
Tedsterh 295d12f
Used the correct resolver mapping
Tedsterh d4f353f
Reverted target
Tedsterh 3548dc0
Removed handler.zip
Tedsterh b6cfd6e
Started tests, added test connection schema
Tedsterh b88af01
Added test for field validation
Tedsterh e3a0378
Started index with new function in handler as well as fields on direc…
Tedsterh 5c8d8a4
Updated tests
Tedsterh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added resolver
- Loading branch information
commit 52db825d3daabb4c0499dad06e072a3ee991ef2b
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { | ||
DirectiveWrapper, | ||
IAM_AUTH_ROLE_PARAMETER, | ||
IAM_UNAUTH_ROLE_PARAMETER, | ||
MappingTemplate, | ||
TransformerNestedStack, | ||
TransformerPluginBase, | ||
} from "@aws-amplify/graphql-transformer-core"; | ||
|
@@ -10,8 +13,10 @@ import { | |
TransformerTransformSchemaStepContextProvider, | ||
} from "@aws-amplify/graphql-transformer-interfaces"; | ||
import * as appsync from "@aws-cdk/aws-appsync"; | ||
import { AuthorizationType } from "@aws-cdk/aws-appsync"; | ||
import * as iam from "@aws-cdk/aws-iam"; | ||
import * as lambda from "@aws-cdk/aws-lambda"; | ||
import * as cdk from "@aws-cdk/core"; | ||
import { | ||
DirectiveNode, | ||
FieldDefinitionNode, | ||
|
@@ -21,12 +26,21 @@ import { | |
NamedTypeNode, | ||
NonNullTypeNode, | ||
ObjectTypeDefinitionNode, | ||
StringValueNode, | ||
TypeNode, | ||
} from "graphql"; | ||
import { | ||
compoundExpression, | ||
Expression, | ||
obj, | ||
printBlock, | ||
qref, | ||
} from "graphql-mapping-template"; | ||
import { | ||
makeField, | ||
makeInputValueDefinition, | ||
makeNamedType, | ||
ResolverResourceIDs, | ||
toCamelCase, | ||
toPascalCase, | ||
} from "graphql-transformer-common"; | ||
|
@@ -43,8 +57,8 @@ export type FieldCountDirectiveConfiguration = { | |
directive: DirectiveNode; | ||
fields: string[]; | ||
fieldNodes: FieldDefinitionNode[]; | ||
relatedType: ObjectTypeDefinitionNode; | ||
relatedTypeIndex: FieldDefinitionNode[]; | ||
resolverTypeName: ObjectTypeDefinitionNode; | ||
resolverFieldName: string; | ||
countFields: string[]; | ||
}; | ||
|
||
|
@@ -86,6 +100,8 @@ export default class CountTransformer | |
directiveName, | ||
object: parent as ObjectTypeDefinitionNode, | ||
field: field, | ||
resolverTypeName: parent, | ||
resolverFieldName: field.name.value, | ||
directive, | ||
}) as FieldCountDirectiveConfiguration; | ||
|
||
|
@@ -144,6 +160,8 @@ export default class CountTransformer | |
}; | ||
|
||
generateResolvers = (ctx: TransformerContextProvider) => { | ||
const createdResources = new Map<string, any>(); | ||
|
||
// Path on the local filesystem to the handler zip file | ||
const HANDLER_LOCAL_PATH = path.join(__dirname, "handler.zip"); | ||
const stack: TransformerNestedStack = ctx.stackManager.createStack( | ||
|
@@ -183,6 +201,107 @@ export default class CountTransformer | |
stack | ||
); | ||
|
||
for (const config of this.fields) { | ||
const { | ||
countFields, | ||
field, | ||
fields, | ||
object, | ||
resolverTypeName, | ||
resolverFieldName, | ||
} = config; | ||
|
||
const localFields = fields.length > 0 ? fields : countFields; | ||
|
||
// Find the table we want to scan | ||
const tableDataSource = ctx.dataSources.get( | ||
resolverTypeName | ||
) as appsync.DynamoDbDataSource; | ||
const table = tableDataSource.ds | ||
.dynamoDbConfig as appsync.CfnDataSource.DynamoDBConfigProperty; | ||
|
||
const dataSource = ctx.api.host.getDataSource( | ||
`${resolverTypeName.name.value}Table` | ||
); | ||
|
||
// Allow the lambda to access this table | ||
funcRole.addToPolicy( | ||
new iam.PolicyStatement({ | ||
actions: ["dynamodb:Scan"], | ||
effect: iam.Effect.ALLOW, | ||
resources: [ | ||
`arn:aws:dynamodb:${table.awsRegion}:${stack.account}:table/${table.tableName}`, | ||
], | ||
}) | ||
); | ||
|
||
// Create the GraphQL resolvers. | ||
const resolverId = ResolverResourceIDs.ResolverResourceID( | ||
config.resolverTypeName.name.value, | ||
config.resolverFieldName | ||
); | ||
let resolver = createdResources.get(resolverId); | ||
|
||
const requestTemplate: Array<Expression> = [ | ||
qref(`$ctx.stash.put("typeName", "${config.resolverTypeName}")`), | ||
qref(`$ctx.stash.put("fieldName", "${config.resolverFieldName}")`), | ||
]; | ||
|
||
const authModes = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you import this code from the |
||
ctx.authConfig.defaultAuthentication, | ||
...(ctx.authConfig.additionalAuthenticationProviders || []), | ||
].map((mode) => mode?.authenticationType); | ||
if (authModes.includes(AuthorizationType.IAM)) { | ||
const authRoleParameter = ( | ||
ctx.stackManager.getParameter( | ||
IAM_AUTH_ROLE_PARAMETER | ||
) as cdk.CfnParameter | ||
).valueAsString; | ||
const unauthRoleParameter = ( | ||
ctx.stackManager.getParameter( | ||
IAM_UNAUTH_ROLE_PARAMETER | ||
) as cdk.CfnParameter | ||
).valueAsString; | ||
requestTemplate.push( | ||
qref( | ||
`$ctx.stash.put("authRole", "arn:aws:sts::${ | ||
cdk.Stack.of(ctx.stackManager.rootStack).account | ||
}:assumed-role/${authRoleParameter}/CognitoIdentityCredentials")` | ||
), | ||
qref( | ||
`$ctx.stash.put("unauthRole", "arn:aws:sts::${ | ||
cdk.Stack.of(ctx.stackManager.rootStack).account | ||
}:assumed-role/${unauthRoleParameter}/CognitoIdentityCredentials")` | ||
) | ||
); | ||
} | ||
requestTemplate.push(obj({})); | ||
|
||
if (resolver === undefined) { | ||
// TODO: update function to use resolver manager | ||
resolver = ctx.api.host.addResolver( | ||
config.resolverTypeName.name.value, | ||
config.resolverFieldName, | ||
MappingTemplate.inlineTemplateFromString( | ||
printBlock("Stash resolver specific context.")( | ||
compoundExpression(requestTemplate) | ||
) | ||
), | ||
MappingTemplate.s3MappingTemplateFromString( | ||
"$util.toJson($ctx.prev.result)", | ||
`${config.resolverTypeName}.${config.resolverFieldName}.res.vtl` | ||
), | ||
undefined, | ||
undefined, | ||
[], | ||
stack | ||
); | ||
createdResources.set(resolverId, resolver); | ||
} | ||
|
||
resolver.pipelineConfig.functions.push(func); | ||
} | ||
|
||
for (const model of this.models) { | ||
// Find the table we want to scan | ||
const tableDataSource = ctx.dataSources.get( | ||
|
@@ -239,6 +358,14 @@ $util.toJson({ | |
}; | ||
} | ||
|
||
function getIndexName(directive: DirectiveNode): string | undefined { | ||
for (const argument of directive.arguments!) { | ||
if (argument.name.value === "name") { | ||
return (argument.value as StringValueNode).value; | ||
} | ||
} | ||
} | ||
|
||
export function ensureHasCountField( | ||
config: FieldCountDirectiveConfiguration, | ||
ctx: TransformerContextProvider | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.