Zendro-dev / graphql-server-model-codegen

Command line utility to auto-generate the structure files for a graphql server
MIT License
1 stars 2 forks source link

Implement a validation filter for contradictory association update arguments #137

Open asishallab opened 4 years ago

asishallab commented 4 years ago

In the rare case of a parent-child relationship inside the same model, i.e. a graph structure of the data, a node can have a to-one association to a "parent" and a to-many association to "descendants". Our update API (resolver) accepts arguments for both ends of the relationship (association).

To explain the problem consider this example. As shown, the problem arises if the "parent" shall be set to any node, while the "descendants" include the node to be updated itself. Thus the foreign key "parent_id" of the current record shall be set to two different conflicting values: The node's own ID and at the same time the other "parent" as provided in the input.

Solution

In the case of two associations that

  1. are with the same target data model as the source data model (parent-child relation),
  2. both ends of the association, to-ne and to-many, are declared,
  3. both associations use the same foreign-key (just to check and be really sure)

the code-generator should create an additional validation filter for the update, and also create, arguments. The validation filter should throw a validation error, if and only if

  1. the record's own id appears in the "add_descendants" argument, and
  2. at the same time the "add_parent" argument is provided and set to something different that the record itself.

Question: Can this be also a problem in the "remove-association" case?

Think through a similar case providing "remove_parent" and "remove_descendants" arguments. Can a similar problem arise?

asishallab commented 4 years ago

Question: Can the above problem occur in 'create' cases?

Yes, if the following conditions are met:

  1. We use "internal IDs", i.e. a unique attribute for which the user provides values - so non-database sequences. This enables the user of Zendro to provide the record ID before creation happens, thus the record can be associated with itself.
  2. If check-existence is switched of, so the record does not need to be persistent to be able to be associated with itself.

Conclusion

We need the above validation check also for create cases. At least those that have the above conditions.