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.
Related
With GraphQL is it possible to define response layout in a file and use the same file while calling the GraphQL query? just to avoid too long request body.
I tried to use different string for request and response from java code. For very large request and response its quite difficult to handle from source.
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"),
),
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.
So currently I'm building a log system using ELK Stack. Before building this ELK, I already have custom log format for my apps, so that it can be easily read by human. My log is formatted something like this
Method: POST
URL: https://localhost:8888/api
Body: {
"field1":"value1",
"field2":[
{
"field3":"value2",
"field4":"value3"
},
{
"field3":"value2",
"field4":"value3"
},
]
}
using grok pattern, I can get the Method and the URL, but how can I get the full body json in grok / logstash so that i can send them to elasticsearch?
Since the length of the json is not fixed and can be longer or shorter each log
Thank you
You can use the JSON Filter.
It should parse the JSON for you, and put it into a structured format so you can then send it where ever you need (e.g. Elasticsearch, another pipeline)
From the docs
It takes an existing field which contains JSON and expands it into an actual data
structure within the Logstash event.
There are also some other questions here on SO that could be helpful. An example: Using JSON with LogStash
I'm having a problem getting a couchdb view to return the proper documents when using multiple keys.
This works fine, returning the documents that match:
GET http://example.com:5984/myDb/_design/myFilters/_view/getItemByForeignKeyId?key=abc123
This returns returns all documents in the view, matching or not:
GET http://example.com:5984/myDb/_design/myFilters/_view/getItemByForeignKeyId?keys=%5B%22abc123%22%5D
I'm usually very good at hunting down my answers. But, CouchDB documentation is very clear on the format for using multiple keys. I've seen some use the ?keys=[123,123] and i've also seen ?keys="abc","abc".
If anyone can offer any clarification on the 'proper' format and encoding of multiple key queries for CouchDB using a GET method, I would be extremely appreciative.
To get multiple keys from a view, you need to do a post request and submit the keys in the request body. Your HTTP request will look like this:
POST /myDb/_design/myFilters/_view/getItemByForeignKeyId
Content-Type: application/json
{
"keys" : [
"abc",
"123"
]
}
function(doc){
{
if([doc.key1, doc.key2])
emit([doc.key1, doc.thingYouWantToKnow]);
}
}
and in the query string, at the end
?key=["key1Value", "key2Value"]
Notice that it is key=[], not keys=[] !!!!!!!!!
Not saying it's correct, but you can actually do it via query string as well. The array enclosing brackets should not be encoded. E.g. this works for me:
http://localhost:5984/test/_design/artists_albums/_view/albums_by_artist?keys=[%22Super%20bad%20artist%22,%20%22Fake%20artist%201%22]