Skip to content

Django relations not traversed #43

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

Closed
adamcharnock opened this issue Nov 20, 2015 · 5 comments
Closed

Django relations not traversed #43

adamcharnock opened this issue Nov 20, 2015 · 5 comments

Comments

@adamcharnock
Copy link
Contributor

Perhaps this is a known limitation, or perhaps I'm doing it wrong.

My I have the following (simplified) django models:

class Account(TimeStampedModel, models.Model):
    name = models.CharField(max_length=100)
    ... more fields ...

class Transaction(TimeStampedModel, models.Model):
    from_account = models.ForeignKey(Account, related_name='transactions_out')
    ... more fields ...

Desire: I want to traverse the Account.transactions_out reverse relationship.

My Query & Nodes look like this:

class Query(ObjectType):
    all_accounts = relay.ConnectionField(nodes.Account, description='All the accounts', args={
        'name': graphene.String(),
    })
    all_transactions = relay.ConnectionField(nodes.Transaction, description='All the transactions')

    account = relay.NodeField(nodes.Account)
    transaction = relay.NodeField(nodes.Transaction)

    @resolve_only_args
    def resolve_all_accounts(self, **kwargs):
        returns ...all accounts filtering for name...

    @resolve_only_args
    def resolve_all_transactions(self, **kwargs):
        returns ...all transactions...


class Account(DjangoNode):
    class Meta:
        model = models.Account

# Transaction node omitted , but is exactly the same form as the Account node

When I run the following query:

query {
  allAccounts(name: "adamcharnock") {
    edges {
      node {
        id,
        name,
        transactionsOut {
          edges {
            node {
              amount
}}}}}}}

The result comes back with "transactionsOut": null, even though transactions do exist.

Solution: After some debugging I've found the following change will produce results:

class Account(DjangoNode):
    class Meta:
        model = models.Account

    @classmethod
    def resolve_transactionsOut(cls, instance, args, attrs):
        return instance.instance.transactions_out.all()

Seems odd because: It seems like the Graphene's Django support does not:

  1. Convert transactionsOut -> transactions_out
  2. Probably more importantly, it does not navigate the relationship automatically.

However – and as always – it is quite possible I'm doing this wrong. I'm also aware this project is a work in progress. Also, if GitHub issues are not the correct place for this I'm happy to move it elsewhere.

@adamcharnock
Copy link
Contributor Author

BTW: I've had a good look around the code to figure this out. I'd ideally write a pull request for this, but I'm not confident enough with the codebase to really know where to start. My guess would be to somehow handle this in graphene.contrib.django.fields.ConnectionOrListField.

@syrusakbary
Copy link
Member

Oh! That's a bug! Thanks for such a detailed log!
As you pointed, if everything works using resolve_transactionsOut it seems that the problem is the field name is not converting well to snake_case.
So I will probably start looking there.

EDIT: Seems to be the the graphene.contrib.django.fields.ConnectionOrListField

@syrusakbary
Copy link
Member

@adamcharnock Could you copy here the output of this execution?

field = Account._meta.fields_map['transactions_out']
print (field, field.attname, field.name, field.object_type, field.type)

Thanks!
EDITED: Corrected from fields to fields_map

@syrusakbary
Copy link
Member

@adamcharnock I've recreated your issue and the last commit fixes it :)

@adamcharnock
Copy link
Contributor Author

Fantastic, just tried it and it works great :-)

I'm about to create another issue to start a discussion around point 2 in the above description.

ronachong pushed a commit to ronachong/graphene that referenced this issue Nov 3, 2017
fixes graphql-python#8, base import of field no longer works for query property. Us…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants