graphen-django突变关系

时间:2017-09-27 16:34:18

标签: python django graphql

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

Django 1.11.5

graphene-django 1.2.1

Amazon Redshift

模型

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模型

import graphene
from graphene import ObjectType, String


class PlatformAccountGraphQL(ObjectType):
    platform_id = String()


class MasterAccountGraphQL(ObjectType):
    master_id = String()
    account_nm = String()
    platform_account = graphene.Field(PlatformAccountGraphQL)

模式

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"
        }
      ]
    }
  }
}

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

如何仅取回相关记录?

0 个答案:

没有答案