How to use one to many relation query in parse.com? - parse-platform

I have the 2 classes one is post and other is comments
=> One post has many comments
And i want to result like this
results:[ {
post_title:'post1',
date:'..',
postcomments:[
{comment1},
{comment2},...]
},
{
post_title:'post2',
date:'..',
postcomments:[
{comment1},
{comment2},...]
}
]

The easiest would be to use Lists: The Post object has a property postcomments that is a list of Comment objects. You could query then as follows (javascript):
new Parse.Query("Comment")
.include("postcomments")
This will give you a list of Post objects. Each Post object will have a list of associated Comment objects which you can access like this: post.get("postcomments")
If you expect to usually have more than 100 or so comments for one post you can also use Parse relations. The advantage here is that the comments are not always fetched together with the post thus saving data.

Related

Group queries in GraphQL (not "group by")

in my app there are many entities which get exposed by GraphQL. All that entities get Resolvers and those have many methods (I think they are called "fields" in GraphQl). Since there is only one Query type allowed, I get an "endless" list of fields which belong to many different contexts, i.E.:
query {
newsRss (...)
newsCurrent (...)
userById(...)
weatherCurrent (...)
weatherForecast(...)
# ... many more
}
As you can see, there are still 3 different contexts here: news, users and weather. Now I can go on and prefix all fields ([contextName]FieldName), as I did in the example, but the list gets longer and longer.
Is there a way to "group" some of them together, if they relate to the same context? Like so, in case of the weather context:
query {
weather {
current(...)
forecast(...)
}
}
Thanks in advance!
If you want to group them together , you need to have a type which contain all fields under the same context . Take weather as an example , you need to have a type which contain currentWeather and forecastWeather field. Does this concept make sense to your application such that you can name it easily and users will not feel strange about it ? If yes , you can change the schema to achieve your purpose.
On the other hand, if all fields of the same context actually return the same type but they just filtering different things, you can consider to define arguments in the root query field to specify the condition that you want to filter , something like :
query {
weather(type:CURRENT){}
}
and
query {
weather(type:FORECAST){}
}
to query the current weather and forecast weather respectively.
So it is a question about how you design the schema.

GraphQL: Querying for a list of object which have relationship to another object

I have set up my schema on GraphCMS and finding graphQL to be very convenient.
I have a workout object and a workoutCategory object. Those two are linked by a many to many relationship.
I'd like to write a query that allows me to get the list of workout which are part of a certain category.
I'm writing the query as follow:
workout(where:{ workoutCategories: { id: "xxxxxxx" } }) {
id
}
graphCMS gives me a syntax error on the workoutCategories which does not make sense to me yet
Field workout categories is not defined by type WorkoutWhereUniqueInput
What do I need to do to be able to achieve my goal?
Thanks in advance
Turns out I need to query on 'workouts' (see the 's' at the end) and not on 'workout'...

How to set same meta data across collections?

Am trying to set a product category on different collections but only the last collection defined in docpad.coffee actually sets it when trying it like so
firstCollection: ->
#getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({category: 'first'})
secondCollection: ->
#getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({category: 'second'})
document.categorywill be 'second' for all documents of each collection.
How to set the same meta data individually per doc in a collection?
What problem are you trying to solve? Because your approach is not going to work. If you share what you're trying to do, we may be able to suggest an alternative approach.
Your current approach won't work because you are setting a metadata property named "category" that is a string. That metadata property lives on the documents in the collection and not on the collection itself.
Both collections are pointing at the same set of documents. Each individual document can only have a single value for that property. It can't be both 'first' and 'second'. The last one to set it wins, and in this case, the event that sets it to 'second' is happening last and so all of the documents have 'second' as the value for that metadata property.
Update: I found a better way to do this: model.setMetaDefaults({foo:'bar'})
For example, to create a blog collection with a default cssClass of post:
collections: {
blog: function() {
return this.getCollection("documents")
.findAllLive({relativeOutDirPath:'blog'}, [{filename:-1}])
.on("add", function (model) {
model.setMetaDefaults({'cssClass': 'post'})
});
}
},
This would go in your docpad.coffee file or, in my case, docpad.js.
See a working example with full context at https://github.com/nfriedly/nfriedly.com/blob/master/docpad.js#L72 (collection is called "techblog", starts around like 72).

Output entities with relationships in json

I have a device entity which has one-to-many relationship with picture entity. Like one device has many pictures. How can I output in JSON all device properties plus array of pictures?
When I try:
dd(Device::find(1)->pictures);
I get array of laravel objects with some additional information. I tried to do it in some ways but didn't managed to get just an array of simple picture objects (or array of arrays)
Although this works:
foreach (Device::find(1)->pictures as $picture) {
$data['pictures']['path'] = $picture->path;
}
dd($data['pictures']);
It seems weird to form array that way
Basically I need to output json with arrays of one-to-many objects, like pictures and some other. So I'll get something like:
["name": "myDevice", "price": "15", "pictures": [...], "another": [...]]
I am using laravel 3
Are you using Laravel 3? In that case this should do the trick:
return Response::eloquent(Device::with('pictures')->find(1));
If your are using Response::eloquent the content-type is also automatically set to application/json.
Edit: if you want only certain fields to return, you could use Response::json(Device::with('pictures')->lists('name', 'id')); to return an array just of that values already JSONified.

How can I use set operations to delete objects in an entitycollection that match a collection of view models?

Here is a very basic example of what I want to do. The code I have come up with seems quite verbose... ie looping through the collection, etc.
I am using a Telerik MVC grid that posts back a collection of deleted, inserted and updated ViewModels. The view models are similar but not exactly the same as the entity.
For example... I have:
Order.Lines. Lines is an entity collection (navigation property) containing OrderDetail records. In the update action of my controller using the I have a List names DeletedLines pulled from the POST data. I also have queried the database and have the Order entity including the Lines collection.
Now I basically want to tell it to delete all the OrderDetails in the Lines EntityCollection.
The way I have done it is something like:
foreach (var line in DeletedLines) {
db.DeleteObject(Order.Lines.Where(l => l.Key == line.Key).SingleOrDefault())
}
I was hoping there was a way that I could use .Interset() to get a collection of entities to delete and pass that to DeleteObject.. however, DeleteObject seems to only accept a single entity rather than a collection.
Perhaps the above is good enough.. but it seemed like there should be an easier method.
Thanks,
BOb
Are the items in DeletedLines attached to the context? If so, what about this?
foreach (var line in DeletedLines) db.DeleteObject(line);
Response to comment #1
Ok, I see now. You can make your code a bit shorter, but not much:
foreach (var line in DeletedLines) {
db.DeleteObject(Order.Lines.SingleOrDefault(l => l.Key == line.Key))
}
I'm not sure if DeleteObject will throw an exception when you pass it null. If it does, you may be even better off using Single, as long as you're sure the item is in there:
foreach (var line in DeletedLines) {
db.DeleteObject(Order.Lines.Single(l => l.Key == line.Key))
}
If you don't want to re-query the database and either already have the mapping table PK values (or can include them in the client call), you could use one of Alex James's tips for deleting without first retrieving:
http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retrieving-it.aspx

Resources