You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dicussion: Support mutations in graphene-sqlalchemy #285
This is going to be a discussion thread to debate whether it is good to implement mutations in graphene-sqlalchemy. There is definitely a scope for this feature and I think it's useful, so the real question is more towards how to implement it.
We can probably start with creating objects and then later expand to updating the various attributes.
There are discussions around this topic in #213 as well as #29. I'll copy the relevant items here so that we won't have to repeat them again.
Points to note:
Have a class named SQLAlchemyInputObjectType and have model and exclude_fields as meta properties.
No need to worry about hybrid or composite columns as they are basically created from other columns. So, we just need a mechanism to just accept the fields. So, a function named construct_fields_for_input.
How to handle relationships ?
Since we are creating row entries for the table, we can probably accept id of the related table entry and convert it to the database id.
It's beyond obvious that this is useful, it's basically required for anyone to seriously use graphene-sqlalchemy in a complex project. I've been using this in my fork for about 2 years now. It's not pretty because I mostly copy pasted stuff from the library code for creating these objects with a few changes, but it gets the job done.
Hello. Just switched from Django to FastAPI for lightweighting a project I'm working on.
There is a big Django project here in the community, called "Saleor". It seems that they have perfected the model mutation logic, based on Graphene. Please Consider to have a look on how they implemented the code 😃
There's definitely a use case for this!
Just adding a few remarks:
No need to worry about hybrid or composite columns as they are basically created from other columns. So, we just need a mechanism to just accept the fields. So, a function named construct_fields_for_input.
I think there's a more differentiated view to this. In some cases, only the composite/hybrid field is relevant to the API. When users want to create/modify an object, they might only know about the hybrid property but not the underlying columns/data structures. Sqlalchemy provides setters for hybrid, tackling this problem. Optimally, our solution should provide similar functionality. However, we can leave that to the developer implementing the mutation for now.
This would be an issue that needs to be resolved first for auto-generation.
Another blueprint could be the way Dgraph autogenerates mutations
This is also a great idea, but I'd see that as a separate feature since it drastically extends the scope of the input objects. A first step could be to design the input objects to be compatible with future extensions like auto-generation.
InputObjects would already be helpful in common mutations aimed at modifying an object.
Some other points that come to my mind are the following:
Handling of required & excluded fields on a mutation basis.
There should be an easy way to configure which fields of an input object are required for a particular mutation. An example of
this is "create" and "update" mutations. Of course, the dev could manually check for specific fields to be not null in the input
type, but providing a general solution might improve this feature's usability. Staying with the create and update example, an update mutation may require the ID to be present while a create mutation would not allow IDs in the input. The original proposal by would now require two separate inputobjects (one excluding the ID, one not excluding any fields). That leads to a lot of clutter when applied to the entire Schema.
Automatic generation of InputType Objects
In most cases, an InputType would have the same fields as its corresponding SQLAlchemyObjectType (OutputType). It might be worth investigating overridable auto-generation of the input types based on the output types. This could be configured in the meta options of the OutputType. @maquino1985's approach could also be an alternative.
Activity
maquino1985 commentedon Oct 2, 2020
It's beyond obvious that this is useful, it's basically required for anyone to seriously use graphene-sqlalchemy in a complex project. I've been using this in my fork for about 2 years now. It's not pretty because I mostly copy pasted stuff from the library code for creating these objects with a few changes, but it gets the job done.
You can use it like this, very easy:
(in the Meta class set create=True for creation, delete=True for deletion. Setting neither will update)
seanxlliu commentedon Mar 17, 2021
@maquino1985 This a good example and good start. Maybe we can work together on a PR.
jbvsmo commentedon Jun 14, 2021
Any news on this?
kpraznik commentedon Jul 14, 2021
maybe tagging @jnak will help to get some feedback.
ArielLahiany commentedon Aug 23, 2021
Hello. Just switched from Django to FastAPI for lightweighting a project I'm working on.
There is a big Django project here in the community, called "Saleor". It seems that they have perfected the model mutation logic, based on Graphene. Please Consider to have a look on how they implemented the code 😃
alexanderhupfer commentedon Jan 26, 2022
Another blueprint could be the way Dgraph autogenerates mutations:
https://dgraph.io/docs/graphql/mutations/mutations-overview/
erikwrede commentedon May 5, 2022
There's definitely a use case for this!
Just adding a few remarks:
I think there's a more differentiated view to this. In some cases, only the composite/hybrid field is relevant to the API. When users want to create/modify an object, they might only know about the hybrid property but not the underlying columns/data structures. Sqlalchemy provides setters for hybrid, tackling this problem. Optimally, our solution should provide similar functionality. However, we can leave that to the developer implementing the mutation for now.
This would be an issue that needs to be resolved first for auto-generation.
This is also a great idea, but I'd see that as a separate feature since it drastically extends the scope of the input objects. A first step could be to design the input objects to be compatible with future extensions like auto-generation.
InputObjects would already be helpful in common mutations aimed at modifying an object.
Some other points that come to my mind are the following:
There should be an easy way to configure which fields of an input object are required for a particular mutation. An example of
this is "create" and "update" mutations. Of course, the dev could manually check for specific fields to be not null in the input
type, but providing a general solution might improve this feature's usability. Staying with the create and update example, an update mutation may require the ID to be present while a create mutation would not allow IDs in the input. The original proposal by would now require two separate inputobjects (one excluding the ID, one not excluding any fields). That leads to a lot of clutter when applied to the entire Schema.
In most cases, an InputType would have the same fields as its corresponding SQLAlchemyObjectType (OutputType). It might be worth investigating overridable auto-generation of the input types based on the output types. This could be configured in the meta options of the OutputType. @maquino1985's approach could also be an alternative.