0

在 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})

4

1 回答 1

0

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

修正功能:

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 方法可以实现我想要做的事情或我错过的事情?

于 2021-02-01T19:50:31.820 回答