GraphQL filtering based on parent context - graphql

I've read the docs and I cannot seem to figure out how to structure my GraphQL for a particular query. For my data I have:
child
|_ school
|_ class
A child has schools and schools have classes, but a child is only assigned to specific classes in a school.
I want to query a specific child to get only the classes they are in.
query={
child(id:$id){
schools{
name
classes{
name
}
}
}
}
I can technically filter the classes while resolving the schools field in the child type by looking deep down the fields but I wanted to clarify that this is still conforming to GraphQL. Should I be placing the classes as a field in the child type instead?

A child also have classes, so it makes sense to create a classes field in the child object. This, in addition to the classes field in the school object.
Of course you could also filter the classes while resolving it but it's just another extra work with no particular reason.
Since there is a direct relationship between a child and his classes it seems better for a child to have a classes field.

Related

Is there a way to easily validate entity association in Spring?

I am working on a Spring REST application where we have RequestMappings with nested path variables like so...
/grandparent/{grandparentId}/parent/{parentId}/child/{childId}
In my database there is a fk constraint from
child => parent
parent => grandparent
so a child knows its parentId and a parent knows its grandparentId
does Spring provide some easy way to validate that all of these are associated with one another?
If I had a request for
/grandparent/1/parent/2/child/3
I would want to ensure that child had a fk with parent and that parent had an fk with grandparent.
Ideally I'd like to abstract this away in the most elegant way possible and my intuition tells me there is a baked in way to handle this situation, I just need to be pointed in the right direction.
Thanks!

Explain the various types of association in ORM and how do you manage the partial changes to a parent objects without modifying the child objects.?

What are the various types of association in ORM and how do we manage the partial changes to a parent objects without modifying the child objects ?

ElasticSearch - Multiple types sharing the same child type

I'm looking for some help with ElasticSearch involving multiple "parent" types sharing the same "child" type.
As a trivial example, let's say I have two parent types:
blogEntry
status
I'd like to have a single "comment" type which is a child of both "blogEntry" and "status", since users can comment on both blog entries and users' status updates.
Is this actually possible in ES?
Or, am I looking at this problem in the wrong way? Does parent-child make no sense here, and instead would I want to use nested objects?
Thanks!
This is not possible at the moment:
The parent child mapping does not allow to specify multiple parents
Lets say if you have 2 parents which are routed to different shards,
which shard will the child document be routed to?
You could instead have one child as nested document to the parent and the more frequently changing one as an actual child referencing the parent type.

Getting all aggregate root entities child entities?

I am attempting to refactor my application from a repository per entity to a repository per aggregate root.
A basic example would be I have an entity root of Cars. Cars have hire contracts. As far as I can see contracts don't exist without cars hence cars is the aggregate root.
I am trying to implement a user view which will shows every contract in the system(all the child entities of the root entities). Before refactoring I could just go to my contracts repository and get All. As contracts repository has been removed (as its not a root) I now need to get all cars out of my repository and then get all their contracts.
My repository has the interface
public interface ICarRepository
{
IQueryable<Car> All { get; }
IQueryable<Car> AllIncluding(params Expression<Func<Car, object>>[] includeProperties);
Car Find(long id);
void InsertOrUpdate(Car car);
void Delete(long id);
void Save();
}
I thought of creating an ICarManagementService and having it have a GetAllContracts method (perhaps with filter parameters). Would that mean to get all contracts I need to pull all car entities out with their contracts and then retrieve each entities associated hire contracts and filter them?
I can then pass these to the controller and AutoMap the contracts as before.
Is this best practice?
Thanks
Graeme
As far as I can see contracts don't exist without cars hence cars is
the aggregate root.
This is not necessarily true. 'Don't exist without' is not enough for an entity to become a part of an Aggregate Root. Consider classic order processing domain. You have an Order that is an Aggregate Root. You also have a Customer that is an Aggregate Root. Order can not exist without a Customer but it does not mean that Orders are part of the Customer Aggregate. In DDD entities inside one Aggregate can have references to other Aggregate Roots. From DDD book:
Objects within the AGGREGATE can hold references to other AGGREGATE
roots.
Aggregate is a life cycle and data exchange unit. It is essentially a cluster of objects that enforces invariants. This is something you want to be locked if you have multiple users changing domain at the same time.
Back to your question, my understanding is that the domain is something like rent / lease a car / truck / limo / bulldozer. I think that HireContract may not be a part of Car aggregate because they may have different lifecycles and HireContract just makes sense on its own, without a Car. It seem to be more of a Order-Product relationship that is also a classic example of two different Aggregates referencing each other. This theory is also confirmed by the fact that business needs to see "All Contracts". They probably don't think of Car containing all Contracts. If this is true than you need to keep your ContractsRepository.
On an unrelated note, you might be interested in reading this answer about repository interface design.
Separate the concept of read/query from the write/command, as guided by CQRS it is preferable to design the application by separating read model which consists of read only queries and the write model on the other hand which consists of commands to execute certain logic on the domain model.
thus querying all aggregate roots or creating custom queries to join sets of data is not a good candidate of domain repository, instead put these queries into read repository (or better named Finders).
if you find yourself wanting to query a collection of objects in order to execute some domain logic then it is an indicator that you have to abstract this collection and put it into an aggregate root to encapsulate them and make the business operation or method act on them.
check out (http://moh-abed.com/2011/09/13/pure-old-ddd-with-a-twist-from-cqrs/) and (http://simon-says-architecture.com/2011/08/23/repository)

LinQ ORM Data Objects and Inheritance

I'm thinking about how to do this, but I have several different shapes of Data in my Database, Articles, NewsItems, etc.
They All have something in common, they all have IDs (in the DB they're named ArticleID, NewsID etc. )
They all have a Title
They all have BodyText.
They all have a Status
They all have a DateAdded
What I'd like to do is standard class inheritance.
I'd like a Master Class (I don't need to write this to the database) called Content with fields like:
ID
Title
SubTitle
BodyText
Status
AddedDate
I'm not sure how I can do this with the ORM. Why I want this is because then I can pass a list of COntent to my UserControl which is responsible for Rendering it. It will only need the information that is common to all objects.
Is this even possible?
This is what Interfaces are for. Have each class implement an IContent interface that contains your Title, BodyText, Status and DateAdded properties. Now you can pass around a collection ( List<IContent> ) around that could containt different types of content.
If you're using LinqToSql you can create partial class files to have the autogenerated classes implement the interface you want.
public partial class SomeContent : IContent
I'm not sure whether you're talking about LINQ to SQL, but there are a few resources online about how to create inheritance with it:
LINQ To SQL Discriminator Column Example - Inheritance Mapping Tutorial
Inheritance in LINQ to SQL Screencast
...and more.
HTH
I found one page that looks like it has something along the lines of what I'm looking for (The last post)... but I'm not sure about using weakly typed objects:
LINQ inheritance

Resources