Hallo we have a problem with finding the best way of connecting
GraphQL, Rebing GraphQL, Laravel and Eloquent.
The main problem is related to mutation for nested resources
.
For example: Manufacturer->Brand->Product
So far we have tried:
mutation {
addManufacturer(
address: "New York"
brands: [
{
name: "Brand A"
products: [
{
name: "Model 1"
}
{
name: "Model 2"
}
]
}
{
name: "Brand B"
products: [
{
name: "Model 3"
}
{
name: "Model 4"
}
]
}
]
) {
address
}
}
mutation {
updateManufacturer(
id: 1
address: "LA"
brands: [
{
id: 1
name: "Brand Z"
products: [
{
id: 1
name: "Model 10"
}
{
name: "Model 11"
}
]
}
]
) {
address
}
}
This basically means:
addManufacturer mutation is only about CREATING new records in DB
updateManufacturer mutation is about UPDATING the records with present ID and CREATING the records without ID.
The problems:
Even though GraphQL allows to define nested structure we have a problem with representing such structure in Eloquent ORM. Right now we separate the presented data to two sparate sets. One set consisting data which needs to be CREATED and the second set holds the data that needs to be UPDATED. For that we have a recursive method which iterates through the mentioned above structure of data and handles (create/update) records in context of the relation to its parent.
To perform updates we need to fetch all the models which we have IDs for (in the data structure). Creating the model using the constructor with the ID provided does not update the record but creates a new one. Creating the new model with createOrUpdate method performs a select query on database too.
Another problem we encountered is related to users authorization for each nested "resource" separately. For example we need to apply some authorization logic for updating a product (only the creator of the product can update it). We don't want to put such logic in our recursive method, because we think it does not belong there. We could delegate such logic to some other place but then we would have to fetch it from the database again.
Our concerns/questions:
Should we use some different approach?
Should we use nested objects with properties like
create: [], update: [], delete: []
instead of arrays?How to populate such data structure to Eloquent models with relations?
Is it ok to nest such
resources
inGraphQL
at all? Or is it an anti-pattern?How about splitting all the nested "resources" to separate mutations?