graphenDjango突变关系

2024-05-14 13:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用django-graphene创建一个包含关系的突变,并在响应中返回相关对象。在

Django 1.11.5款

石墨烯django 1.2.1

亚马逊红移

模型

import uuid

from django.db import models

from dwtools.sources.base_models import BaseModel


class MasterAccount(BaseModel):
    master_id = models.CharField(
        max_length=36, help_text='''The UUID of the record.''',
        unique=True,
        default=uuid.uuid4)
    account_nm = models.CharField(
        max_length=255, null=True, blank=True,
        help_text='''The account's name.''')

    class Meta:
        managed = False
        db_table = 'account_dev'
        app_label = 'mappings'


class PlatformAccount(BaseModel):
    platform_id = models.BigIntegerField(help_text='''Platform account ID''')
    master = models.ForeignKey(MasterAccount, to_field='master_id')

    class Meta:
        managed = False
        db_table = 'platform_dev'
        app_label = 'mappings'

GraphQL模型

^{pr2}$

架构

class MasterAccountNode(DjangoObjectType):
    class Meta:
        model = MasterAccount
        interfaces = (relay.Node,)


class PlatformAccountNode(DjangoObjectType):
    class Meta:
        model = PlatformAccount
        interfaces = (relay.Node,)


class CreateAccount(ClientIDMutation):
    class Input:
        account_nm = String()

    ok = Boolean()
    master_account = Field(MasterAccountNode)
    platform_account = Field(PlatformAccountNode)

    @classmethod
    def mutate_and_get_payload(cls, args, instance, info):
        ok = True
        master_account = MasterAccount(account_nm=args.get('account_nm'))

        master_account.save(force_insert=True)

        # Testing ID
        platform_id = 123456
        platform_account = PlatformAccount(master=master_account,
                                           platform_id=platform_id)
        platform_account.save()

        return CreateAccount(master_account=master_account,
                             platform_account=platform_account,
                             ok=ok)


class Mutations(AbstractType):
    """Mutations for the mappings class."""

    create_account = CreateAccount.Field()


class Query(AbstractType):
    """The account query class for GraphQL."""

    master_account = Field(MasterAccountGraphQL)
    platform_account = Field(PlatformAccountGraphQL)

查询

mutation createAccount {
  createAccount(input: {accountNm: "testing account 2"}) {
    masterAccount {
      masterId
      accountNm
      id
      platformaccount {
        edges {
          node {
            platformId
          }
        }
      }
    }
    ok
  }
  __debug {
    sql {
      rawSql
    }
  }
}

结果

   {
  "data": {
    "createAccount": {
      "masterAccount": {
        "masterId": "698afbcb-5c90-43ec-b234-f15e0cd320b0",
        "accountNm": "testing account 2",
        "id": "TWFzdGVyQWNjb3VudE5vZGU6MTU=",
        "platformaccount": {
          "edges": [
            {
              "node": {
                "platformId": 123456
              }
            },
            {
              "node": {
                "platformId": 123456
              }
            },
            {
              "node": {
                "platformId": 123456
              }
            }
          ]
        }
      },
      "ok": true
    },
    "__debug": {
      "sql": [
        {
          "rawSql": "INSERT INTO \"account_dev\" (\"created_dt\", \"created_user\", \"updated_dt\", \"updated_user\", \"master_id\", \"account_nm\") VALUES (%s, %s, %s, %s, %s, %s)"
        },
        {
          "rawSql": "SELECT MAX(id) from \"account_dev\""
        },
        {
          "rawSql": "INSERT INTO \"platform_dev\" (\"created_dt\", \"created_user\", \"updated_dt\", \"updated_user\", \"platform_id\", \"master_id\") VALUES (%s, %s, %s, %s, %s, %s)"
        },
        {
          "rawSql": "SELECT MAX(id) from \"platform_dev\""
        },
        {
          "rawSql": "SELECT COUNT(*) AS \"__count\" FROM \"platform_dev\""
        },
        {
          "rawSql": "SELECT \"platform_dev\".\"id\", \"platform_dev\".\"created_dt\", \"platform_dev\".\"created_user\", \"platform_dev\".\"updated_dt\", \"platform_dev\".\"updated_user\", \"platform_dev\".\"platform_id\", \"platform_dev\".\"master_id\" FROM \"platform_dev\" LIMIT 3"
        }
      ]
    }
  }
}

数据似乎正确地插入了两个表中。但是,我在结果中寻找的是与masterAccount相关的platformaccount。我正在接收数据库中的所有platformaccount,而不仅仅是通过masterId与新创建的主服务器相关的帐户。在

我怎么才能只拿回相关记录?在


Tags: devmasteridtruemodelsdtokaccount

热门问题