Hasura request headers in mutation - hasura

Is there any way to include request header in mutation variable?
Say, I have CF-Connecting-IP: 128.0.0.1 in my request header and i need to insert in in mutation like
mutation MyQuery {
insert_signatures(objects:
{ip: CF-Connecting-IP}) {
returning {
ip
}
}
}

It seems this is currently not possible: https://github.com/hasura/graphql-engine/issues/2951#issuecomment-605525605. Hopefully that will change.

Related

How do I select a specific property in nested array json rails 5

I need help with this. I would like to access transactionCustomFields and I would like to get the token from the response.
{
"event":"TRANSACTION OUTCOME",
"timestamp":1672749136029,
"data":{
"transactionId":"b5247b19-1e14-4f3e-818f-a3b1554dd64b",
"transactionStatus":"SUCCESS",
"statusDescription":"SUCCESS",
"transaction":{
"id":"b5247b19-1e14-4f3e-818f-a3b1554dd64b",
"paymentMethod":"MOBILE_MONEY",
"transactionType":"PULL",
"phoneNumber":"+256754187853",
"telecom":"AIRTEL",
"amount":500,
"status":"SUCCESS",
"narration":"",
"failedDescription":null,
"webhookUrl":"https://webhook.site/cbf423c2-15f5-46f3-9c76-1dcc0b9a2e18",
"createdAt":"2023-01-03T12:32:04.176Z",
"transactionCustomFields":[
{
"id":"1fc25a5c-ba0b-4959-b295-31628d8da0e1",
"key":"token",
"value":"31644"
},
{
"id":"2686effb-4f40-45ac-867f-b39d84282dae",
"key":"reference",
"value":"1672749122"
}
]
}}}
This is the JSON response
Assuming you want to access the first item of the "transactionCustomFields" array and its id/key you could do something like this.
json_object = JSON.parse(response)
json_object['data']['transaction']["transactionCustomFields"][0]["key"] //gives you token
json_object['data']['transaction']["transactionCustomFields"][0]["id"] //gives you "1fc25a5c-ba0b-4959-b295-31628d8da0e1"
Hope this answers your question

How do you get the top value of newData in Firebase Realtime Database rules?

New to firebase rules. I may be thinking about this all wrong, but anyway, I am trying to create a rule that makes sure that the newData being written to the database does not exist under the node it is being written. To write to the database I am using this code:
self.reference?.child("users").child(userid).setValue(data) {
(error, result) in
if (error != nil) {
completion(true, error!.localizedDescription)
} else {
completion(false, "")
}
}
Here is where I'd like to create the rule:
{
"rules": {
".read": false,
"users": {
".write": //allow write if the newData's keyBasedOnAuthID is not under students already
"$student_id": {
//more rules...
}
},
My incoming newData looks something like this:
{ keyBasedOnAuthID {
uid: auth.uid
name: xxxx
other:{
key: xxx
key: xxx
}
} }
I am writing this data to a node named 'users,' so the final path would be root.child('users').child(newData). The rule I would like to make would prevent users from writing duplicate data to the 'users' node. So I want to get the keyBasedOnAuthID value from the newData and check that it does not already exist.
Is this possible? I've tried many different ways to retrieve the keyBasedOnAuthID from the newData snapshot, but none have worked. Maybe I need a different approach? Any help would be appreciated!
Unfortunately, I'm not sure which language your using.
However, setValue doesn't create duplicates: either creates or updates data for the given path.
So, if you don't want to update existing data, you need to check for existence before calling your setValue method in your application code.

Firebase Cloud Functions Check Value is not empty

In my cloud function, I take the JSON body and particular elements of it.
const type = request.body.type;
But what if type was optional. And the user didn't have to put it. Is there a way of doing the above without it erroring out.
Because in the instant above, if the user left "type" out of their object, then it would cause an error.
If I correctly understand your question you want to check if there is a type property in the request's body.
The following should do the trick:
if (request.body.type) {
//There is a type, act accordingly
} else {
//No type
}

How to mock a POST request with mockk and fuel in kotlin?

I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every { client.executeRequest(any()).statusCode } returns 200
every { client.executeRequest(any()).responseMessage } returns "test"
every { client.executeRequest(any()).data } returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any() here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every { client.executeRequest(req).statusCode } returns 200
every { client.executeRequest(req).responseMessage } returns "OK"
every { client.executeRequest(req).data } returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?
To all those people that ended up here trying to find a solution to this, I've found something that solves the problem for my use case (but there are likely many use cases it's not appropriate for and I accept that it may not be the nicest...).
Provided you always have the calls to different endpoints in the same order every time you can do -
every { client.executeRequest(any()).data} returnsMany listOf(responseBody1, responseBody2, ... , responseBodyN)
Which will return the next response body for every subsequent call to the Fuel client.
The full code would look like (using OP's example) -
val response1 = "This is response one"
val response2 = "This is response two"
val client = mockk<Client>()
every { client.executeRequest(any()).statusCode } returns 200
every { client.executeRequest(any()).responseMessage } returns "test"
every { client.executeRequest(any()).data } returnsMany listOf(response1.toByteArray(), response2.toByteArray())
FuelManager.instance.client = client
assertEquals("This is response one", testHttpRequest())
assertEquals("This is response two", testHttpRequest())
I suspect the correct way to do this is with a 'CustomMatcher' extension function on the MockKMatcherScope as detailed here. I could only get the mock to response with the last item that'd been mocked when doing that, rather than the correct item but YMMV...
Try to use following:
every { client.executeRequest(req) } returns <mock object>
You could try these lines. Regarding how to intercept fuel request.
fun interceptFuel(method: Method, url: String) {
val interceptor = { next: (Request) -> Request ->
{ req: Request ->
if (req.method == method && req.url.toString() == url) {
val client = mockk<Client>()
/*mock fuel into whatever you like*/
FuelManager.instance.client = client
}
next(req)
}
}
FuelManager.instance.addRequestInterceptor(interceptor)
}
then, use it like this
interceptFuel(Method.GET, "https://google.com")
BTW, this code not fully tested. Use at your own risk

How to traverse object with dynamic keys in Paw?

Let's say we have the following JSON response:
{
"abcd1234": {
"foo": "bar"
}
}
How would "bar" be accessed in a response parsed body value? In the response, "abcd1234" could be anything. But we want the first key in the object (in JavaScript this would be Object.keys(res)[0]).
Paw makes it easy to parse JSON (and XML) responses and access subfields via their key-path.
This documentation article may help: https://paw.cloud/docs/advanced/reuse-values-from-previous-responses
Insert the Response Parsed Body dynamic value
Set the input request and extract the needed value
In your example, the key path will be:
abcd1234.foo
Though, it seems like you need to access the path without knowing the key before hand. If so, one way would be to use a JavaScript snippet to be able to achieve the behavior you want.
On any field, you may right-click and pick Extensions > JS Script.
Here's a snippet that may fit your needs:
function evaluate(context){
var request = context.getCurrentRequest();
var exchange = request.getLastExchange();
var body = JSON.parse(exchange.responseBody);
var key = Object.keys(body)[0];
var value = body[key].foo;
return value;
};

Resources