Using dynamodb document client on step function workflow - aws-lambda

Am trying to get results for a getItem action as "normal" json instead of the DynamoDB json format on my step function workflow.
If i use:
"Resource": "arn:aws:states:::dynamodb:getItem"
On my ASL file i get the DynamoDB json format, for example:
{
"entity_id": {
"S": "d0e96ad0-4f83-4aa7-bcaf-2cf02c6216cb"
}
}
And i need:
{
"entity_id": "d0e96ad0-4f83-4aa7-bcaf-2cf02c6216cb"
}
I could create lambdas to interact with dynamo and use de sdk documentClient but it will be really convenient to be able to do that directly with the ASL template.
So far i tried something like:
"Resource": "arn:aws:states:::aws-sdk:dynamodb:documentClient:getItem"
But is not valid for the template. Also did a bit of research into intrinsic functions with no success, i could also do some mapping and use ResultSelector but at that point i guess is better to use a lambda

Stepfunctions uses Java SDK V2 to interact with DynamoDB and unfortunately can only return DynamoDB JSON. You would need to use a utility function on the client side to parse the DDB JSON or as you mentioned use a document client in Lambda.

Related

AWS AppSync - formatting hardcoded JSON data in response mapping template

I need a GraphQL query returning hardcoded informations as JSON response.
In the AppSync GraphQL schema, I added the following query:
type Query {
getHealthCheck: AWSJSON
}
My response mapping template where the values are hardcoded is the following:
$util.toJson({"version": "0.1.0"})
However, as the response of the query, I get a string instead of a proper JSON, i.e.:
{
"data": {
"getHealthCheck": "{\"version\":\"0.1.0\"}"
}
}
How can I modify the response mapping template to get a proper JSON? I tried several utils but I'm a bit lost with the data structures in VTL.
I don't think this is possible.
If you think about it, it goes against the idea of even having a GraphQL schema to then return essential arbritrary JSON.
I believe that AWSJSON will parse stringified JSON for inputs, but serialize to stringified JSON for outputs.
There's an answer to an other question that might give some ideas of how to work around this. Other than that, it seems your client will need to parse the JSON.

How to create a custom, hardcoded health check GraphQL query with AWS Appsync?

I'm new to AWS Appsync and to GraphQL.
Previously, I used to create REST APIs in Python. I was always creating a GET /health-check endpoint, sending back, for example and among many other info, the API version number, easily parsed from the project descriptor pyproject.toml file.
That helped me massively to maintain APIs: with a single GET query in my browser, I was always able to instantly get if branch/version it was, the status of other services, etc. .
I want to do something similar with AWS Appsync / GraphQL and my IaC tool (Pulumi).
Since I'm using IaC tool Pulumi in Python, I could still easily get the info I need and inject them in any resolver response template.
But if I create a resolver, should I create a corresponding health-check query itself in the GraphQL schema? When creating a resolver with a hardcoded JSON response, should it be associated with a GraphQL query in the schema, and if yes, how should that query in the schema look like?
I finally found a way, but it's a very ugly workaround since AppSync VTL resolvers have a lot of limitations and since I'm using Pulumi as a Iac tool which also doesn't accept all arguments when creating a resolver - for example for GetItem operation it needs an id key in the request template.
I'm posting the workaround here anyway if it can be of any help to someone.
GraphQL schema:
schema {
query: Query
}
type Query {
getHealthCheck: String
}
AppSync getHealthCheck resolver request template:
{
"version": "2018-05-29",
"operation": "GetItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson(""),
},
}
AppSync getHealthCheck resolver response template example:
"'version':'0.1', 'branch':'staging', 'commitId':'abc123'"
So in a IaC tool like Pulumi using Python, one could build the response template like that:
"""
"'version':'{}', 'branch':'{}', 'commitId':'{}'"
""".format(
version,
os.environ.get("GITHUB_REF_NAME", "unknown"),
os.environ.get("GITHUB_SHA", "unknown"),
),

How to derive values in apollo client

I have a few react components that mutate my server data and for now I have refetchQueries: [{query:myQuery}]. I need to restructured the data as a map for faster lookup time. How can I accomplish this? In Redux, I would have used reselect and in MobX I would have used #computed. As far as I can tell, apollo doesn't support this functionality yet.
I looked into:
#client directive, but this doesn't work for me since I have to compute the data on the server response.
reactive variables don't work either since I will have to change the variable everywhere I mutate the data, far from ideal.
There seems to be very little information out there about computed/derived values when using Apollo Client, the only reference I found was this one:
Apollo GraphQl Storing derived data
How about if you define in your schema & in your resolver some alternative (union) response structure? (not sure if this would work actually)
type Query{
books(mapBy:String): [Book]|JSON
}
so if you query (using it instead of mutation for simplicity)
query{
books(mapBy:"id")
}
it would return JSON
{
123: { __typename: "Book", name: "Dune"}
}
And if you don't want to return all of the Book fields in JSON, maybe pass extra param which would list actual structure you need.
Didn't encounter such problem myself yet, interesting. But otherwise, it should be done on client side.

Graphql query resolvers split into seperate lamdas

I am writing a serverless app using aws lambdas. we are using apollo graphql for single endpoint. graphql endpoint run in one single lamda, how can i split graphql query mutation resolvers into seperate lamda functions, like in graphcool and aws app-sync.
AWS AppSync has the concept of Datasources which can be individual Lambda functions (or you can use DynamoDB tables or Elasticsearch domains). In your GraphQL schema you would then attach a resolver to a field (like a query or mutation) and this resolver would invoke the Lambda datasource. AppSync lets you do this as a single Lambda invocation or "BatchInvoke" for having the Lambda hit multiple resources and return a LIST.
The resolver template (written in Velocity Template Language) is fairly straightforward to send your GraphQL request on through to Lambda, and adding this is a drop down selection from the samples in the console:
{
"version" : "2017-02-28",
"operation": "Invoke",
"payload": $util.toJson($context.arguments)
}
You can read more about this here: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html
As of now, the only way to do it on Apollo is to call your Lambdas by their URIs. The good about this is that you can cache their responses. The bad part is that you can't have a pure GraphQL environment.

Does Parse.Cloud.beforeRead exist is some form?

In Parse there is something called:
Parse.Cloud.beforeSave
I wonder if there is something to play the role of a:
Parse.Cloud.beforeRead
I need a way to control what is going to be returned to the user when a request is made to the DB.
In particular in certain circomstances, depending on information on the server, I want to force blank fields in the result of the DB request made by the user. Any standard way to do this?
There is no Parse.Cloud.beforeRead kind of function supported by Parse.
Instead, you can define a custom cloud function using
Parse.Cloud.define('readObjects', function(request, response) {...} );
that returns array of objects. This function will act as a wrapper over the Parse query.
Then, your client apps should be calling this cloud function to fetch objects rather than direct Parse.Query requests.

Resources