Apollo/Graphql mutate array - vue.js

I am trying to mutate an array in my Vue application via Apollo to my Graphql backend.
For example the array which I try to mutate is stored in my schema as:
type type1 {
property1: [BigInt]!
}
However, when I assign and pass a string to my mutation, i.e. "[1, 2, 3]" something weird happens and a large integer number will be stored, i.e. "5013710821996412394", instead of my list. When I just try to pass an array I am not able to send my mutation and I get the status error 400 for bad request.
What kind of datatype for the array do I need to execute my mutation correctly to my backend graphql server?

Okay it works now with an array of strings. Before I passed an array of integers to my mutation:
array = [1, 2, 3]
Now I have:
array = ["1", "2", "3"]
... and I am able to execute my mutation correctly. :)

Related

Vuex: Best practice computed object with dynamic keys

I got a question about a computed object in Vuex.
For example, I need to fetch an X amount of requests and I store the response in an object with ID and response
// In the vuex-state
{
1: {object-response for ID 1},
2: {object-response for ID 2},
3: {object-response for ID 3},
...
}
What I want to achieve is that I create a getter where I can just pass the ID and retrieve the object.
getMyData: state => id => state.example[id]
But now I get the problem, the state receives a new response and updates the object of responses, but the getter getMyData doesn't retrigger.
Is this a good practice?
Thanks in advance

react native geocoding get address

I need help for this kind of error when running this will happen object [] , []
Geocoder.getFromLatLng(28.6139,77.2090).then(
json => {
var addressComponent = json.results[0].address_components[0];
alert(addressComponent);
}, error => {
alert(error+"CATCH");
} )
It looks like you are trying to alert an object doing so usually results in getting the following response:
[object Object]
Which isn't really useful. You can solve this by using JSON.stringify
alert(JSON.stringify(addressComponent)) or alert(JSON.stringify(error))
Doing a reverse geocode lookup on the coordinates gives the following alert when I use JSON.stringify
You can read more about JSON.stringify here in the docs.
The JSON.stringify() method converts a JavaScript object or value to a JSON string
While using alert is useful, I prefer using console.warn as you would get something like this, which means you don't have to stringify every response

How to retrieve the old and new values of a store state

In my component I am trying to get the old value and new value of a particular array of objects that is assigned in the vuex store state as followed. However when I newArray and oldArray return the same array of objects.
I understand from the documentation the following but I dont understand what is the best way to retrieve the different versions.
Note: when mutating (rather than replacing) an Object or an Array, the old value will be the same as new value because they reference the same Object/Array. Vue doesn’t keep a copy of the pre-mutate value.
here how I am trying to do it right now in the component
export default {
name: 'O1_OrderBook',
watch: {
'$store.state.orderBookSell': {
deep: true,
handler (newArray, oldArray) {
console.log(newArray,oldArray)
}
}
},
}
let say when you create an array/object in Javascript,
var arr = [1,2,3];
This creates an array in the browser's memory. But what arr variable contains is not the entire array value, it contains the reference to the array in browser memory. You could think of it as arr contains an address that can point you to the real array value in browser's memory.
if you do
var arr = [1,2,3];
var arr2 = arr;
arr2[0] = 4;
console.log(arr); // you would get [4,2,3];
editing arr2 changed arr too. Because they both point to the same array in browser memory.
That's what "the old value will be the same as the new value because they reference the same Object/Array" means.
The same principle applies to object as well. In Javascript, an array is just a special type of object.
to retrieve the different versions of the array in the watcher, you must clone it to set it as a new array everytime you mutate it.
for e.g.,
state.orderBookSell = [...state.orderBookSell];
BUT..... [...array] is shallow cloning, not deep cloning. And that's a problem. You have an array of objects. Remember that object also have the same rules. They are passed by reference. So you gotta do a deep clone, so that all the objects is cloned as well.
using lodash cloneDeep method for deep cloning
state.orderBookSell = _.cloneDeep(state.orderBookSell);
Based on Jacobs answer this is what I re-arranged in my component as to get it to work.
I created a computed variable in the component that deepClones the particular state array of objects.
computed: {
orders () {
return _.cloneDeep(this.$store.state.theArray)
},
},
and then setup a watch for that computed variable
watch: {
orders (newValue,oldValue) {
console.log(newValue,oldValue)
}
}

convert json string to hash and retrieve hash items values in ruby

I have a string object which is basically in a json format and while trying to print it shows in console as
item =
{
"id": "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
"item": "{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"dummy","name":"Finance"}"
}
I need to get the values of business, class, date etc and pass it as params to my method. So I tried to convert it into hashes as below
hash_item = JSON.parse (item)
and output in console shows as
The converted hash item is
{"guid"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba", "item"=>"{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"Dummy","name":"Finance"}"}
But when I try to access the hash value for business as
hash_item['item']['business'] it shows
"business"
since the value of item is a String in the hash_item. I am not sure whether my approach is correct or not. So is there any better idea or any inputs to retrieve the hash values . Please help.
Updated Code:
Actually I am implementing a queuing mechanism with right_aws gem for my application and the queue item returns a Message Instance of String format. so in my controller I am accessing the queue object as
def next
#receiver = #queue.pop()
if #receiver.present?
respond_with(#receiver)
end
end
And my next.json.erb would be
{
"guid": "<%=#receiver.id%>",
"item": "<%=generate_json_format(#receiver)%>"
}
my helper to generate json format is
def generate_json_format(sender)
(JSON.parse (sender.body.gsub('=>', ':'))).to_json
end

How to store an array into a cookie?

How do you store a multi-array type of data into a cookie.
For example: [[1, 'foo'], [2, 'bar'], [3, 'foobar']]
I can get it to work with a single dimensional array as such:
cookies[:foobar] = { :value => cookies[:foobar] << ",1" }
and then do
cookies[:foobar].split(',').include?("1")
To verify that 1 exists inside the cookie. Not too sure how I can get around this with a multidimensional array
Serialize array into json and store to cookies.
Look at two methods:
ActiveSupport::JSON.encode(object)
ActiveSupport::JSON.decode(string)
The easiest is probably to use one of the serialisation methods rails/ruby provides such as YAML, marshalling or json.