-
Notifications
You must be signed in to change notification settings - Fork 83
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 mutations #47
Comments
@wodCZ this is somewhat related to your problem so I thought I'd share to see if it helps (tl;dr at the bottom): I've been implementing subscriptions using class MySubscription(channels_graphql_ws.Subscription):
# ...
my_node = graphene.Field(nodes.MyNode)
@staticmethod
def publish(payload, info, **kwargs):
return MySubscription(my_node=gql_optimizer.query(MyNode.objects.filter(**kwargs).first(), info)) After some digging I found that, under the hood, the problem boils down to a mismatch between the The solution (or so I thought) was to find a way to make the optimizer "think" that it was optimizing the What I did originally was a bit of a hack. I basically copied and tweaked the class MySubscription(channels_graphql_ws.Subscription):
# ...
my_node = graphene.Field(nodes.MyNode)
@staticmethod
def publish(payload, info, **kwargs):
# In order for the query optimizer to function as expected, we must adjust the ResolverInfo
# object such that the root of the resolver is the `my_node` field. Effectively changing
# this `publish` method into a resolver for the `my_node` field.
new_info = copy.copy(info)
new_info.field_name = 'my_node'
new_info.return_type = info.schema.get_graphql_type(MyNode)
new_info.parent_type = info.schema.get_graphql_type(MySubscription)
# The selected fields should be whatever subfields of `my_node` were selected in the query.
new_info.field_asts = info.field_asts[0].selection_set.selection
return MySubscription(my_node=gql_optimizer.query(MyNode.objects.filter(**kwargs).first(), info)) This was a fairly unsatisfying -- albeit working -- hack. Later, as I was looking at the source for mutations, I found that you can specify a node type for This allows the optimizer to match up the current field type (a tl;dr Try this: import graphene_django_optimizer as gql_optimizer
class MyNode(gql_optimizer.OptimizedDjangoObjectType):
class Meta:
model = MyNodeModel
# ...
class MyMutation:
#...
class Meta:
# Tell MyMutation.Field() that it returns a `MyNode` instead of a
# `MyMutation` with a set of fields.
output = MyNode
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
# ...
return gql_optimizer.query(MyNode._meta.model.objects.filter(pk=my_id), info) |
Hi @eabruzzese, thank you for extensive response! I've tried a quick test of the tl;dr solution, and I was immediately stopped by this assert on relay mutation. Somehow the mutation I'm testing is now optimized without any extra code 🤔 I'll have to further investigate what's the difference between our two similar projects. Maybe it's already magically resolved 🎉 I'll keep this issue open and I'll come back with what I've found. |
Hi,
we're successfully using the optimizer for queries and it works perfectly.
But now I've noticed that mutation result is not optimized, which generates tons of sql queries.
I've tried tricking the resolver by calling
MyNode.get_optimized_node
directly, but without success:Result is returned properly, but without optimization.
I've noticed a mention about mutations here: #18, but it didn't lead me anywhere.
Is there some workaround?
Thank you!
The text was updated successfully, but these errors were encountered: