Reputation: 66
I'm currently developing a GraphQL API using AWS CDK and facing a challenge with configuring a query that utilizes the "begins with" condition for a secondary key (SK). The problem is that the query consistently returns null. For this particular query, only the primary key (PK) is mandatory. However, when I omit the SK entirely, the query still returns null. On the other hand, if I include both the complete PK and SK, the query functions correctly and produces the expected result.
Here’s an example of what I’m aiming for:
query GetData {
getData(input: {pk: "somePK", sk: {beginsWith: "som"}}) {
pk,
sk,
data
}
}
I've set up Query.getData.req.vtl and Query.getData.res.vtl resolvers to handle this. Here's the configuration for the request resolver
#set($GetRequest = {
"version": "2018-05-29",
"operation": "Query"
})
#if(!$ctx.args.input.pk)
#set( $errorMessage = "Primary key is required" )
$util.error($errorMessage, "Error")
#end
#set($expression = "pk = :pk")
#set($expressionValues = {
":pk": {"S": $ctx.args.input.pk}
})
#if($ctx.args.input.sk && $ctx.args.input.sk.beginsWith)
#set( $errorMessage = "$expressionValues" )
#set($expression = "$expression AND begins_with(sk, :sk)")
$util.qr($expressionValues.put(":sk", {"S": $ctx.args.input.sk.beginsWith}))
#end
#set($query = {
"expression": $expression,
"expressionValues": $expressionValues
})
$util.qr($GetRequest.put("query", $query))
$util.toJson($GetRequest)
And here’s what I have for the response resolver:
## [Start] Get Response template. **
#if( $ctx.error )
$util.error($ctx.error.message, $ctx.error.type)
#end
#if( !$ctx.result.items.isEmpty() && $ctx.result.scannedCount == 1 )
$util.toJson($ctx.result.items[0])
#else
#if( $ctx.result.items.isEmpty() && $ctx.result.scannedCount == 1 )
$util.unauthorized()
#end
$util.toJson(null)
#end
## [End] Get Response template. **
Currently, the query returns null unless the full secondary key (sk) is provided. It also returns null when only the primary key (pk) is included.
Here is the part of my schema that creates the query:
type SP {
pk: String!
sk: String!
type: String!
data: String!
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
}
input SKConditionInput {
beginsWith: String
}
input GetDataInput {
pk: String!
sk: SKConditionInput
type: String
}
type Query {
getData(input: GetDataInput!): SP
getSP(pk: String!, sk: String!): SP
listSPS(
pk: String
sk: ModelStringKeyConditionInput
filter: ModelSPFilterInput
limit: Int
nextToken: String
sortDirection: ModelSortDirection
): ModelSPConnection
sPSByType(
type: String!
sortDirection: ModelSortDirection
filter: ModelSPFilterInput
limit: Int
nextToken: String
): ModelSPConnection
}
...
Any help would be appreciated.
Thank you
Upvotes: 0
Views: 32