Skip to content

Django relations not traversed  #43

Closed
@adamcharnock

Description

@adamcharnock

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions