【问题标题】:Can't access nested dictionary data **kwargs python and best practices for nested data mutation in GraphQL无法访问嵌套字典数据 **kwargs python 和 GraphQL 中嵌套数据突变的最佳实践
【发布时间】:2021-05-05 23:55:25
【问题描述】:

在 Graphene-Django 和 GraphQL 中,我正在尝试创建一个 resolve_or_create 方法,用于在我的突变中创建嵌套数据。

我正在尝试将用户输入的字典作为 **kwarg 传递给我的 resolve_or_create 函数,虽然我可以在变量观察器中看到“位置”(在 VSCode 中),但我不断收到错误'dict' object has no attribute 'location'

这是我的 resolve_or_create 方法:

def resolve_or_create(*args, **kwargs):
    input = {}
    result = {}
    input.location = kwargs.get('location', None)

    if input.location is not None:
        input.location = Location.objects.filter(pk=input.location.id).first()
        if input.location is None:
            location = Location.objects.create(
                location_city = input.location.location_city,
                location_state = input.location.location_state,
                location_sales_tax_rate = input.location.location_sales_tax_rate
            )
            if location is None:
                return None
        result.location = location
    return result

还有我的 CreateCustomer 定义,这个方法被调用的地方

class CreateCustomer(graphene.Mutation):
    class Arguments:
        input = CustomerInput(required=True)
    
    ok = graphene.Boolean()
    customer = graphene.Field(CustomerType)

    @staticmethod
    def mutate(root, info, input=None):
        ok = True
        resolved = resolve_or_create(**{'location':input.customer_city})
        customer_instance = Customer(
            customer_name = input.customer_name,
            customer_address = input.customer_address,
            customer_city = resolved.location,
            customer_state = input.customer_state,
            customer_zip = input.customer_zip,
            customer_email = input.customer_email,
            customer_cell_phone = input.customer_cell_phone,
            customer_home_phone = input.customer_home_phone,
            referred_from = input.referred_from
        )
        customer_instance.save()
        return CreateCustomer(ok=ok, customer=customer_instance)

这是一个示例突变,它将创建一个具有现有位置的新客户

mutation createCustomer {
  createCustomer(input: {
    customerName: "Ricky Bobby",
    customerAddress: "1050 Airport Drive",
    customerCity: {id:1},
    customerState: "TX",
    customerZip: "75222",
    customerEmail: "mem",
    customerCellPhone: "124567894",
    referredFrom: "g"
  }) {
    ok,
    customer{
      id,
      customerName,
      customerAddress,
      customerCity {
        id
      },
      customerState,
      customerZip,
      customerEmail,
      customerCellPhone,
      referredFrom
    }
  }
}

这是一个示例突变,它将创建具有新位置的客户

mutation createCustomer {
  createCustomer(input: {
    customerName: "Ricky Bobby",
    customerAddress: "1050 Airport Drive",
    customerCity: {locationCity: "Dallas", locationState: "TX", locationSalesTaxRate:7.77},
    customerState: "TX",
    customerZip: "75222",
    customerEmail: "mem",
    customerCellPhone: "124567894",
    referredFrom: "g"
  }) {
    ok,
    customer{
      id,
      customerName,
      customerAddress,
      customerCity {
        id
      },
      customerState,
      customerZip,
      customerEmail,
      customerCellPhone,
      referredFrom
    }
  }
}

所以我的问题有两个。

首先,如何从 kwargs 检索我传入的位置字典?

其次,有没有比这更好的方法来解析和创建嵌套数据?这是最佳实践 GraphQL API 中的预期行为吗?

我也试过resolve_or_create(location=input.customer_city})

【问题讨论】:

    标签: python django graphql graphene-django


    【解决方案1】:

    我意识到我的字典分配和访问语法是错误的。

    修正函数:

    def resolve_or_create(*args, **kwargs):
        input = {}
        result = {}
        candidate = None
        input['location'] = kwargs['location']
        input['customer'] = kwargs.get('customer', None)
        input['technician'] = kwargs.get('tech', None)
    
        if input['location'] is not None:
            if 'id' in input['location']:
                candidate = Location.objects.filter(pk=input['location']['id']).first()
                result['location'] = candidate
            if candidate is None:
                result['location'] = Location.objects.create(
                    location_city = input['location']['location_city'],
                    location_state = input['location']['location_state'],
                    location_sales_tax_rate = input['location']['location_sales_tax_rate']
                )
                if result['location'] is None:
                    return None
        return result
    
    

    我仍然想讨论在 graphene-django 中完成创建和变异嵌套数据的最有效方法。有没有一种 DRYer 方法可以实现我想要做的事情或我缺少的事情?

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2020-10-21
      • 2020-10-03
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      相关资源
      最近更新 更多