I am new to GraphQL and I've been trying to come up with a simple query but I'm not getting the results I need.
Basically, I am writing a mutation to delete a row when the following conditions are met:
(A = a AND B = b) OR (A = b AND B = a)
However, I can't seem to figure out how to write it.
I tried doing something like:
delete_links(where: {_or: {_and: {a: {_eq: a}, b: {_eq: b}}}, {_and: {a: {_eq: b}, b: {_eq: a}}}) {
affected_rows
}
}
I am using Hasura on postgresql.
Basically I have a table called Links storing:
link_a | link_b
The problem is a link between item 9 and 2 in link can either be:
link_a | link_b
2 | 9
or
link_a | link_b
9 | 2
Hence I want my delete mutation to delete BOTH cases. However I cant seem to do a query (A = a AND B = b) OR (A = b AND B = a)
Since _or, _and is array. So, your mutation should look like this:
# in this example: typeof A = Int, typeof B = Int
mutation delete_links($a: Int, $b: Int) {
delete_links(
where: {
_or: [
{ A: { _eq: $a }, B: { _eq: $b } }
{ A: { _eq: $b }, B: { _eq: $a } }
]
}
) {
affected_rows
}
}
# if you'd like to use additional _and
mutation delete_links($a: Int, $b: Int) {
delete_links(
where: {
_or: [
{ _and: [{ A: { _eq: $a } }, { B: { _eq: $b } }] }
{ _and: [{ A: { _eq: $b } }, { B: { _eq: $a } }] }
]
}
) {
affected_rows
}
}
Related
In the database, there are shoes of sizes 41.0, or 41.5-42, 42-43, etc. I have them saved as:
41.0 -> shoes_min=41.0; shoes_max=41.0
41.5-42 -> shoes_min=41.5; shoes_max=42.0
42-43 -> shoes_min=42.0; shoes_max=43.0
My proposed filter to show up all of the above examples as a user might (on the frontend there would just be 2 input values of (41, 42) which indicates the range the user is interested in. It should match all of the above 3 examples, except in my case it is not matching entries with shoes_min=42; shoes_max=43
query MyQuery {
models(
where: {
shoes_min: {_gte: "41"}
_and: {
shoes_min: {_lte: "42"}
},
_or: {
shoes_max: {_gte: "41"}
_and: {
shoes_max: {_lte: "42"}
},
},
},
order_by: {shoes_max: asc}
) {
shoes_min
shoes_max
name
url
}
}
Here is the correct request body:
query MyQuery {
models(
where: {
_or: [
{_and: [{shoes_min: {_gte: 41}}, {shoes_min: {_lte: 42}}]},
{_and: [{shoes_max: {_gte: 41}}, {shoes_max: {_lte: 42}}]}
]
},
order_by: [{shoes_min: asc}, {shoes_max: asc}]
) {
shoes_min
shoes_max
name
}
}
types.json:
{
"WorkerId": {
"_enum": {
"Single": "Single",
"Double": "Double"
}
},
"Single": "u8",
"Double": "(u8, u8)",
}
substrate code:
#[pallet::storage]
#[pallet::getter(fn worker_infos)]
pub type WorkerInfos<T: Config> = StorageMap<_, Twox64Concat, WorkerId, WorkerInfo, ValueQuery>;
pub enum WorkerId {
Single(u8),
Double(u8, u8),
}
I want to query worker_infos by WorkerId in polkadot.js:
workerIds = [1,2]
api.query[wrpc][wcallable]
.multi(workerIds, (results) => {
...
})
.then((unsub) => {
...
})
.catch(console.error);
Error info:
REGISTRY: Error: Unable to create Enum via index 2, in Single, Double
Any ideas on this? How to pass workerIds(enum type) in polkadot.js?
{ Single: 1 } or { Double: [2, 3] }
Trying to assign part of a has to another variable. I have a hash. Something like:
hash = {
"cupcake" => {
"a" => 1
},
"muffin" => {
"b" => 2
}
}
When I do something like:
cupcake = hash["cupcake"]
cupcake is nil after this code.
If you want string keys you have to use this syntax
hash = {
"cupcake" => {
"a" => 1
},
"muffin" => {
"b" => 2
}
}
Syntax with colons is for symbol keys
hash = {
cupcake: {
a: 1
},
muffin: {
b: 2
}
}
cupcake = hash[:cupcake]
I have many records where the msg is 'a'. Some of these records have the same type.
I'm trying to write a query that counts the number of with msg 'a', but doesn't count duplicates.
Example:
1: msg = 'a', type = 'b'
2: msg = 'a', type = 'b'
3: msg = 'a', type
= 'c'
This should return a count of two because the first and second records have the same type and are only counted once.
Here is my query so far.
body: {
query: {
bool: {
must: [
{
range: {
"#timestamp" => { from: 'now-1d', to: 'now' }
}
},
{ match: { msg: 'a' }}
]
}
}
}
Any help is appreciated!
Try using aggregations they'll count it for you :)
Read here:
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-aggregations-bucket-terms-aggregation.html
And try something like this:
body:{
query: {
bool: {
must: [
{
range: {
"#timestamp" => { from: 'now-1d', to: 'now' }
}
},
{ match: { msg: 'a' }}
]
}
}
},
aggs:{
"type":{
"terms":{
"field":"type"
}
}
}
}
I would like a crossfilter group that gives the word frequency and average rating for each word in a series of surveys so that I can make an awesome interactive word-bubble-frequency chart.
My data looks like:
[{feedback: "This is a horrible service", rating:2},
{feedback: "I love everything about everything", rating: 10},
{feedback: "love the user interface, good service", rating:6},
{feedback: "", rating: 7} ]
I would like something like:
[ {key: love, count:2, ave: 8}, {key: horrible, count:1, rating:2 }, {key: service,
count: 2, rating: 4 } ,.... ]
So far I have:
function to break up string into tokens, returns object with frequency for each word
var wordcnt = function(bah ){
var hist = {}, words = bah.split(/[\s*\.*\,\;\+?\#\|:\-\/\\\[\]\(\)\{\}$%&0-9*]/)
for( var i in words)
if(words[i].length >1 )
hist[words[i]] ? hist[words[i]]+=1 : hist[words[i]]=1;
return hist;
};
Loading data into d3 and crossfilter
d3.csv("test.csv", function( error, data) {
data.forEach(function(d) {
d.rating= +d.rating;
d.wordCount= wordcnt(d.feedback.toLowerCase());
});
var ndx = crossfilter(data);
var all = ndx.groupAll();
var frequencyDimension=ndx.dimension(function(d){return d.wordCount; });
And one butchered group reduce function!
var frequencyGroup= frequencyDimension.group().reduce(
function (p, v) {
for (var key in v.wordCount) {
if (v.frequency.hasOwnProperty(key)) {
p.frequency[key]+= v[key];
p.frequency[key].count++;
p.frequency[key].sum+= v.rating ;
}
else{
p.frequency[key]=v[key];
p.frequency[key].count=1;
p.frequency[key].sum = v.rating;
}
}
p.frequency[key].ave = p.frequency[key].sum/p.frequency[key].count ;
return p;
},
function (p, v) {
for (var key in v.wordCount) {
if (v.frequency.hasOwnProperty(key)) {
p.frequency[key]-= v[key];
p.frequency[key].count--;
p.frequency[key].sum-= v.rating ;
}
//don't need an else statement because can't remove key if it doesn't exist
}
p.frequency[key].ave = p.frequency[key].sum/p.frequency[key].count ;
return p;
},
function (p,v) {
return { frequency: {} } ;
}
)