schema design union vs flat properties - graphql

when I have a parent Object type that needs to point to a child Object type but that child Object's type can be of several types and only one can be chosen and populated. I see 2 options I can do with regard to graphql schema design.
option 1 - use union
type child1{......}
type child2{......}
union chooseOne = child1 | child2
type parent{
ref: chooseOne
}
option 2 use multiple props and have only one with data - the rest with nulls
type child1{......}
type child2{......}
type parent{
ref1: child1
ref2: child2
}
is there a 3rd option I am not thinking of? I am not that please with either. I feel like I am missing something... can anyone please comment? thanks! (edited)

am I missing a third possible option?
using an array
type Meeting{
.........(some properties)
recurrences : [Recurrence]
}
union Recurrence = Once | WeeklyRecurrence | MonthlyRecurrence | DailyRecurrence
Of course it looks like a relation ... and it is ... and gives you more possibilities.
all of them can have validFrom and validUntil times - you can filter out all historical entries automatically without manual disabling/removing
you can have AND combinations instead OR, for example 'every working day at 10.00, additionally at 15.00 on fridays'
you can process recurrence entries separately f.e. for notifications (having meeting id just fetch attendees from another relation)
But in fact maybe you don't need separate recurrence types at all, only one field/property (enumerated types) to know which fields are required for render/forms/resolver/mutations etc.
... all of them can use the same DB table, the same record structures in both cases (types mapped to enumed type field).
Update
I prefer 2nd option:
it's more readable/debuggable
it's easier to handle - if(ref1)...<Ref1Component data={ref1}/> ... passing union data object you don't have info about type!
unions sometimes leads to troubles like this

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.

Agile PLM API create relationship between items: Invalid Parameter

I'm developing an scala application using Agile PLM API.
Everything works so far, adding attachments, adding BOM items, creating items and so on.
But when creating Relationships in the Relationship Table, I always get this error:
com.agile.api.APIException: Invalid parameter.
at com.agile.api.pc.Session.createError(Session.java:2039)
at com.agile.api.pc.APIObject.createError(APIObject.java:76)
at com.agile.api.pc.TableRelationships.convertCreateParamMapToVOCells(TableRelationships.java:92)
at com.agile.api.pc.TablePC.doCreateServerRowWithParam(TablePC.java:58)
at com.agile.api.pc.Table.createTableRow(Table.java:267)
at com.agile.api.pc.Table.createRow(Table.java:231)
The Agile API requires a hashmap with attributes and values as parameter to create the relationship. So this is my code:
val cells: java.utils.Map[_, _] = Map(
Attrs.Items.Relationships.CriteriaMet -> null,
Attrs.Items.Relationships.TypeImage -> 666, // id of item type as Integer
Attrs.Items.Relationships.Name -> "foo", // name as String
Attrs.Items.Relationships.Description -> "bar", // the description as String
Attrs.Items.Relationships.CurrentStatus -> "Production", // lifecyclephase 'Production' as a String
Attrs.Items.Relationships.Rule -> null,
Attrs.Items.Relationships.Type -> 600 // id of item type as Integer
)
relationshipTable.createRow(cells)
The relationshipTable instance is of type ITable and this kind of Map is working for adding BOM items and attachments, so I think this is not the issue here.
I simply queried the cells of existing relationships manually and compared their key with my constants used in this map and they are the same. I really don't know what the invalid parameter is. Is there an attribute missing? Is the type of an parameter wrong? There is no indication what's wrong.
Ok, the answer was quite simple, despite it works different than for the BOM and it is not documented.
The solution is simply passing the iitem you want to add as a relationship:
relationshipTable.createRow(iitem)

GraphQL enum union workaround?

I have Major categories and Minor Categories that belong to a Major category.
Both are ENUM type.
I want client to choose matching minor category ENUM to submit with its Major category.
I don't want to include all different minor category ENUMs as fields.
I first tried doing
union MinorCategories = Minor1 | Minor2
However this failed because union only works with ObjectTypes
Enforcing minor category depending on the major category is not necessary. I only want to receive one field that can be selective by the client by ENUM.
Is there any work around?
GraphQL does not support union types for scalar values, only for object types.
One option, albeit an ugly one, is to wrap your enums in an object type.
type Minor1Wrapper {
value: Minor1!
}
type Minor2Wrapper {
value: Minor2!
}
union MinorCategories = Minor1Wrapper | Minor2Wrapper

Select distinct value from a list in linq to entity

There is a table, it is a poco entity generated by entity framework.
class Log
{
int DoneByEmpId;
string DoneByEmpName
}
I am retrieving a list from the data base. I want distinct values based on donebyempid and order by those values empname.
I have tried lot of ways to do it but it is not working
var lstLogUsers = (context.Logs.GroupBy(logList => logList.DoneByEmpId).Select(item => item.First())).ToList(); // it gives error
this one get all the user.
var lstLogUsers = context.Logs.ToList().OrderBy(logList => logList.DoneByEmpName).Distinct();
Can any one suggest how to achieve this.
Can I just point out that you probably have a problem with your data model here? I would imagine you should just have DoneByEmpId here, and a separate table Employee which has EmpId and Name.
I think this is why you are needing to use Distinct/GroupBy (which doesn't really work for this scenario, as you are finding).
I'm not near a compiler, so i can't test it, but...
Use the other version of Distinct(), the one that takes an IEqualityComparer<TSource> argument, and then use OrderBy().
See here for example.

Use LINQ to select elements within a generic list and casting to their specific type

I have a base class, called NodeUpgrade, which have several child types. An example of a specific child class is FactoryUpgrade.
I have a list of NodeUpgrades, which can be a mix of different child types. How do I write a linq query to retrieve a type of NodeUpgrade and cast to that specific type?
My working query looks something like this:
var allFactories = (from Node n in assets.Nodes
from FactoryUpgrade u in n.NodeUpgrades
where u.ClassID == NodeUpgradeTypes.Factory
select u)
This, of course, doesn't work. Can I specify the final type of the output?
If you are sure that every type in a sequence is a given type, you can use the Cast<T>() extension method. If there can be multiple types in the list and you only want one of them, you can use OfType<T>() to filter the sequence.
List<Animal> animals = ...
// assumes all animals are cats
var cats = animals.Cast<Cat>();
// var cats = (from animal in animals where ... select animal).Cast<Cat>();
// or maybe animals can contain dogs, but you don't want them
var cats = animals.OfType<Cat>();
The difference is that Cast will throw an exception if an animal isn't a cat, whereas OfType will perform a type check before actually trying the conversion. I would favor Cast over OfType when you are confident of the uniform type. (Also note that these do not perform user-defined conversions. If you have defined an implicit or explicit conversion, those will not be supported by these methods.)
The resulting sequence in each case will be IEnumerable<Cat>, which you can do further query operations on (filters, groupings, projections, ToList(), etc.)
You can use the method OfType<>
var allFactories = (from Node n in assets.Nodes
from FactoryUpgrade in n.NodeUpgrades
where u.ClassID == NodeUpgradeTypes.Factory
select u).OfType<ChildType>();
As others have said, use the .OfType extension method to filter the types (assuming you have set the inheritance model and appropriate discriminators on your data source). This will translate in the database to include the appropriate Where clause on the discriminator (ClassID).
var allFactories = from n in assets.Nodes
from u in n.NodeUpgrades.OfType<FactoryUpgrade>()
select u;
You didn't specify here if you were using EF, LINQ to SQL, or just Linq to Objects in this case. Each has a different way of modeling the inheritance. If you need help with the modeling portion, let us know which OR/M you are using.

Resources