Hasura: Rename relationship fields - hasura

I have renamed my GQL root fields, which works as described in the documentation.
Unfortunately, these names do not seem to be applied to relations - is this a bug or simply not possible?
Example: Two entities, "Categories" and "Projects", connected by a one-to-many relationship. I renamed the aggregation root fields to "CategoriesAggregate" and "ProjectsAggregate", which works as expected.
However, in the query "Catgeories" (select all) I am offered "Projects_aggregate" as available relationship, not "ProjectsAggregate" as expected.
Here is an example query to illustrate it:
query MyQuery {
Categories {
Projects_aggregate {
aggregate {
count
}
}
id
}
ProjectsAggregate {
aggregate {
count
}
}
}
Is there any way to adjust this?
Thanks

Related

How to override Doctrine #userAware annotion / filter on specific query?

I follow this instruction to create a #userAware annotation which automatically limits all queries on specific entities to the current user.
For example $todoRepo->findByTag("someTag") will automatically add WHERE user_id = 123 to the resulting query to make sure that only ToDo entities of the current user (id 123) can be accessed.
This works great but has one big downside: If for example the admin whats to know how many ToDo entities use the tag someTag he will only find his own entities...
Since using an #userAware annotation seems to be quite common, I wonder if there is any best practice on how to disable/bypass/override this filter/annotation on a specific query.
You can disable the filter. In your example, you must change the kernel request event, like that ...
public function onKernelRequest()
{
if ($user = $this->getUser()) {
if ($user->isAdmin() /* or whatever */) {
$this->em->getFilters()->disable('user_filter');
} else {
$filter = $this->em->getFilters()->enable('user_filter');
$filter->setParameter('id', $user->getId());
$filter->setAnnotationReader($this->reader);
}
}
}

Filter by another node?

Let's say that I have the following structure:
posts {
post_key_1 {
user_key: “key”
message: “message”
}
…
}
users {
user_key_1 {
location_key: true
}
…
}
Each user has a location.
How could I get the posts from a certain location (for pagination)? Is there a way to order the posts by the location in the users node?
One way would be to also include the location_key in the post nodes, but if the user decides to change his location and there are a lot of posts, there would be too much workload on the server.
In SQL, it would be something like select * from posts join users using(user_key) where location_key = 1
Thanks.

symfony add unique values from array collection within foreach loop on prepersist

I'd like to add 'markets' to my document which can be created within a form where you can select agencies which are assigned to these markets. So you don't specifically select the markets but they are automatically added by selecting the agencies.
The logic behind this is working but one thing I didn't yet achieve:
there are several agencies for one market, but I only want the markets to be displayed once. My foreach loop looks like that:
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
$this->addMarket($agency->getMarket());
}
}
}
this is working well, as long as I select only one agency per market. As soon as I select several agencies for one market, it's not working anymore. to avoid this, I changed the code to:
$markets = $this->getMarkets();
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
if(!$this->markets->contains($markets)) {
$this->addMarket($agency->getMarket());
}
}
}
Since markets and agencies are both arraycollections, a simple "in_Array" or "unique_array" is not working. So I thought "contains" is the function I should use for arraycollections. But apparently it's not..
Any further ideas? :)
change condition part code, you should check if market for agency already exists in $this->markets collections:
if(!$this->markets->contains($agency->getMarket())) {
$this->addMarket($agency->getMarket());
}

Joining a table through another join table using withRelated

I have 3 tables:
Order: id
Item: id
OrderItems: order_id, item_id
I want to be able to fetch a particular Order and get all the Items related to that Order, without the clutter introduced by doing Order.where(...).fetch({withRelated: ['orderItem.item']).
Ideally I'd like to do Order.where(...).fetch({withRelated: ['items']) and the items relationship knows to go through the OrderItems table to get the information.
I have tried the relationship
items() {
return this.hasMany('Item').through('OrderItem')
}
but that doesn't seem to work as I'd expected.
Is this possible using Bookshelf's API without writing a manual join?
The relationship you tried:
items() {
return this.hasMany('Item').through('OrderItem')
}
Means to Bookshelf:
Item(id, OrderItem_id) -> OrderItem(id, Order_id) -> Order(id)
Or a 1:n:m relationship.
But your database says:
Item(id) <- OrderItem(Item_id, Order_id) -> Order(id)
This kind of relationship on Bookshelf maps better as belongsToMany():
bookshelf.plugin('registry') // to ease the cyclic mapping
const Item = bookshelf.Model.extend({
tableName: 'item',
orders: function() {
this.belongsToMany('Order', 'OrderItem')
},
})
bookshelf.model('Item', Item)
const Order = bookshelf.Model.extend({
tableName: 'order',
items: function() {
//return this.hasMany('Item').through('OrderItem')
return this.belongsToMany('Item', 'OrderItem')
},
})
bookshelf.model('Order', Order)
Note that until you start placing useful data on the intermediate table you don't need to create a model for it.
With that a Order.where(...).fetch({withRelated: 'items'}).then(...) query should work as expected.

couchdb how to get doc by id in map function

Suppose we have two types of documents. One - stores meta data, many others - have complicated processing, depending on the state of the first document. We do not want to duplicate the state in all documents of the second type because lots of them. How to develop MAP function to receive data from the first-type document.
{ "_id":"123",
"public":true
}
{
"_id":"321",
"owner_id":"123"
"data":"..."
}
function(doc) {
if (doc._id=="321"){
// How do get another document like in python, for example
var doc2 = db[doc.owner_id];
if (doc2.public) {
emit(doc._id, null);
}
}
}
You can do this;
function(doc) {
emit(doc._id, {"_id":doc.owner_id});
}
Then when you do ?key=321&include_docs=true the included doc will be the one with id of doc.owner_id not 321.
More here:
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents

Resources