-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for default mutations #30
Comments
I was able to achieve this functionality using code such as:
Although currently you still must explicitly define arguments. If this feature is desired I can submit a PR :) |
Update: I ended up needing to return type instead, otherwise it was treated as the same object each time. Here's the new code that works (so far) with more than one model: def get_create(object_model, arguments):
class CreateMutation(graphene.Mutation):
Arguments = arguments
model = graphene.Field(object_model)
def mutate(self, _, **kwargs):
model = object_model._meta.model(**kwargs)
model.save(force_insert=True)
return CreateMutation(model=model)
return type(
"create{}".format(object_model.__name__),
(CreateMutation,),
{
"Arguments": arguments,
"model": graphene.Field(object_model)
}
) |
@KCarretto : Thanks for the input! It's a good feature 👍 , and I assign this one to you 😛 Btw, please add the corresponding test case to make sure it works. |
I've got some working code that overlaps this issue but don't have time this month to publish it! @KCarretto, if you are diving into this, I have some insights you might consider: For the case of embedded documents, an input registry is extremely useful (also might want to consider using input objects instead of direct kwargs). To automatically generate an InputObject, we're using:
We're creating a custom name for the mutation so the return type looks nicer in graphiql/docs, but creating a Payload object might be more broadly recognized. Update and delete are very similar. The api for using this in a schema is:
This creates a mutation and input type that looks like If we had an embedded document that we wanted to accept as a nested input structure, we would manually create the input type the same way we manually create an embedded objecttype:
And this would get registered and automatically used in the mutation based on the embedded mongoengine field type.
|
I end up creating a pull request by myself, which takes part of you guys suggestion (implemented in mutation.py) There are 2 things I want to bring up:
Feel free to leave your comments in #36 directly, I am all ears. |
@abawchen Thanks for the PR, sorry I couldn't get around to it. I'll try to make time to check it out later this week. |
Are there any plans to add support for simple create / update / delete mutations? It seems like you could view the fields associated with a Mongoengine Document and automatically build the arguments to a graphene mutation, instead of needing to manually write one for each object. Thoughts on this?
The text was updated successfully, but these errors were encountered: